Skip to content

Commit 2ea1223

Browse files
committed
Tokenizer/PHP: bug fix for static typed properties with union/intersection types
Just like the `var` keyword, the `static` keyword can also be used stand-alone with property declarations. https://3v4l.org/sbaDM In that case, the tokenization of the `|` operator was not changed to `T_TYPE_UNION` and the `&` operator was not changed to `T_TYPE_INTERSECTION` as the `static` keyword can also be used in return type declarations, so was seen as part of the type declaration. Fixed now by removing the `T_STATIC` token from the `$allowed` list before walking backwards from the operator. Includes tests. Note: this does mean that one test for the `File::getMemberProperties()` method needs to be changed, but as that was testing an illegal syntax anyway, I'm not concerned about making this change.
1 parent c1cf656 commit 2ea1223

8 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ The file documents changes to the PHP_CodeSniffer project.
127127
- Thanks to Dan Wallis (@fredden) for the patch
128128
- Fixed bug #3816 : PSR12/FileHeader: bug fix - false positives on PHP 8.2+ readonly classes
129129
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
130+
- Fixed bug #3867 : Tokenizer/PHP: union type and intersection type operators were not correctly tokenized for static typed properties without explicit visibility
131+
- Thanks to Juliette Reinders Folmer (@jrfnl) for the patch
130132
- Fixed bug #3877 : Filter names can be case-sensitive. The -h help text will now display the correct case for the available filters
131133
- Thanks to @simonsan for the patch
132134
- Fixed bug #3906 : Tokenizer/CSS: fixed a bug related to the unsupported slash comment syntax

src/Tokenizers/PHP.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2998,6 +2998,10 @@ protected function processAdditional()
29982998
continue;
29992999
}
30003000

3001+
if ($suspectedType === 'property or parameter') {
3002+
unset($allowed[\T_STATIC]);
3003+
}
3004+
30013005
$typeTokenCount = 0;
30023006
$typeOperators = [$i];
30033007
$confirmed = false;
@@ -3030,6 +3034,7 @@ protected function processAdditional()
30303034
if ($suspectedType === 'property or parameter'
30313035
&& (isset(Util\Tokens::$scopeModifiers[$this->tokens[$x]['code']]) === true
30323036
|| $this->tokens[$x]['code'] === T_VAR
3037+
|| $this->tokens[$x]['code'] === T_STATIC
30333038
|| $this->tokens[$x]['code'] === T_READONLY)
30343039
) {
30353040
// This will also confirm constructor property promotion parameters, but that's fine.

tests/Core/File/GetMemberPropertiesTest.inc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ $anon = class() {
210210

211211
/* testPHP8UnionTypesIllegalTypes */
212212
// Intentional fatal error - types which are not allowed for properties, but that's not the concern of the method.
213-
public callable|static|void $unionTypesIllegalTypes;
213+
// Note: static is also not allowed as a type, but using static for a property type is not supported by the tokenizer.
214+
public callable|void $unionTypesIllegalTypes;
214215

215216
/* testPHP8UnionTypesNullable */
216217
// Intentional fatal error - nullability is not allowed with union types, but that's not the concern of the method.

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,7 @@ public function dataGetMemberProperties()
587587
'scope_specified' => true,
588588
'is_static' => false,
589589
'is_readonly' => false,
590-
// Missing static, but that's OK as not an allowed syntax.
591-
'type' => 'callable||void',
590+
'type' => 'callable|void',
592591
'nullable_type' => false,
593592
],
594593
],

tests/Core/Tokenizer/BitwiseOrTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class TypeUnion
4848
/* testTypeUnionPropertyWithOnlyReadOnlyKeyword */
4949
readonly string|null $nullableString;
5050

51+
/* testTypeUnionPropertyWithOnlyStaticKeyword */
52+
static Foo|Bar $obj;
53+
5154
public function paramTypes(
5255
/* testTypeUnionParam1 */
5356
int|float $paramA /* testBitwiseOrParamDefaultValue */ = CONSTANT_A | CONSTANT_B,

tests/Core/Tokenizer/BitwiseOrTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function dataTypeUnion()
110110
['/* testTypeUnionPropertyWithStaticAndReadOnlyKeywords */'],
111111
['/* testTypeUnionPropertyWithVarAndReadOnlyKeywords */'],
112112
['/* testTypeUnionPropertyWithOnlyReadOnlyKeyword */'],
113+
['/* testTypeUnionPropertyWithOnlyStaticKeyword */'],
113114
['/* testTypeUnionParam1 */'],
114115
['/* testTypeUnionParam2 */'],
115116
['/* testTypeUnionParam3 */'],

tests/Core/Tokenizer/TypeIntersectionTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class TypeIntersection
3636
/* testTypeIntersectionPropertyWithReadOnlyKeyword */
3737
protected readonly Foo&Bar $fooBar;
3838

39+
/* testTypeIntersectionPropertyWithStaticKeyword */
40+
static Foo&Bar $obj;
41+
3942
public function paramTypes(
4043
/* testTypeIntersectionParam1 */
4144
Foo&Bar $paramA /* testBitwiseAndParamDefaultValue */ = CONSTANT_A & CONSTANT_B,

tests/Core/Tokenizer/TypeIntersectionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function dataTypeIntersection()
109109
['/* testTypeIntersectionPropertyPartiallyQualified */'],
110110
['/* testTypeIntersectionPropertyFullyQualified */'],
111111
['/* testTypeIntersectionPropertyWithReadOnlyKeyword */'],
112+
['/* testTypeIntersectionPropertyWithStaticKeyword */'],
112113
['/* testTypeIntersectionParam1 */'],
113114
['/* testTypeIntersectionParam2 */'],
114115
['/* testTypeIntersectionParam3 */'],

0 commit comments

Comments
 (0)