Skip to content

Commit 372cbf2

Browse files
committed
SlevomatCodingStandard.Classes.PropertyDeclaration: Fixed false positives where there's function with "static" return type hint before property
1 parent 3c0ae90 commit 372cbf2

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

SlevomatCodingStandard/Sniffs/Classes/PropertyDeclarationSniff.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@
2525
use const T_DOUBLE_COLON;
2626
use const T_FUNCTION;
2727
use const T_NULLABLE;
28+
use const T_OPEN_CURLY_BRACKET;
29+
use const T_OPEN_PARENTHESIS;
2830
use const T_PRIVATE;
2931
use const T_PROTECTED;
3032
use const T_PUBLIC;
3133
use const T_READONLY;
34+
use const T_SEMICOLON;
3235
use const T_STATIC;
36+
use const T_TYPE_INTERSECTION;
37+
use const T_TYPE_UNION;
3338
use const T_VAR;
3439
use const T_VARIABLE;
3540
use const T_WHITESPACE;
@@ -97,6 +102,16 @@ public function process(File $phpcsFile, $modifierPointer): void
97102
return;
98103
}
99104

105+
if ($tokens[$nextPointer]['code'] === T_OPEN_PARENTHESIS) {
106+
// Ignore static()
107+
return;
108+
}
109+
110+
if (in_array($tokens[$nextPointer]['code'], [T_OPEN_CURLY_BRACKET, T_SEMICOLON, T_TYPE_UNION, T_TYPE_INTERSECTION], true)) {
111+
// Ignore "static" as return type hint of method
112+
return;
113+
}
114+
100115
$propertyPointer = TokenHelper::findNext($phpcsFile, [T_FUNCTION, T_CONST, T_VARIABLE], $modifierPointer + 1);
101116

102117
if ($propertyPointer === null || $tokens[$propertyPointer]['code'] !== T_VARIABLE) {

tests/Sniffs/Classes/data/propertyDeclarationNoErrors.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,63 @@ public static function detailsUpdated(): self
4747

4848
public User $user;
4949
}
50+
51+
abstract class Test
52+
{
53+
abstract public function begin(): static;
54+
55+
protected array $field = [];
56+
}
57+
58+
class Test2
59+
{
60+
public function begin(): static
61+
{}
62+
63+
protected array $field = [];
64+
}
65+
66+
abstract class Test3
67+
{
68+
abstract public function begin(): static|null;
69+
70+
protected array $field = [];
71+
}
72+
73+
class Test4
74+
{
75+
public function begin(): static|null
76+
{}
77+
78+
protected array $field = [];
79+
}
80+
81+
class Test5
82+
{
83+
public function begin(): false|static|null
84+
{}
85+
86+
protected array $field = [];
87+
}
88+
89+
class Test6
90+
{
91+
public function begin(): static&null
92+
{}
93+
94+
protected array $field = [];
95+
}
96+
97+
class Tes7
98+
{
99+
public static function begin(): static
100+
{
101+
return new static();
102+
}
103+
104+
protected array $field = [];
105+
106+
final private function __construct()
107+
{
108+
}
109+
}

0 commit comments

Comments
 (0)