Skip to content

Commit

Permalink
PHP 8.0 | Tokenizer/PHP: bugfix for union types using namespace operator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Apr 19, 2021
1 parent 342dbf2 commit 52f2e80
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2583,6 +2583,7 @@ protected function processAdditional()
T_STATIC => T_STATIC,
T_FALSE => T_FALSE,
T_NULL => T_NULL,
T_NAMESPACE => T_NAMESPACE,
T_NS_SEPARATOR => T_NS_SEPARATOR,
];

Expand Down
27 changes: 27 additions & 0 deletions tests/Core/Tokenizer/BitwiseOrTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ class TypeUnion
/* testTypeUnionPropertyMulti3 */
| null $arrayOrFalse;

/* testTypeUnionPropertyNamespaceRelative */
public namespace\Sub\NameA|namespace\Sub\NameB $namespaceRelative;

/* testTypeUnionPropertyPartiallyQualified */
public Partially\Qualified\NameA|Partially\Qualified\NameB $partiallyQual;

/* testTypeUnionPropertyFullyQualified */
public \Fully\Qualified\NameA|\Fully\Qualified\NameB $fullyQual;

public function paramTypes(
/* testTypeUnionParam1 */
int|float $paramA /* testBitwiseOrParamDefaultValue */ = CONSTANT_A | CONSTANT_B,
Expand All @@ -35,6 +44,15 @@ class TypeUnion
return (($a1 ^ $b1) |($a2 ^ $b2)) + $c;
}

public function identifierNames(
/* testTypeUnionParamNamespaceRelative */
namespace\Sub\NameA|namespace\Sub\NameB $paramA,
/* testTypeUnionParamPartiallyQualified */
Partially\Qualified\NameA|Partially\Qualified\NameB $paramB,
/* testTypeUnionParamFullyQualified */
\Fully\Qualified\NameA|\Fully\Qualified\NameB $paramC,
) {}

/* testTypeUnionReturnType */
public function returnType() : int|false {}

Expand All @@ -43,6 +61,15 @@ class TypeUnion

/* testTypeUnionAbstractMethodReturnType1 */
abstract public function abstractMethod(): object|array /* testTypeUnionAbstractMethodReturnType2 */ |false;

/* testTypeUnionReturnTypeNamespaceRelative */
public function identifierNamesReturnRelative() : namespace\Sub\NameA|namespace\Sub\NameB {}

/* testTypeUnionReturnPartiallyQualified */
public function identifierNamesReturnPQ() : Partially\Qualified\NameA|Partially\Qualified\NameB {}

/* testTypeUnionReturnFullyQualified */
public function identifierNamesReturnFQ() : \Fully\Qualified\NameA|\Fully\Qualified\NameB {}
}

/* testTypeUnionClosureParamIllegalNullable */
Expand Down
9 changes: 9 additions & 0 deletions tests/Core/Tokenizer/BitwiseOrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,22 @@ public function dataTypeUnion()
['/* testTypeUnionPropertyMulti1 */'],
['/* testTypeUnionPropertyMulti2 */'],
['/* testTypeUnionPropertyMulti3 */'],
['/* testTypeUnionPropertyNamespaceRelative */'],
['/* testTypeUnionPropertyPartiallyQualified */'],
['/* testTypeUnionPropertyFullyQualified */'],
['/* testTypeUnionParam1 */'],
['/* testTypeUnionParam2 */'],
['/* testTypeUnionParam3 */'],
['/* testTypeUnionParamNamespaceRelative */'],
['/* testTypeUnionParamPartiallyQualified */'],
['/* testTypeUnionParamFullyQualified */'],
['/* testTypeUnionReturnType */'],
['/* testTypeUnionConstructorPropertyPromotion */'],
['/* testTypeUnionAbstractMethodReturnType1 */'],
['/* testTypeUnionAbstractMethodReturnType2 */'],
['/* testTypeUnionReturnTypeNamespaceRelative */'],
['/* testTypeUnionReturnPartiallyQualified */'],
['/* testTypeUnionReturnFullyQualified */'],
['/* testTypeUnionClosureParamIllegalNullable */'],
['/* testTypeUnionWithReference */'],
['/* testTypeUnionWithSpreadOperator */'],
Expand Down

0 comments on commit 52f2e80

Please sign in to comment.