diff --git a/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php b/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php index ad59a10..71776f7 100644 --- a/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php @@ -41,3 +41,28 @@ public function methodWithStaticVar() { echo static::$storedHello; } }; + +class ClassWithAnonymousClassAndTypeHints +{ + readonly int $main_id; + public int $id = 1; + public \My\Data|bool $data; + + public function test_1(): object + { + return new class + { + readonly int $main_id; + public int $id = 123456; + public \My\Data|bool $data; + }; + } + + public function test_2(): object + { + return new class + { + public $id = 123456; + }; + } +} diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index a08f7b1..531cd40 100644 --- a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php +++ b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php @@ -864,29 +864,7 @@ protected function processVariableAsUseImportDefinition(File $phpcsFile, $stackP */ protected function processVariableAsClassProperty(File $phpcsFile, $stackPtr) { - $propertyDeclarationKeywords = [ - T_PUBLIC, - T_PRIVATE, - T_PROTECTED, - T_VAR, - ]; - $stopAtPtr = $stackPtr - 2; - $visibilityPtr = $phpcsFile->findPrevious($propertyDeclarationKeywords, $stackPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0); - if ($visibilityPtr) { - return true; - } - $staticPtr = $phpcsFile->findPrevious(T_STATIC, $stackPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0); - if (! $staticPtr) { - return false; - } - $stopAtPtr = $staticPtr - 2; - $visibilityPtr = $phpcsFile->findPrevious($propertyDeclarationKeywords, $staticPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0); - if ($visibilityPtr) { - return true; - } - // it's legal to use `static` to define properties as well as to - // define variables, so make sure we are not in a function before - // assuming it's a property. + // Make sure we are not in a class method before assuming it's a property. $tokens = $phpcsFile->getTokens(); /** @var array{conditions?: (int|string)[], content?: string}|null */