Skip to content

Commit 6f80106

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 321d252 commit 6f80106

7 files changed

+16
-3
lines changed

src/Tokenizers/PHP.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,6 +2916,10 @@ protected function processAdditional()
29162916
continue;
29172917
}
29182918

2919+
if ($suspectedType === 'property or parameter') {
2920+
unset($allowed[\T_STATIC]);
2921+
}
2922+
29192923
$typeTokenCount = 0;
29202924
$typeOperators = [$i];
29212925
$confirmed = false;
@@ -2948,6 +2952,7 @@ protected function processAdditional()
29482952
if ($suspectedType === 'property or parameter'
29492953
&& (isset(Util\Tokens::$scopeModifiers[$this->tokens[$x]['code']]) === true
29502954
|| $this->tokens[$x]['code'] === T_VAR
2955+
|| $this->tokens[$x]['code'] === T_STATIC
29512956
|| $this->tokens[$x]['code'] === T_READONLY)
29522957
) {
29532958
// 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
@@ -584,8 +584,7 @@ public function dataGetMemberProperties()
584584
'scope_specified' => true,
585585
'is_static' => false,
586586
'is_readonly' => false,
587-
// Missing static, but that's OK as not an allowed syntax.
588-
'type' => 'callable||void',
587+
'type' => 'callable|void',
589588
'nullable_type' => false,
590589
],
591590
],

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)