Skip to content

Commit 52f2e80

Browse files
committed
PHP 8.0 | Tokenizer/PHP: bugfix for union types using namespace operator
Type declarations can use namespace relative Types, i.e. `namespace\Sub\Name`. However, in that case, the `T_BITWISE_OR` token was incorrectly not converted to `T_TYPE_UNION`. Includes unit tests. Includes additional tests with all other "namespaced identifier name" types to safeguard this for the PHPCS 4.x change to the PHP 8.0 tokenization of identifier names.
1 parent 342dbf2 commit 52f2e80

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Tokenizers/PHP.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2583,6 +2583,7 @@ protected function processAdditional()
25832583
T_STATIC => T_STATIC,
25842584
T_FALSE => T_FALSE,
25852585
T_NULL => T_NULL,
2586+
T_NAMESPACE => T_NAMESPACE,
25862587
T_NS_SEPARATOR => T_NS_SEPARATOR,
25872588
];
25882589

tests/Core/Tokenizer/BitwiseOrTest.inc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ class TypeUnion
2424
/* testTypeUnionPropertyMulti3 */
2525
| null $arrayOrFalse;
2626

27+
/* testTypeUnionPropertyNamespaceRelative */
28+
public namespace\Sub\NameA|namespace\Sub\NameB $namespaceRelative;
29+
30+
/* testTypeUnionPropertyPartiallyQualified */
31+
public Partially\Qualified\NameA|Partially\Qualified\NameB $partiallyQual;
32+
33+
/* testTypeUnionPropertyFullyQualified */
34+
public \Fully\Qualified\NameA|\Fully\Qualified\NameB $fullyQual;
35+
2736
public function paramTypes(
2837
/* testTypeUnionParam1 */
2938
int|float $paramA /* testBitwiseOrParamDefaultValue */ = CONSTANT_A | CONSTANT_B,
@@ -35,6 +44,15 @@ class TypeUnion
3544
return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
3645
}
3746

47+
public function identifierNames(
48+
/* testTypeUnionParamNamespaceRelative */
49+
namespace\Sub\NameA|namespace\Sub\NameB $paramA,
50+
/* testTypeUnionParamPartiallyQualified */
51+
Partially\Qualified\NameA|Partially\Qualified\NameB $paramB,
52+
/* testTypeUnionParamFullyQualified */
53+
\Fully\Qualified\NameA|\Fully\Qualified\NameB $paramC,
54+
) {}
55+
3856
/* testTypeUnionReturnType */
3957
public function returnType() : int|false {}
4058

@@ -43,6 +61,15 @@ class TypeUnion
4361

4462
/* testTypeUnionAbstractMethodReturnType1 */
4563
abstract public function abstractMethod(): object|array /* testTypeUnionAbstractMethodReturnType2 */ |false;
64+
65+
/* testTypeUnionReturnTypeNamespaceRelative */
66+
public function identifierNamesReturnRelative() : namespace\Sub\NameA|namespace\Sub\NameB {}
67+
68+
/* testTypeUnionReturnPartiallyQualified */
69+
public function identifierNamesReturnPQ() : Partially\Qualified\NameA|Partially\Qualified\NameB {}
70+
71+
/* testTypeUnionReturnFullyQualified */
72+
public function identifierNamesReturnFQ() : \Fully\Qualified\NameA|\Fully\Qualified\NameB {}
4673
}
4774

4875
/* testTypeUnionClosureParamIllegalNullable */

tests/Core/Tokenizer/BitwiseOrTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,22 @@ public function dataTypeUnion()
102102
['/* testTypeUnionPropertyMulti1 */'],
103103
['/* testTypeUnionPropertyMulti2 */'],
104104
['/* testTypeUnionPropertyMulti3 */'],
105+
['/* testTypeUnionPropertyNamespaceRelative */'],
106+
['/* testTypeUnionPropertyPartiallyQualified */'],
107+
['/* testTypeUnionPropertyFullyQualified */'],
105108
['/* testTypeUnionParam1 */'],
106109
['/* testTypeUnionParam2 */'],
107110
['/* testTypeUnionParam3 */'],
111+
['/* testTypeUnionParamNamespaceRelative */'],
112+
['/* testTypeUnionParamPartiallyQualified */'],
113+
['/* testTypeUnionParamFullyQualified */'],
108114
['/* testTypeUnionReturnType */'],
109115
['/* testTypeUnionConstructorPropertyPromotion */'],
110116
['/* testTypeUnionAbstractMethodReturnType1 */'],
111117
['/* testTypeUnionAbstractMethodReturnType2 */'],
118+
['/* testTypeUnionReturnTypeNamespaceRelative */'],
119+
['/* testTypeUnionReturnPartiallyQualified */'],
120+
['/* testTypeUnionReturnFullyQualified */'],
112121
['/* testTypeUnionClosureParamIllegalNullable */'],
113122
['/* testTypeUnionWithReference */'],
114123
['/* testTypeUnionWithSpreadOperator */'],

0 commit comments

Comments
 (0)