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 Aug 19, 2022
1 parent 17e5591 commit 67c1ff1
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 23 deletions.
3 changes: 1 addition & 2 deletions 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 All @@ -51,7 +50,7 @@ final class PhpdocToParamTypeFixer extends AbstractPhpdocToTypeDeclarationFixer
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'EXPERIMENTAL: Takes `@param` annotations of non-mixed types and adjusts accordingly the function signature. Requires PHP >= 7.0.',
'EXPERIMENTAL: Takes `@param` annotations types and adjusts accordingly the function signature.',
[
new CodeSample(
'<?php
Expand Down
3 changes: 1 addition & 2 deletions 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 All @@ -40,7 +39,7 @@ final class PhpdocToPropertyTypeFixer extends AbstractPhpdocToTypeDeclarationFix
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'EXPERIMENTAL: Takes `@var` annotation of non-mixed types and adjusts accordingly the property signature. Requires PHP >= 7.4.',
'EXPERIMENTAL: Takes `@var` annotation types and adjusts accordingly the property signature.',
[
new VersionSpecificCodeSample(
'<?php
Expand Down
7 changes: 1 addition & 6 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 All @@ -53,7 +52,7 @@ final class PhpdocToReturnTypeFixer extends AbstractPhpdocToTypeDeclarationFixer
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'EXPERIMENTAL: Takes `@return` annotation of non-mixed types and adjusts accordingly the function signature. Requires PHP >= 7.0.',
'EXPERIMENTAL: Takes `@return` annotation types and adjusts accordingly the function signature.',
[
new CodeSample(
'<?php
Expand Down Expand Up @@ -134,10 +133,6 @@ protected function isSkippedType(string $type): bool
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
if (\PHP_VERSION_ID >= 80000) {
unset($this->skippedTypes['mixed']);
}

for ($index = $tokens->count() - 1; 0 < $index; --$index) {
if (!$tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
continue;
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
31 changes: 29 additions & 2 deletions tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,25 @@ final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = 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 !== $availableAboveVersion && \PHP_VERSION_ID < $availableAboveVersion)
) {
$expected = $input;
$input = null;
}

$this->fixer->configure($config);
$this->doTest($expected, $input);
}
Expand Down Expand Up @@ -111,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 @@ -133,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 mixed type' => [
'<?php
/** @return mixed */
function bar(): mixed {}
',
'<?php
/** @return mixed */
function bar() {}
',
80000,
],
'arrow function' => [
'<?php /** @return int */ fn(): int => 1;',
'<?php /** @return int */ fn() => 1;',
Expand Down

0 comments on commit 67c1ff1

Please sign in to comment.