civicrm / cv

CiviCRM CLI Utility
26 stars 29 forks source link

Remove PHP Deprecation warnings when booting in WordPress #210

Open michaelmcandrew opened 2 weeks ago

michaelmcandrew commented 2 weeks ago
admin@civicrm:/var/www/html$ cv ev 'echo 1'
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:120
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:120
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:127
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:134
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:134
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:141
[PHP Deprecation] str_contains(): Passing null to parameter #1 ($haystack) of type string is deprecated at /var/www/html/wp-includes/vars.php:141
1

This happens because wordpress does this in var/www/html/wp-includes/vars.php at line 120

$is_apache = (str_contains($_SERVER['SERVER_SOFTWARE'], 'Apache') || str_contains($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed'));

According to https://www.php.net/manual/en/reserved.variables.server.php

When running PHP on the command line most of these entries will not be available or have any meaning.

The wp cli doesn't complain becuase earlier in wp-includes/load.php WordPress has done this:

function wp_fix_server_vars() {
    global $PHP_SELF;

    $default_server_values = array(
        'SERVER_SOFTWARE' => '',
        'REQUEST_URI'     => '',
    );

    $_SERVER = array_merge( $default_server_values, $_SERVER );

Is there some way that we can detect is cv is booting WP and if so do this

    $default_server_values = array(
        'SERVER_SOFTWARE' => '',
        'REQUEST_URI'     => '',
    );

    $_SERVER = array_merge( $default_server_values, $_SERVER );

Otherwise we will have to live with those PHP Deprecation warnings for ever.

michaelmcandrew commented 2 weeks ago

Somewhere in here, maybe? https://github.com/civicrm/cv/blob/master/lib/src/Util/BootTrait.php