PHPCompatibility / PHPCompatibility

PHP Compatibility check for PHP_CodeSniffer

Home Page:http://techblog.wimgodden.be/tag/codesniffer/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Weird errors relating $this in class methods

WanWizard opened this issue · comments

Bug Description

While checking the codebase for PHP 8.3 compatibility, I got these errors:

--------------------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------------------
 29 | ERROR | "$this" can no longer be used in a plain function or method since PHP 7.1.
--------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
-----------------------------------------------------------------------------------
 1671 | ERROR | "$this" can no longer be unset since PHP 7.1.
-----------------------------------------------------------------------------------

Given the following reproduction Scenario

The issue happens when running this command:

fuel/vendor/bin/phpcs -p fuel/packages --standard=PHPCompatibility --runtime-set testVersion 8.3

Code triggering the first:

class View_Plates extends \View
{
        // parser instance
        protected static $_parser;

        // create a parser instance
        public static function parser()
        {
                if (empty(static::$_parser))
                {
                        // create a parser instance
                        static::$_parser = new \League\Plates\Engine(null, $this->extension);   // <-- line 29
                }

                return static::$_parser;
        }
        ....

complete file: https://github.com/fuel/parser/blob/1.9/develop/classes/view/plates.php#L29

Code triggering the second:

class Model
{
        ....
        public function delete($cascade = null, $use_transaction = false)
        {
                ....
                // Perform cleanup:
                // remove from internal object cache, remove PK's, set to non saved object, remove db original values
                if (array_key_exists(get_called_class(), static::$_cached_objects)
                        and array_key_exists(static::implode_pk($this), static::$_cached_objects[get_called_class()]))
                {
                        unset(static::$_cached_objects[get_called_class()][static::implode_pk($this)]); // <-- line 1671
                }
                ....

complete file: https://github.com/fuel/orm/blob/1.9/develop/classes/model.php#L1671

I'd expect the following behaviour

No error being reported

Instead this happened

Errors were reported

Environment

Environment Answer
PHP version 8.2.15
PHP_CodeSniffer version 3.8.1
PHPCSUtils version ?
PHPCompatibility version 9.3.5
Install type Composer project local

@WanWizard The second one I need to take a look at as that looks like a false positive. The first one, however, is 100% correct. The parser() method is declared as static and does not have access to $this: https://3v4l.org/OFrnb#eol

Grmpfff, how could I have missed that? Thanks for the kick up the proverbial backside !

@WanWizard I've lined up a fix for the second issue and will make sure it is included in the 10.0.0 release.

PR #1670 should fix the bug for the unset() case. Testing appreciated.