From b63cb8a5906d96683eb2fa3dd3b65328e1b5ff84 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Fri, 29 Nov 2024 13:32:54 -0500 Subject: [PATCH 1/3] Add test for anon class with typehint properties --- .../AnonymousClassWithPropertiesFixture.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php b/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php index ad59a105..9095527f 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 +{ + int $main_id = 1; + public int $id = 1; + public \My\Data|bool $data; + + public function test_1(): object + { + return new class + { + int $main_id = 1; + public int $id = 123456; + public \My\Data|bool $data; + }; + } + + public function test_2(): object + { + return new class + { + public $id = 123456; + }; + } +} From 168e2c24bb41d3cddea484e55024a670c91c56ef Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sat, 30 Nov 2024 11:54:26 -0500 Subject: [PATCH 2/3] Remove search for visibility keywords to find properties This defaults to only using the condition: if a variable is defined inside a class and not inside a method within a class, then it's a property. --- .../CodeAnalysis/VariableAnalysisSniff.php | 24 +------------------ 1 file changed, 1 insertion(+), 23 deletions(-) diff --git a/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php b/VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php index a08f7b17..531cd401 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 */ From d4a82865a94d956a760882316d12cee2d13be434 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Sat, 30 Nov 2024 12:23:24 -0500 Subject: [PATCH 3/3] Add readonly properties --- .../fixtures/AnonymousClassWithPropertiesFixture.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php b/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php index 9095527f..71776f7a 100644 --- a/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php +++ b/Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php @@ -44,7 +44,7 @@ public function methodWithStaticVar() { class ClassWithAnonymousClassAndTypeHints { - int $main_id = 1; + readonly int $main_id; public int $id = 1; public \My\Data|bool $data; @@ -52,7 +52,7 @@ public function test_1(): object { return new class { - int $main_id = 1; + readonly int $main_id; public int $id = 123456; public \My\Data|bool $data; };