Skip to content

Commit 122a9bf

Browse files
committed
RequireConstructorPropertyPromotionSniff: Fixed false positive
1 parent 28351a2 commit 122a9bf

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

SlevomatCodingStandard/Sniffs/Classes/RequireConstructorPropertyPromotionSniff.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
use function strtolower;
2222
use function substr;
2323
use function trim;
24+
use const T_ATTRIBUTE_END;
2425
use const T_BITWISE_AND;
2526
use const T_CALLABLE;
27+
use const T_CLOSE_CURLY_BRACKET;
2628
use const T_COMMA;
2729
use const T_ELLIPSIS;
2830
use const T_ELSE;
@@ -31,6 +33,7 @@
3133
use const T_FUNCTION;
3234
use const T_IF;
3335
use const T_OBJECT_OPERATOR;
36+
use const T_OPEN_CURLY_BRACKET;
3437
use const T_OPEN_PARENTHESIS;
3538
use const T_SEMICOLON;
3639
use const T_SWITCH;
@@ -138,6 +141,10 @@ public function process(File $phpcsFile, $functionPointer): void
138141
continue;
139142
}
140143

144+
if ($this->isPropertyWithAttribute($phpcsFile, $propertyPointer)) {
145+
continue;
146+
}
147+
141148
$propertyTypeHint = PropertyHelper::findTypeHint($phpcsFile, $propertyPointer);
142149
$parameterTypeHint = FunctionHelper::getParametersTypeHints($phpcsFile, $functionPointer)[$parameterName];
143150
if (!$this->areTypeHintEqual($parameterTypeHint, $propertyTypeHint)) {
@@ -322,6 +329,19 @@ private function isPropertyDocCommentUseful(File $phpcsFile, int $propertyPointe
322329
return false;
323330
}
324331

332+
private function isPropertyWithAttribute(File $phpcsFile, int $propertyPointer): bool
333+
{
334+
$tokens = $phpcsFile->getTokens();
335+
336+
$previousPointer = TokenHelper::findPrevious(
337+
$phpcsFile,
338+
[T_ATTRIBUTE_END, T_SEMICOLON, T_OPEN_CURLY_BRACKET, T_CLOSE_CURLY_BRACKET],
339+
$propertyPointer - 1
340+
);
341+
342+
return $tokens[$previousPointer]['code'] === T_ATTRIBUTE_END;
343+
}
344+
325345
private function areTypeHintEqual(?TypeHint $parameterTypeHint, ?TypeHint $propertyTypeHint): bool
326346
{
327347
if ($parameterTypeHint === null && $propertyTypeHint === null) {

tests/Sniffs/Classes/RequireConstructorPropertyPromotionSniffTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testErrors(): void
2121
'enable' => true,
2222
]);
2323

24-
self::assertSame(4, $report->getErrorCount());
24+
self::assertSame(5, $report->getErrorCount());
2525

2626
self::assertSniffError(
2727
$report,
@@ -43,7 +43,13 @@ public function testErrors(): void
4343
);
4444
self::assertSniffError(
4545
$report,
46-
29,
46+
18,
47+
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
48+
'Required promotion of property $e.'
49+
);
50+
self::assertSniffError(
51+
$report,
52+
36,
4753
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
4854
'Required promotion of property $from.'
4955
);

tests/Sniffs/Classes/data/requireConstructorPropertyPromotionErrors.fixed.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
class Whatever
44
{
55

6-
public function __construct(public string $a, protected int|null $b = 0, private ?bool $c = null)
6+
#[SomeAttribute]
7+
private $d;
8+
9+
public function __construct(public string $a, protected int|null $b = 0, private ?bool $c = null, $d, private $e)
710
{
11+
$this->d = $d;
812
}
913

1014
}

tests/Sniffs/Classes/data/requireConstructorPropertyPromotionErrors.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ class Whatever
1212

1313
private ?bool $c = null;
1414

15-
public function __construct(string $a, int|null $b = 0, ?bool $c)
15+
#[SomeAttribute]
16+
private $d;
17+
18+
private $e;
19+
20+
public function __construct(string $a, int|null $b = 0, ?bool $c, $d, $e)
1621
{
1722
$this->a = $a;
1823
$this->b = $b;
1924
$this->c = $c;
25+
$this->d = $d;
26+
$this->e = $e;
2027
}
2128

2229
}

tests/Sniffs/Classes/data/requireConstructorPropertyPromotionNoErrors.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ class Nothing
7272

7373
private $j;
7474

75-
public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j)
75+
#[SomeAttribute]
76+
private $k;
77+
78+
public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j, $k)
7679
{
7780
$phpVersion = phpversion();
7881

@@ -97,6 +100,8 @@ public function __construct($a, $c, $d, $e, $f, $g, $h, string $i, string $j)
97100
$this->i = $i;
98101

99102
$this->j = $j;
103+
104+
$this->k = $k;
100105
}
101106

102107
}

0 commit comments

Comments
 (0)