mrclay / minify

Combines. minifies, and serves CSS or Javascript files
BSD 3-Clause "New" or "Revised" License
3.01k stars 472 forks source link

Error in php8.1 Deprecated Functionality: preg_replace_callback(): Passing null to parameter 3 #701

Closed 0m3r closed 1 year ago

0m3r commented 1 year ago

A lot of error messages like this are logged into the log file

Exception: Deprecated Functionality: preg_replace_callback(): Passing null to parameter 3 (subject) of type array|string is deprecated in /var/www/vhosts/oasestore.nl/httpdocs/vendor/mrclay/minify/lib/Minify/HTML.php on line 111

    /**
     * Minify the markeup given in the constructor
     *
     * @return string
     */
    public function process()
    {
        if ($this->_isXhtml === null) {
            $this->_isXhtml = (false !== strpos($this->_html, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML'));
        }

        $this->_replacementHash = 'MINIFYHTML' . md5($_SERVER['REQUEST_TIME']);
        $this->_placeholders = array();

        // replace SCRIPTs (and minify) with placeholders
        $this->_html = preg_replace_callback(
            '/(\\s*)<script(\\b[^>]*?>)([\\s\\S]*?)<\\/script>(\\s*)/iu',
            array($this, '_removeScriptCB'),
            $this->_html
        );

        // replace STYLEs (and minify) with placeholders
        $this->_html = preg_replace_callback(
            '/\\s*<style(\\b[^>]*>)([\\s\\S]*?)<\\/style>\\s*/iu',
            array($this, '_removeStyleCB'),
            $this->_html
        );
glensc commented 1 year ago

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request

0m3r commented 1 year ago

The first preg_replace_callback return NULL

Return Values preg_replace_callback() returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is null. If matches are found, the new subject will be returned, otherwise subject will be returned unchanged.

because

protected function _removeScriptCB($m)
    {
...
        // minify
        $minifier = $this->_jsMinifier
            ? $this->_jsMinifier
            : 'trim';
        $js = call_user_func($minifier, $js);

throw exception

  | string(48) "JSMin: Unterminated RegExp at byte 2320: /ko--> "
  | string(2076) "#0 /var/www/vhosts/xxx.nl/httpdocs/vendor/mrclay/jsmin-php/src/JSMin/JSMin.php(150): JSMin\JSMin->action()
  | #1 /var/www/vhosts/xxx.nl/httpdocs/vendor/mrclay/jsmin-php/src/JSMin/JSMin.php(84): JSMin\JSMin->min()
  | #2 [internal function]: JSMin\JSMin::minify()
glensc commented 1 year ago

Send pull request with your proposed fix