Skip to content

Commit

Permalink
feat: phpDoc to property/return/param Fixer - allow fixing mixed on P…
Browse files Browse the repository at this point in the history
…HP >= 8 (#6356)

Co-authored-by: Greg Korba <greg@codito.dev>
  • Loading branch information
MortalFlesh and Wirone committed Oct 31, 2023
1 parent 5f102b4 commit 877bfb6
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/Fixer/FunctionNotation/PhpdocToParamTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ final class PhpdocToParamTypeFixer extends AbstractPhpdocToTypeDeclarationFixer
* @var array<string, true>
*/
private const SKIPPED_TYPES = [
'mixed' => true,
'resource' => true,
'static' => true,
'void' => true,
Expand Down
1 change: 0 additions & 1 deletion src/Fixer/FunctionNotation/PhpdocToPropertyTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ final class PhpdocToPropertyTypeFixer extends AbstractPhpdocToTypeDeclarationFix
* @var array<string, true>
*/
private array $skippedTypes = [
'mixed' => true,
'resource' => true,
'null' => true,
];
Expand Down
5 changes: 0 additions & 5 deletions src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ final class PhpdocToReturnTypeFixer extends AbstractPhpdocToTypeDeclarationFixer
* @var array<string, true>
*/
private array $skippedTypes = [
'mixed' => true,
'resource' => true,
'null' => true,
];
Expand Down Expand Up @@ -120,10 +119,6 @@ protected function isSkippedType(string $type): bool

protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
if (\PHP_VERSION_ID >= 8_00_00) {
unset($this->skippedTypes['mixed']);
}

for ($index = $tokens->count() - 1; 0 < $index; --$index) {
if (!$tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
continue;
Expand Down
55 changes: 47 additions & 8 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,6 @@ function my_foo(string $bar, int $baz, float $tab) {}',
function my_foo($bar, $baz, $tab) {}',
];

yield 'non-root class with mixed type of param' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
];

yield 'non-root namespaced class' => [
'<?php /** @param My\Bar $foo */ function my_foo(My\Bar $foo) {}',
'<?php /** @param My\Bar $foo */ function my_foo($foo) {}',
Expand Down Expand Up @@ -505,4 +497,51 @@ function bar($x) {}
',
];
}

/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, string $input = null): void
{
$this->doTest($expected, $input);
}

public static function provideFixPre80Cases(): iterable
{
yield 'skip mixed type of param' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
];
}

/**
* @dataProvider provideFixPhp80Cases
*
* @requires PHP 8.0
*/
public function testFixPhp80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public static function provideFixPhp80Cases(): iterable
{
yield 'non-root class with mixed type of param for php >= 8' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo(mixed $bar) {}',
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
];
}
}
39 changes: 35 additions & 4 deletions tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,6 @@ class Foo {
'<?php class Foo { /** @var resource */ private $foo; }',
];

yield 'skip mixed special type' => [
'<?php class Foo { /** @var mixed */ private $foo; }',
];

yield 'null alone cannot be a property type' => [
'<?php class Foo { /** @var null */ private $foo; }',
];
Expand Down Expand Up @@ -522,6 +518,41 @@ public abstract function getFoo();
];
}

/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, string $input = null): void
{
$this->doTest($expected, $input);
}

public static function provideFixPre80Cases(): iterable
{
yield 'skip mixed type' => [
'<?php class Foo { /** @var mixed */ private $foo; }',
];
}

/**
* @dataProvider provideFixPhp80Cases
*
* @requires PHP 8.0
*/
public function testFixPhp80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public static function provideFixPhp80Cases(): iterable
{
yield 'fix mixed type' => [
'<?php class Foo { /** @var mixed */ private mixed $foo; }',
'<?php class Foo { /** @var mixed */ private $foo; }',
];
}

/**
* @dataProvider provideFix81Cases
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class Foo {
'<?php /** @return null */ function my_foo() {}',
];

yield 'skip mixed types' => [
yield 'skip union types' => [
'<?php /** @return Foo|Bar */ function my_foo() {}',
];

Expand Down Expand Up @@ -254,7 +254,7 @@ class Foo {
'<?php /** @return string|string[] */ function my_foo() {}',
];

yield 'skip mixed nullable types' => [
yield 'skip nullable union types' => [
'<?php /** @return null|Foo|Bar */ function my_foo() {}',
];

Expand Down

0 comments on commit 877bfb6

Please sign in to comment.