Skip to content

Commit

Permalink
Change the way how PHP pre-8 and post-8 are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Oct 31, 2023
1 parent 1818ef2 commit 4305639
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 113 deletions.
4 changes: 0 additions & 4 deletions src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,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
89 changes: 51 additions & 38 deletions tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,18 @@ final class PhpdocToParamTypeFixerTest extends AbstractFixerTestCase
*
* @dataProvider provideFixCases
*/
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));
}

public function testFix(string $expected, ?string $input = null, ?int $versionSpecificFix = null, array $config = []): void
{
if (
null !== $input
&& (null !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
&& (null !== $versionSpecificFix && \PHP_VERSION_ID < $versionSpecificFix)
) {
$expected = $input;
$input = null;
}

$this->fixer->configure($config);

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

Expand Down Expand Up @@ -198,32 +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 for php < 8' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
null,
null,
[],
80000,
];

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) {}',
80000,
];

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 @@ -531,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) {}',
];
}
}
71 changes: 37 additions & 34 deletions tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,8 @@ final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
*
* @dataProvider provideFixCases
*/
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;
}

public function testFix(string $expected, ?string $input = null, array $config = []): void
{
$this->fixer->configure($config);
$this->doTest($expected, $input);
}
Expand Down Expand Up @@ -147,7 +130,6 @@ class Foo {
yield 'do not fix scalar types when configured as such' => [
'<?php class Foo { /** @var int */ private $foo; }',
null,
null,
['scalar_types' => false],
];

Expand All @@ -174,20 +156,6 @@ class Foo {
'<?php class Foo { /** @var resource */ private $foo; }',
];

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

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

yield 'null alone cannot be a property type' => [
'<?php class Foo { /** @var null */ private $foo; }',
];
Expand Down Expand Up @@ -550,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
42 changes: 5 additions & 37 deletions tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,11 @@ final class PhpdocToReturnTypeFixerTest extends AbstractFixerTestCase
*
* @dataProvider provideFixCases
*/
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));
}

public function testFix(string $expected, ?string $input = null, ?int $versionSpecificFix = null, array $config = []): void
{
if (
null !== $input
&& (null !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
&& (null !== $versionSpecificFix && \PHP_VERSION_ID < $versionSpecificFix)
) {
$expected = $input;
$input = null;
Expand Down Expand Up @@ -235,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 @@ -263,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 Expand Up @@ -399,29 +390,6 @@ function bar() {}
',
];

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

yield 'fix mixed type' => [
'<?php
/** @return mixed */
function bar(): mixed {}
',
'<?php
/** @return mixed */
function bar() {}
',
80000,
];

yield 'arrow function' => [
'<?php /** @return int */ fn(): int => 1;',
'<?php /** @return int */ fn() => 1;',
Expand Down

0 comments on commit 4305639

Please sign in to comment.