• Profiling a site with latest WooCommerce and PHP7.4 fills the debug.log with fatal errors and a site crash, which only ?P3_SHUTOFF=1 can fix.

    The error concerns many WC pages and looks like this:

    PHP Fatal error: strict_types declaration must be the very first statement in the script in \wp-content\plugins\woocommerce\lib\packages\League\Container\Definition\DefinitionAggregateInterface.php on line 1

    Skimming through the P3 code, it seems there’s a flaw in the class.streamwrapper.php approach of blindly injecting <?php declare(ticks=1);?> as the first line of streamed php pages. When a php page already has a declare(strict_types=1); statement as its first line, then the aforementioned error will arise at run time, because the injected <?php declare(ticks=1);?> statement now takes precedence at the top.

    A possible fix is concatenating the declare statements as <?php declare(ticks=1,strict_types=1);?> etc. in a case by case basis, but then the class FileStreamWrapper implementation needs to be updated accordingly.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter alx359

    (@alx359)

    (redundant)

    Thread Starter alx359

    (@alx359)

    Looked into this some more and came up with a possible fix. It’s a rather ugly hack but appears to get the job done in the least intrusive manner for someone unfamiliar with the code.

    Namely:

    \plugins\p3-profiler\classes\class.streamwrapper.php line 14, changed this:

    define('STREAM_WRAPPER_INJECTION_STRING', '<?php declare(ticks=1);?>');
    define('STREAM_WRAPPER_INJECTION_LENGTH', 25);

    to this:

    define('STREAM_WRAPPER_INJECTION_STRING_ALT', '<?php declare(ticks=1,strict_types=1);?>');
    define('STREAM_WRAPPER_INJECTION_STRING',     '<?php declare(ticks=1);               ?>'); //add spaces to match
    define('STREAM_WRAPPER_INJECTION_LENGTH', 40);

    and at line 119, changed this:

    return $this->doWrapper(function () use ($count) {
        $result = '';
        if (ftell($this->handle) == 0 && $this->is_php_file) {
            $result = STREAM_WRAPPER_INJECTION_STRING;
            $count -= STREAM_WRAPPER_INJECTION_LENGTH;
        }
        return $result . fread($this->handle, $count);
    });

    to this:

    return $this->doWrapper(function () use ($count) {
        $result  = '';
        $wrap_it = false;
        if (ftell($this->handle) == 0 && $this->is_php_file) {
            $count  -= STREAM_WRAPPER_INJECTION_LENGTH;
            $wrap_it = true;
        }
        $file = fread($this->handle, $count);
    
        // discern injection string type to use
        if ($wrap_it) {
            // look for statement: "declare(strict_types=1);" 
            $pattern = '/declare\s*+\(\s*+strict_types\s*+\=\s*+1\s*+\)\s*+;/i';
            if (preg_match($pattern, $file, $matches)) {
                // replace statement with same amount of spaces, to keep file stream the same size
                $replace_with = str_repeat(' ', strlen($matches[0]));
              //$file = preg_replace($pattern, $replace_with, $file, 1);
                $file = str_replace($matches[0], $replace_with, $file);
                // use alternate injection string
                $result = STREAM_WRAPPER_INJECTION_STRING_ALT;
            } else {
                // use default injection string
                $result = STREAM_WRAPPER_INJECTION_STRING;
            }
        }
        return $result . $file;
    });

    The bolted on code above appears to fix the reported issue, but the next show-stopper is multiple memory allocation errors that happen specifically in the lines below when doing an Auto Scan:

    PHP Fatal error:  Out of memory (allocated 138412032) (tried to allocate 6291464 bytes) in \plugins\p3-profiler\classes\class.p3-profiler.php on line 278
    PHP Fatal error:  Out of memory (allocated 161480704) (tried to allocate 4096 bytes) in \plugins\p3-profiler\classes\class.p3-profiler.php on line 306

    Have a rather heavy WC site with 76 plugins active and I’m doing the tests only on WAMP atm. Tried tweaking various memory params in WP and PHP with mixed results. More times than not had to stop tests with ?P3_SHUTOFF=1.

    Hope for advise. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘strict_types=1 fatal error’ is closed to new replies.