Skip to content

Commit 433c093

Browse files
committed
PropertyHelper can detect promoted properties
1 parent e08596b commit 433c093

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

SlevomatCodingStandard/Helpers/PropertyHelper.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,28 @@
3333
class PropertyHelper
3434
{
3535

36-
public static function isProperty(File $phpcsFile, int $variablePointer): bool
36+
public static function isProperty(File $phpcsFile, int $variablePointer, bool $promoted = false): bool
3737
{
3838
$tokens = $phpcsFile->getTokens();
3939

40-
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $variablePointer - 1);
40+
$previousPointer = TokenHelper::findPreviousExcluding(
41+
$phpcsFile,
42+
array_merge(TokenHelper::$ineffectiveTokenCodes, TokenHelper::getTypeHintTokenCodes(), [T_NULLABLE]),
43+
$variablePointer - 1
44+
);
4145

4246
if ($tokens[$previousPointer]['code'] === T_STATIC) {
4347
$previousPointer = TokenHelper::findPreviousEffective($phpcsFile, $previousPointer - 1);
4448
}
4549

4650
if (in_array($tokens[$previousPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_VAR, T_READONLY], true)) {
4751
$constructorPointer = TokenHelper::findPrevious($phpcsFile, T_FUNCTION, $previousPointer - 1);
48-
return $constructorPointer === null || $tokens[$constructorPointer]['parenthesis_closer'] < $previousPointer;
52+
53+
if ($constructorPointer === null) {
54+
return true;
55+
}
56+
57+
return $tokens[$constructorPointer]['parenthesis_closer'] < $previousPointer || $promoted;
4958
}
5059

5160
if (

tests/Helpers/PropertyHelperTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function dataIsProperty(): array
3434
'$boo',
3535
false,
3636
],
37+
[
38+
'$propertyPromotionWithTypeHint',
39+
false,
40+
],
3741
[
3842
'$hoo',
3943
false,
@@ -104,6 +108,28 @@ public function testIsProperty(string $variableName, bool $isProperty): void
104108
self::assertSame($isProperty, PropertyHelper::isProperty($phpcsFile, $variablePointer));
105109
}
106110

111+
/**
112+
* @return mixed[][]
113+
*/
114+
public function dataIsPropertyIfPromoted(): array
115+
{
116+
return [
117+
['$propertyPromotion'],
118+
['$propertyPromotionWithTypeHint'],
119+
];
120+
}
121+
122+
/**
123+
* @dataProvider dataIsPropertyIfPromoted
124+
*/
125+
public function testIsPropertyIfPromoted(string $variableName): void
126+
{
127+
$phpcsFile = $this->getTestedCodeSnifferFile();
128+
129+
$variablePointer = TokenHelper::findNextContent($phpcsFile, T_VARIABLE, $variableName, 0);
130+
self::assertTrue(PropertyHelper::isProperty($phpcsFile, $variablePointer, true));
131+
}
132+
107133
/**
108134
* @return mixed[][]
109135
*/

tests/Helpers/data/propertyOrNot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Foo
1010

1111
private static $string = 'string';
1212

13-
public function __construct(private $propertyPromotion, $boo)
13+
public function __construct(private $propertyPromotion, $boo, public readonly Foo|Bar $propertyPromotionWithTypeHint)
1414
{
1515
$hoo = $boo;
1616
}

0 commit comments

Comments
 (0)