Skip to content

Commit bf8c4d2

Browse files
authored
Merge pull request #246 from PHPCSStandards/operators/istypeunion-additional-tests
Operators::isTypeUnion() + BCFile/FunctionDeclarations::get[Method]Parameters(): add some additional tests
2 parents 2622cbd + 104ec78 commit bf8c4d2

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Tests/BackCompat/BCFile/GetMethodParametersTest.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ function namespaceOperatorTypeHint(?namespace\Name $var1) {}
138138
/* testPHP8UnionTypesSimple */
139139
function unionTypeSimple(int|float $number, self|parent &...$obj) {}
140140

141+
/* testPHP8UnionTypesWithSpreadOperatorAndReference */
142+
function globalFunctionWithSpreadAndReference(float|null &$paramA, string|int ...$paramB ) {}
143+
141144
/* testPHP8UnionTypesSimpleWithBitwiseOrInDefault */
142145
$fn = fn(int|float $var = CONSTANT_A | CONSTANT_B) => $var;
143146

Tests/BackCompat/BCFile/GetMethodParametersTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,46 @@ public function testPHP8UnionTypesSimple()
13171317
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
13181318
}
13191319

1320+
/**
1321+
* Verify recognition of PHP8 union type declaration when the variable has either a spread operator or a reference.
1322+
*
1323+
* @return void
1324+
*/
1325+
public function testPHP8UnionTypesWithSpreadOperatorAndReference()
1326+
{
1327+
$expected = [];
1328+
$expected[0] = [
1329+
'token' => 9, // Offset from the T_FUNCTION token.
1330+
'name' => '$paramA',
1331+
'content' => 'float|null &$paramA',
1332+
'pass_by_reference' => true,
1333+
'reference_token' => 8, // Offset from the T_FUNCTION token.
1334+
'variable_length' => false,
1335+
'variadic_token' => false,
1336+
'type_hint' => 'float|null',
1337+
'type_hint_token' => 4, // Offset from the T_FUNCTION token.
1338+
'type_hint_end_token' => 6, // Offset from the T_FUNCTION token.
1339+
'nullable_type' => false,
1340+
'comma_token' => 10,
1341+
];
1342+
$expected[1] = [
1343+
'token' => 17, // Offset from the T_FUNCTION token.
1344+
'name' => '$paramB',
1345+
'content' => 'string|int ...$paramB',
1346+
'pass_by_reference' => false,
1347+
'reference_token' => false,
1348+
'variable_length' => true,
1349+
'variadic_token' => 16, // Offset from the T_FUNCTION token.
1350+
'type_hint' => 'string|int',
1351+
'type_hint_token' => 12, // Offset from the T_FUNCTION token.
1352+
'type_hint_end_token' => 14, // Offset from the T_FUNCTION token.
1353+
'nullable_type' => false,
1354+
'comma_token' => false,
1355+
];
1356+
1357+
$this->getMethodParametersTestHelper('/* ' . __FUNCTION__ . ' */', $expected);
1358+
}
1359+
13201360
/**
13211361
* Verify recognition of PHP8 union type declaration with a bitwise or in the default value.
13221362
*

Tests/Utils/Operators/IsTypeUnionTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ class TypeUnion
7878
/* testTypeUnionClosureParamIllegalNullable */
7979
$closureWithParamType = function (?string|null $string) {};
8080

81+
function globalFunctionWithSpreadAndReference(
82+
/* testTypeUnionWithReference */
83+
float|null &$paramB,
84+
/* testTypeUnionWithSpreadOperator */
85+
string|int ...$paramA
86+
) {}
87+
8188
/* testBitwiseOrClosureParamDefault */
8289
$closureWithReturnType = function ($string = NONSENSE | FAKE)/* testTypeUnionClosureReturn */ : \Package\MyA|PackageB {};
8390

Tests/Utils/Operators/IsTypeUnionTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function dataIsTypeUnion()
9999
'return-partially-qualified' => ['/* testTypeUnionReturnPartiallyQualified */'],
100100
'return-fully-qualified' => ['/* testTypeUnionReturnFullyQualified */'],
101101
'parameter-closure-with-nullable' => ['/* testTypeUnionClosureParamIllegalNullable */'],
102+
'parameter-with-reference' => ['/* testTypeUnionWithReference */'],
103+
'parameter-with-spread-operator' => ['/* testTypeUnionWithSpreadOperator */'],
102104
'return-closure' => ['/* testTypeUnionClosureReturn */'],
103105
'parameter-arrow' => ['/* testTypeUnionArrowParam */'],
104106
'return-arrow' => ['/* testTypeUnionArrowReturnType */'],

0 commit comments

Comments
 (0)