Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties in anonymous classes marked as undefined #75

Closed
Levivb opened this issue Mar 23, 2019 · 2 comments
Closed

Properties in anonymous classes marked as undefined #75

Levivb opened this issue Mar 23, 2019 · 2 comments
Labels

Comments

@Levivb
Copy link
Contributor

Levivb commented Mar 23, 2019

Hey,

phpcs: PHP_CodeSniffer version 3.4.1 (stable) by Squiz (http://www.squiz.net)
sirbrillig/phpcs-variable-analysis: v2.6.0

It looks like anonymous classed aren't fully supported.
I did notice this issue: #52 which is referenced in this pr: https://github.com/sirbrillig/phpcs-variable-analysis/pull/53/files

Both changes in VariableAnalysis/Lib/Helpers.php and VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php are in my local installation.

However, this heavily simplified file:

<?php
declare(strict_types=1);

namespace Test;

class Test {
    public function getTest() {
        return new class() {
            protected $property;

            public function __construct() {
                $this->property = 'my-property';
            }

            public function getProperty() {
                return $this->property;
            }
        };
    }
}

Shows the following error: 13 | WARNING | Variable $property is undefined.

A var_export($token) on vendor/sirbrillig/phpcs-variable-analysis/VariableAnalysis/Lib/Helpers.php:196 shows:

array (
  'code' => 320,
  'type' => 'T_VARIABLE',
  'content' => '$property',
  'line' => 13,
  'column' => 23,
  'length' => 9,
  'level' => 3,
  'conditions' =>
  array (
    16 => 361,
    25 => 346,
    39 => 'PHPCS_T_ANON_CLASS',
  ),
)

I'm not that knowledgable about the inner workings of the code sniffer, but maybe you can reproduce the issue and possibly fix it?

Thanks anyway!

@sirbrillig
Copy link
Owner

Thanks for noticing! It turns out that while the sniff does ignore certain variables (the fix in #53), it doesn't actually recognize class property definitions explicitly, which I think was leading to this bug. #77 should fix this.

@sirbrillig
Copy link
Owner

(I'll wait until #76 has been merged before making a release to include this fix.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants