Skip to content

Commit 65290db

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.
1 parent c9b6415 commit 65290db

File tree

5 files changed

+13
-0
lines changed

5 files changed

+13
-0
lines changed

src/Tokenizers/PHP.php

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

2920+
if ($suspectedType === 'property or parameter') {
2921+
unset($allowed[\T_STATIC]);
2922+
}
2923+
29202924
$typeTokenCount = 0;
29212925
$typeOperators = [$i];
29222926
$confirmed = false;
@@ -2949,6 +2953,7 @@ protected function processAdditional()
29492953
if ($suspectedType === 'property or parameter'
29502954
&& (isset(Util\Tokens::$scopeModifiers[$this->tokens[$x]['code']]) === true
29512955
|| $this->tokens[$x]['code'] === T_VAR
2956+
|| $this->tokens[$x]['code'] === T_STATIC
29522957
|| $this->tokens[$x]['code'] === T_READONLY)
29532958
) {
29542959
// This will also confirm constructor property promotion parameters, but that's fine.

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)