Skip to content

Commit

Permalink
PHP 8.0 | Tokenizer/PHP: bugfix for union type operator tokenization
Browse files Browse the repository at this point in the history
In `PHP::processAdditional()`, in the part which retokenizes `T_BITWISE_OR` tokens to `T_TYPE_UNION`, the code already took reference operators and spread operators between the type declaration and the variable into account, but would also increment the current position in the loop, while this is already done in the `for()`. My bad.

Fixed now.

Includes additional unit tests for all relevant functions within the chain, as well as for the sniff for which the issue was reported.

Fixes 3267
  • Loading branch information
jrfnl committed Mar 16, 2021
1 parent 46715ed commit c7f7013
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ $fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

function issue3267(string|int ...$values) {}
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,5 @@ $fn = fn(array &$one) => 1;
$fn = fn(array & $one) => 1;

$fn = static fn(DateTime $a, DateTime $b): int => -($a->getTimestamp() <=> $b->getTimestamp());

function issue3267(string|int ...$values) {}
1 change: 0 additions & 1 deletion src/Tokenizers/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -2540,7 +2540,6 @@ protected function processAdditional()
|| $this->tokens[$x]['code'] === T_ELLIPSIS)
) {
// Skip past reference and variadic indicators for parameter types.
++$x;
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Core/File/GetMethodParametersTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function namespaceOperatorTypeHint(?namespace\Name $var1) {}
/* testPHP8UnionTypesSimple */
function unionTypeSimple(int|float $number, self|parent &...$obj) {}

/* testPHP8UnionTypesWithSpreadOperatorAndReference */
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB) {}

/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;

Expand Down
30 changes: 30 additions & 0 deletions tests/Core/File/GetMethodParametersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,36 @@ public function testPHP8UnionTypesSimple()
}//end testPHP8UnionTypesSimple()


/**
* Verify recognition of PHP8 union type declaration when the variable has either a spread operator or a reference.
*
* @return void
*/
public function testPHP8UnionTypesWithSpreadOperatorAndReference()
{
$expected = [];
$expected[0] = [
'name' => '$paramA',
'content' => 'float|null &$paramA',
'pass_by_reference' => true,
'variable_length' => false,
'type_hint' => 'float|null',
'nullable_type' => false,
];
$expected[1] = [
'name' => '$paramB',
'content' => 'string|int ...$paramB',
'pass_by_reference' => false,
'variable_length' => true,
'type_hint' => 'string|int',
'nullable_type' => false,
];

$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);

}//end testPHP8UnionTypesWithSpreadOperatorAndReference()


/**
* Verify recognition of PHP8 union type declaration with a bitwise or in the default value.
*
Expand Down
7 changes: 7 additions & 0 deletions tests/Core/Tokenizer/BitwiseOrTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class TypeUnion
/* testTypeUnionClosureParamIllegalNullable */
$closureWithParamType = function (?string|null $string) {};

function globalFunctionWithSpreadAndReference(
/* testTypeUnionWithReference */
float|null &$paramA,
/* testTypeUnionWithSpreadOperator */
string|int ...$paramB
) {}

/* testBitwiseOrClosureParamDefault */
$closureWithReturnType = function ($string = NONSENSE | FAKE)/* testTypeUnionClosureReturn */ : \Package\MyA|PackageB {};

Expand Down
2 changes: 2 additions & 0 deletions tests/Core/Tokenizer/BitwiseOrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public function dataTypeUnion()
['/* testTypeUnionAbstractMethodReturnType1 */'],
['/* testTypeUnionAbstractMethodReturnType2 */'],
['/* testTypeUnionClosureParamIllegalNullable */'],
['/* testTypeUnionWithReference */'],
['/* testTypeUnionWithSpreadOperator */'],
['/* testTypeUnionClosureReturn */'],
['/* testTypeUnionArrowParam */'],
['/* testTypeUnionArrowReturnType */'],
Expand Down

0 comments on commit c7f7013

Please sign in to comment.