Skip to content

Commit 43ab100

Browse files
maryokukulich
authored andcommitted
RequireConstructorPropertyPromotion: Properly autofixing when argument name has an attribute
1 parent 06b18b3 commit 43ab100

4 files changed

+49
-2
lines changed

SlevomatCodingStandard/Sniffs/Classes/RequireConstructorPropertyPromotionSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ public function process(File $phpcsFile, $functionPointer): void
201201
TokenHelper::findNextNonWhitespace($phpcsFile, $propertyEndPointer + 1),
202202
);
203203

204-
$pointerBeforeParameterStart = TokenHelper::findPrevious($phpcsFile, [T_COMMA, T_OPEN_PARENTHESIS], $parameterPointer - 1);
204+
$pointerBeforeParameterStart = TokenHelper::findPrevious(
205+
$phpcsFile,
206+
[T_COMMA, T_OPEN_PARENTHESIS, T_ATTRIBUTE_END],
207+
$parameterPointer - 1,
208+
);
205209
$parameterStartPointer = TokenHelper::findNextEffective($phpcsFile, $pointerBeforeParameterStart + 1);
206210

207211
$parameterEqualPointer = TokenHelper::findNextEffective($phpcsFile, $parameterPointer + 1);

tests/Sniffs/Classes/RequireConstructorPropertyPromotionSniffTest.php

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

24-
self::assertSame(7, $report->getErrorCount());
24+
self::assertSame(9, $report->getErrorCount());
2525

2626
self::assertSniffError(
2727
$report,
@@ -65,6 +65,18 @@ public function testErrors(): void
6565
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
6666
'Required promotion of property $from.',
6767
);
68+
self::assertSniffError(
69+
$report,
70+
60,
71+
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
72+
'Required promotion of property $login.',
73+
);
74+
self::assertSniffError(
75+
$report,
76+
61,
77+
RequireConstructorPropertyPromotionSniff::CODE_REQUIRED_CONSTRUCTOR_PROPERTY_PROMOTION,
78+
'Required promotion of property $password.',
79+
);
6880

6981
self::assertAllFixedInFile($report);
7082
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ public function __construct(bool $active = false, private ?DateTimeImmutable $fr
2828
}
2929

3030
}
31+
32+
class Credentials
33+
{
34+
35+
public function __construct(
36+
#[\SensitiveParameter]
37+
private string $login,
38+
#[\SensitiveParameter]
39+
private string $password
40+
) {
41+
}
42+
43+
}

tests/Sniffs/Classes/data/requireConstructorPropertyPromotionErrors.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ public function __construct(bool $active = false, ?DateTimeImmutable $from = nul
5353
}
5454

5555
}
56+
57+
class Credentials
58+
{
59+
60+
private string $login;
61+
private string $password;
62+
63+
public function __construct(
64+
#[\SensitiveParameter]
65+
string $login,
66+
#[\SensitiveParameter]
67+
string $password
68+
) {
69+
$this->login = $login;
70+
$this->password = $password;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)