Skip to content

Commit 4eb94f8

Browse files
committed
Merge branch 'feature/php8-add-tests-for-type-mixed' of https://github.com/jrfnl/PHP_CodeSniffer
2 parents 285f465 + 6d6b0eb commit 4eb94f8

6 files changed

+133
-0
lines changed

tests/Core/File/GetMemberPropertiesTest.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,12 @@ function_call( 'param', new class {
179179
/* testNestedMethodParam 2 */
180180
public function __construct( $open, $post_id ) {}
181181
}, 10, 2 );
182+
183+
class PHP8Mixed {
184+
/* testPHP8MixedTypeHint */
185+
public static miXed $mixed;
186+
187+
/* testPHP8MixedTypeHintNullable */
188+
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
189+
private ?mixed $nullableMixed;
190+
}

tests/Core/File/GetMemberPropertiesTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,26 @@ public function dataGetMemberProperties()
459459
'nullable_type' => false,
460460
],
461461
],
462+
[
463+
'/* testPHP8MixedTypeHint */',
464+
[
465+
'scope' => 'public',
466+
'scope_specified' => true,
467+
'is_static' => true,
468+
'type' => 'miXed',
469+
'nullable_type' => false,
470+
],
471+
],
472+
[
473+
'/* testPHP8MixedTypeHintNullable */',
474+
[
475+
'scope' => 'private',
476+
'scope_specified' => true,
477+
'is_static' => false,
478+
'type' => '?mixed',
479+
'nullable_type' => true,
480+
],
481+
],
462482
];
463483

464484
}//end dataGetMemberProperties()

tests/Core/File/GetMethodParametersTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@ function myFunction($a = 10 & 20) {}
3131

3232
/* testArrowFunction */
3333
fn(int $a, ...$b) => $b;
34+
35+
/* testPHP8MixedTypeHint */
36+
function mixedTypeHint(mixed &...$var1) {}
37+
38+
/* testPHP8MixedTypeHintNullable */
39+
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
40+
function mixedTypeHintNullable(?Mixed $var1) {}

tests/Core/File/GetMethodParametersTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,50 @@ public function testArrowFunction()
274274
}//end testArrowFunction()
275275

276276

277+
/**
278+
* Verify recognition of PHP8 mixed type declaration.
279+
*
280+
* @return void
281+
*/
282+
public function testPHP8MixedTypeHint()
283+
{
284+
$expected = [];
285+
$expected[0] = [
286+
'name' => '$var1',
287+
'content' => 'mixed &...$var1',
288+
'pass_by_reference' => true,
289+
'variable_length' => true,
290+
'type_hint' => 'mixed',
291+
'nullable_type' => false,
292+
];
293+
294+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
295+
296+
}//end testPHP8MixedTypeHint()
297+
298+
299+
/**
300+
* Verify recognition of PHP8 mixed type declaration with nullability.
301+
*
302+
* @return void
303+
*/
304+
public function testPHP8MixedTypeHintNullable()
305+
{
306+
$expected = [];
307+
$expected[0] = [
308+
'name' => '$var1',
309+
'content' => '?Mixed $var1',
310+
'pass_by_reference' => false,
311+
'variable_length' => false,
312+
'type_hint' => '?Mixed',
313+
'nullable_type' => true,
314+
];
315+
316+
$this->getMethodParametersTestHelper('/* '.__FUNCTION__.' */', $expected);
317+
318+
}//end testPHP8MixedTypeHintNullable()
319+
320+
277321
/**
278322
* Test helper.
279323
*

tests/Core/File/GetMethodPropertiesTest.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,10 @@ class ReturnMe {
7373
return $this;
7474
}
7575
}
76+
77+
/* testPHP8MixedTypeHint */
78+
function mixedTypeHint() :mixed {}
79+
80+
/* testPHP8MixedTypeHintNullable */
81+
// Intentional fatal error - nullability is not allowed with mixed, but that's not the concern of the method.
82+
function mixedTypeHintNullable(): ?mixed {}

tests/Core/File/GetMethodPropertiesTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,52 @@ public function testReturnTypeStatic()
406406
}//end testReturnTypeStatic()
407407

408408

409+
/**
410+
* Test a function with return type "mixed".
411+
*
412+
* @return void
413+
*/
414+
public function testPHP8MixedTypeHint()
415+
{
416+
$expected = [
417+
'scope' => 'public',
418+
'scope_specified' => false,
419+
'return_type' => 'mixed',
420+
'nullable_return_type' => false,
421+
'is_abstract' => false,
422+
'is_final' => false,
423+
'is_static' => false,
424+
'has_body' => true,
425+
];
426+
427+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
428+
429+
}//end testPHP8MixedTypeHint()
430+
431+
432+
/**
433+
* Test a function with return type "mixed" and nullability.
434+
*
435+
* @return void
436+
*/
437+
public function testPHP8MixedTypeHintNullable()
438+
{
439+
$expected = [
440+
'scope' => 'public',
441+
'scope_specified' => false,
442+
'return_type' => '?mixed',
443+
'nullable_return_type' => true,
444+
'is_abstract' => false,
445+
'is_final' => false,
446+
'is_static' => false,
447+
'has_body' => true,
448+
];
449+
450+
$this->getMethodPropertiesTestHelper('/* '.__FUNCTION__.' */', $expected);
451+
452+
}//end testPHP8MixedTypeHintNullable()
453+
454+
409455
/**
410456
* Test helper.
411457
*

0 commit comments

Comments
 (0)