Skip to content

Commit

Permalink
minor #5254 PHP8 - mixed type support (SpacePossum)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.15 branch.

Discussion
----------

PHP8 - mixed type support

On master check `phpdoc_to_param_type`  as well.

`PhpdocToReturnTypeFixer` could support:
```
            'skip mixed types' => [
                '<?php /** @return Foo|Bar */ function my_foo(): mixed {}',
                '<?php /** @return Foo|Bar */ function my_foo() {}',
            ],
```
through configuration if we want to

Commits
-------

1008bc1 PHP8 - mixed type support
  • Loading branch information
SpacePossum committed Nov 16, 2020
2 parents d0b82e6 + 1008bc1 commit 8cefe7e
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 52 deletions.
24 changes: 13 additions & 11 deletions src/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
/**
* https://secure.php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.
*
* self PHP 5.0.0
* array PHP 5.1.0
* callable PHP 5.4.0
* bool PHP 7.0.0
* float PHP 7.0.0
* int PHP 7.0.0
* string PHP 7.0.0
* iterable PHP 7.1.0
* void PHP 7.1.0
* object PHP 7.2.0
* static PHP 8.0.0 (return type only)
* self PHP 5.0
* array PHP 5.1
* callable PHP 5.4
* bool PHP 7.0
* float PHP 7.0
* int PHP 7.0
* string PHP 7.0
* iterable PHP 7.1
* void PHP 7.1
* object PHP 7.2
* static PHP 8.0 (return type only)
* mixed PHP 8.0
*
* @var array<string, true>
*/
Expand Down Expand Up @@ -89,6 +90,7 @@ public function __construct()

if (\PHP_VERSION_ID >= 80000) {
$this->hints = array_merge($this->hints, ['static' => true]);
$this->hints = array_merge($this->hints, ['mixed' => true]);
}

$this->functionsAnalyzer = new FunctionsAnalyzer();
Expand Down
4 changes: 4 additions & 0 deletions src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ protected function createConfigurationDefinition()
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens)
{
if (\PHP_VERSION_ID >= 80000) {
unset($this->skippedTypes['mixed']);
}

for ($index = $tokens->count() - 1; 0 < $index; --$index) {
if (
!$tokens[$index]->isGivenKind(T_FUNCTION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,19 @@ public function testFix80($expected, $input)

public function provideFix80Cases()
{
return [
[
'<?php class T { public function Foo(object $A): static {}}',
'<?php class T { public function Foo(object $A): StatiC {}}',
],
[
'<?php class T { public function Foo(object $A): ?static {}}',
'<?php class T { public function Foo(object $A): ?StatiC {}}',
],
yield [
'<?php class T { public function Foo(object $A): static {}}',
'<?php class T { public function Foo(object $A): StatiC {}}',
];

yield [
'<?php class T { public function Foo(object $A): ?static {}}',
'<?php class T { public function Foo(object $A): ?StatiC {}}',
];

yield [
'<?php class T { public function Foo(mixed $A): mixed {}}',
'<?php class T { public function Foo(Mixed $A): MIXED {}}',
];
}
}
20 changes: 20 additions & 0 deletions tests/Fixer/FunctionNotation/FunctionTypehintSpaceFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,24 @@ public function provideFix74Cases()
],
];
}

/**
* @param string $expected
* @param string $input
*
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80($expected, $input)
{
$this->doTest($expected, $input);
}

public function provideFix80Cases()
{
yield [
'<?php function foo(mixed $a){}',
'<?php function foo(mixed$a){}',
];
}
}
79 changes: 47 additions & 32 deletions tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ class Foo {
'skip resource special type' => [
'<?php /** @return resource */ function my_foo() {}',
],
'skip mixed special type' => [
'<?php /** @return mixed */ function my_foo() {}',
],
'null alone cannot be a return type' => [
'<?php /** @return null */ function my_foo() {}',
],
Expand Down Expand Up @@ -278,6 +275,10 @@ class Foo {
}
',
];

yield 'skip mixed special type' => [
'<?php /** @return mixed */ function my_foo() {}',
];
}
}

Expand Down Expand Up @@ -318,35 +319,49 @@ public function testFixPhp80($expected, $input = null)

public function provideFixPhp80Cases()
{
return [
'static' => [
'<?php
final class Foo {
/** @return static */
public function something(): static {}
}
',
'<?php
final class Foo {
/** @return static */
public function something() {}
}
',
],
'nullable static' => [
'<?php
final class Foo {
/** @return null|static */
public function something(): ?static {}
}
',
'<?php
final class Foo {
/** @return null|static */
public function something() {}
}
',
],
yield 'static' => [
'<?php
final class Foo {
/** @return static */
public function something(): static {}
}
',
'<?php
final class Foo {
/** @return static */
public function something() {}
}
',
];

yield 'nullable static' => [
'<?php
final class Foo {
/** @return null|static */
public function something(): ?static {}
}
',
'<?php
final class Foo {
/** @return null|static */
public function something() {}
}
',
];

yield 'mixed' => [
'<?php
final class Foo {
/** @return mixed */
public function something(): mixed {}
}
',
'<?php
final class Foo {
/** @return mixed */
public function something() {}
}
',
];
}
}
25 changes: 25 additions & 0 deletions tests/Fixer/FunctionNotation/ReturnTypeDeclarationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,29 @@ public function provideFixWithSpaceBeforeNonePhp74Cases()
],
];
}

/**
* @param string $expected
* @param string $input
*
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80($expected, $input)
{
$this->doTest($expected, $input);
}

public function provideFix80Cases()
{
yield [
'<?php function foo(): mixed{}',
'<?php function foo() : mixed{}',
];

yield [
'<?php class A { public function foo(): static{}}',
'<?php class A { public function foo() :static{}}',
];
}
}
20 changes: 20 additions & 0 deletions tests/Fixer/Whitespace/NoSpacesInsideParenthesisFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,24 @@ function foo( $bar, $baz )
],
];
}

/**
* @param string $expected
* @param string $input
*
* @dataProvider provideFix80Cases
* @requires PHP 8.0
*/
public function testFix80($expected, $input)
{
$this->doTest($expected, $input);
}

public function provideFix80Cases()
{
yield [
'<?php function foo(mixed $a){}',
'<?php function foo( mixed $a ){}',
];
}
}

0 comments on commit 8cefe7e

Please sign in to comment.