Skip to content

Commit 01cd5ae

Browse files
jrfnlalekitto
andcommitted
PHP 8.0 | Variables::getMemberProperties(): correctly handle attributes
Sync with upstream PR 3203 which ensures that attributes for properties are correctly regarded as "stop points" for gathering property information. Ref: * squizlabs/PHP_CodeSniffer 3203 Co-authored-by: Alessandro Chitolina <alekitto@gmail.com>
1 parent 9a96c58 commit 01cd5ae

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

PHPCSUtils/Utils/Variables.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Variables
8888
*
8989
* @since 1.0.0
9090
* @since 1.0.0-alpha4 Added support for PHP 8.0 union types.
91+
* @since 1.0.0-alpha4 No longer gets confused by PHP 8.0 property attributes.
9192
*
9293
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
9394
* @param int $stackPtr The position in the stack of the `T_VARIABLE` token
@@ -138,6 +139,7 @@ public static function getMemberProperties(File $phpcsFile, $stackPtr)
138139
\T_SEMICOLON,
139140
\T_OPEN_CURLY_BRACKET,
140141
\T_CLOSE_CURLY_BRACKET,
142+
\T_ATTRIBUTE_END,
141143
],
142144
($stackPtr - 1)
143145
);

Tests/BackCompat/BCFile/GetMemberPropertiesTest.inc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,19 @@ $anon = class() {
243243
// Intentional fatal error - duplicate types are not allowed in union types, but that's not the concern of the method.
244244
public int |string| /*comment*/ INT $duplicateTypeInUnion;
245245
};
246+
247+
$anon = class {
248+
/* testPHP8PropertySingleAttribute */
249+
#[PropertyWithAttribute]
250+
public string $foo;
251+
252+
/* testPHP8PropertyMultipleAttributes */
253+
#[PropertyWithAttribute(foo: 'bar'), MyAttribute]
254+
protected ?int|float $bar;
255+
256+
/* testPHP8PropertyMultilineAttribute */
257+
#[
258+
PropertyWithAttribute(/* comment */ 'baz')
259+
]
260+
private mixed $baz;
261+
};

Tests/BackCompat/BCFile/GetMemberPropertiesTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,42 @@ public function dataGetMemberProperties()
764764
'nullable_type' => false,
765765
],
766766
],
767+
'php8-property-with-single-attribute' => [
768+
'/* testPHP8PropertySingleAttribute */',
769+
[
770+
'scope' => 'public',
771+
'scope_specified' => true,
772+
'is_static' => false,
773+
'type' => 'string',
774+
'type_token' => -2, // Offset from the T_VARIABLE token.
775+
'type_end_token' => -2, // Offset from the T_VARIABLE token.
776+
'nullable_type' => false,
777+
],
778+
],
779+
'php8-property-with-multiple-attributes' => [
780+
'/* testPHP8PropertyMultipleAttributes */',
781+
[
782+
'scope' => 'protected',
783+
'scope_specified' => true,
784+
'is_static' => false,
785+
'type' => '?int|float',
786+
'type_token' => -4, // Offset from the T_VARIABLE token.
787+
'type_end_token' => -2, // Offset from the T_VARIABLE token.
788+
'nullable_type' => true,
789+
],
790+
],
791+
'php8-property-with-multiline-attribute' => [
792+
'/* testPHP8PropertyMultilineAttribute */',
793+
[
794+
'scope' => 'private',
795+
'scope_specified' => true,
796+
'is_static' => false,
797+
'type' => 'mixed',
798+
'type_token' => -2, // Offset from the T_VARIABLE token.
799+
'type_end_token' => -2, // Offset from the T_VARIABLE token.
800+
'nullable_type' => false,
801+
],
802+
],
767803
];
768804
}
769805

0 commit comments

Comments
 (0)