Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 04e3d23

Browse files
Simplify detecting class properties to improve detection for anonymous classes (#337)
* Add test for anon class with typehint properties * 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. * Add readonly properties
1 parent e9dae13 commit 04e3d23

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

‎Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,28 @@ public function methodWithStaticVar() {
4141
echo static::$storedHello;
4242
}
4343
};
44+
45+
class ClassWithAnonymousClassAndTypeHints
46+
{
47+
readonly int $main_id;
48+
public int $id = 1;
49+
public \My\Data|bool $data;
50+
51+
public function test_1(): object
52+
{
53+
return new class
54+
{
55+
readonly int $main_id;
56+
public int $id = 123456;
57+
public \My\Data|bool $data;
58+
};
59+
}
60+
61+
public function test_2(): object
62+
{
63+
return new class
64+
{
65+
public $id = 123456;
66+
};
67+
}
68+
}

‎VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -864,29 +864,7 @@ protected function processVariableAsUseImportDefinition(File $phpcsFile, $stackP
864864
*/
865865
protected function processVariableAsClassProperty(File $phpcsFile, $stackPtr)
866866
{
867-
$propertyDeclarationKeywords = [
868-
T_PUBLIC,
869-
T_PRIVATE,
870-
T_PROTECTED,
871-
T_VAR,
872-
];
873-
$stopAtPtr = $stackPtr - 2;
874-
$visibilityPtr = $phpcsFile->findPrevious($propertyDeclarationKeywords, $stackPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0);
875-
if ($visibilityPtr) {
876-
return true;
877-
}
878-
$staticPtr = $phpcsFile->findPrevious(T_STATIC, $stackPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0);
879-
if (! $staticPtr) {
880-
return false;
881-
}
882-
$stopAtPtr = $staticPtr - 2;
883-
$visibilityPtr = $phpcsFile->findPrevious($propertyDeclarationKeywords, $staticPtr - 1, $stopAtPtr > 0 ? $stopAtPtr : 0);
884-
if ($visibilityPtr) {
885-
return true;
886-
}
887-
// it's legal to use `static` to define properties as well as to
888-
// define variables, so make sure we are not in a function before
889-
// assuming it's a property.
867+
// Make sure we are not in a class method before assuming it's a property.
890868
$tokens = $phpcsFile->getTokens();
891869

892870
/** @var array{conditions?: (int|string)[], content?: string}|null */

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /