Skip to content

Commit

Permalink
[Downgradephp82] Add DowngradeStandaloneNullTrueFalseReturnTypeRector
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Dec 11, 2023
1 parent a08ccad commit c05f11e
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class DowngradeStandaloneNullTrueFalseReturnTypeRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeStandaloneNullTrueFalseReturnTypeRector\Fixture;

final class ReturnNull
{
public function run(): null
{
return null;
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp82\Rector\Class_\DowngradeStandaloneNullTrueFalseReturnTypeRector\Fixture;

final class ReturnNull
{
public function run(): mixed
{
return null;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(DowngradeStandaloneNullTrueFalseReturnTypeRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Rector\DowngradePhp82\Rector\FunctionLike;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://wiki.php.net/rfc/null-false-standalone-types
* @changelog https://wiki.php.net/rfc/true-type
* @see \Rector\Tests\DowngradePhp82\Rector\FunctionLike\DowngradeStandaloneNullTrueFalseReturnTypeRector\DowngradeStandaloneNullTrueFalseReturnTypeRectorTest
*/
final class DowngradeStandaloneNullTrueFalseReturnTypeRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FunctionLike::class];
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Downgrade standalone return null, true, or false',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(): null
{
return null;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(): mixed
{
return null;
}
}
CODE_SAMPLE
),
]
);
}

/**
* @param FunctionLike $node
*/
public function refactor(Node $node): ?Node
{
$returnType = $node->getReturnType();
if (! $returnType instanceof Node) {
return null;
}

$returnTypeName = $this->getName($returnType);
if (! in_array($returnTypeName, ['null', 'false', 'true'])) {
return null;
}


if (! $node instanceof ClassMethod) {
$node->returnType = new Identifier('mixed');
return $node;
}

// todo: verify parent
$node->returnType = new Identifier('mixed');
return null;
}
}

0 comments on commit c05f11e

Please sign in to comment.