Skip to content

Commit

Permalink
PhpdocTo Property/Return/Param Fixer - allow fixing mixed on PHP >= 8
Browse files Browse the repository at this point in the history
  • Loading branch information
MortalFlesh committed Apr 6, 2022
1 parent aa805a8 commit 9d3b094
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 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 @@ -29,7 +29,6 @@ final class PhpdocToPropertyTypeFixer extends AbstractPhpdocToTypeDeclarationFix
* @var array<string, true>
*/
private array $skippedTypes = [
'mixed' => true,
'resource' => true,
'null' => true,
];
Expand Down
1 change: 0 additions & 1 deletion 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
39 changes: 31 additions & 8 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,26 @@ final class PhpdocToParamTypeFixerTest extends AbstractFixerTestCase
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?int $versionSpecificFix = null, array $config = null): void
{
public function testFix(
string $expected,
?string $input = null,
?int $availableAboveVersion = null,
array $config = [],
?int $skipFromVersion = null
): void {
if (null !== $skipFromVersion && \PHP_VERSION_ID >= $skipFromVersion) {
static::markTestSkipped(sprintf('Only available up to version %d', $skipFromVersion));
}

if (
null !== $input
&& (null !== $versionSpecificFix && \PHP_VERSION_ID < $versionSpecificFix)
&& (null !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
) {
$expected = $input;
$input = null;
}

if (null !== $config) {
$this->fixer->configure($config);
}

$this->fixer->configure($config);
$this->doTest($expected, $input);
}

Expand Down Expand Up @@ -175,12 +181,29 @@ function my_foo(string $bar, int $baz, float $tab) {}',
*/
function my_foo($bar, $baz, $tab) {}',
],
'non-root class with mixed type of param' => [
'non-root class with mixed type of param for php < 8' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
null,
null,
[],
80000,
],
'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) {}',
80000,
],
'non-root namespaced class' => [
'<?php /** @param My\Bar $foo */ function my_foo(My\Bar $foo) {}',
Expand Down
28 changes: 25 additions & 3 deletions tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $config = []): void
{
if (null !== $input && \PHP_VERSION_ID < 70400) {
public function testFix(
string $expected,
?string $input = null,
?int $availableAboveVersion = null,
array $config = [],
?int $skipFromVersion = null
): void {
if (null !== $skipFromVersion && \PHP_VERSION_ID >= $skipFromVersion) {
static::markTestSkipped(sprintf('Only available up to version %d', $skipFromVersion));
}

if (
null !== $input
&& (null !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
) {
$expected = $input;
$input = null;
}
Expand Down Expand Up @@ -116,6 +128,7 @@ class Foo {
'do not fix scalar types when configured as such' => [
'<?php class Foo { /** @var int */ private $foo; }',
null,
null,
['scalar_types' => false],
],
'array native type' => [
Expand All @@ -138,6 +151,15 @@ class Foo {
],
'skip mixed special type' => [
'<?php class Foo { /** @var mixed */ private $foo; }',
null,
null,
[],
80000,
],
'fix mixed special type' => [
'<?php class Foo { /** @var mixed */ private mixed $foo; }',
'<?php class Foo { /** @var mixed */ private $foo; }',
80000,
],
'null alone cannot be a property type' => [
'<?php class Foo { /** @var null */ private $foo; }',
Expand Down
36 changes: 33 additions & 3 deletions tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ final class PhpdocToReturnTypeFixerTest extends AbstractFixerTestCase
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?int $versionSpecificFix = null, array $config = []): void
{
public function testFix(
string $expected,
?string $input = null,
?int $availableAboveVersion = null,
array $config = [],
?int $skipFromVersion = null
): void {
if (null !== $skipFromVersion && \PHP_VERSION_ID >= $skipFromVersion) {
static::markTestSkipped(sprintf('Only available up to version %d', $skipFromVersion));
}

if (
null !== $input
&& (null !== $versionSpecificFix && \PHP_VERSION_ID < $versionSpecificFix)
&& (null !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
) {
$expected = $input;
$input = null;
Expand Down Expand Up @@ -330,6 +339,27 @@ function bar() {}
function bar() {}
',
],
'skip mixed type' => [
'<?php
/** @return mixed */
function bar() {}
',
null,
null,
[],
80000,
],
'fix skip mixed type' => [
'<?php
/** @return mixed */
function bar(): mixed {}
',
'<?php
/** @return mixed */
function bar() {}
',
80000,
],
];
}

Expand Down

0 comments on commit 9d3b094

Please sign in to comment.