[DowngradePhp70] Add DowngradeParentTypeDeclarationRector (#755)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2021-08-25 13:48:25 +07:00 committed by GitHub
parent 885ef768e5
commit f9ee33b4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 369 additions and 0 deletions

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\ClassMethod\DowngradeSelfTypeDeclarationRector;
use Rector\DowngradePhp70\Rector\Coalesce\DowngradeNullCoalesceRector;
use Rector\DowngradePhp70\Rector\Declare_\DowngradeStrictTypeDeclarationRector;
@ -31,4 +32,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
$services->set(DowngradeSessionStartArrayOptionsRector::class);
$services->set(SplitGroupedUseImportsRector::class);
$services->set(DowngradeGeneratedScalarTypesRector::class);
$services->set(DowngradeParentTypeDeclarationRector::class);
};

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeParentTypeDeclarationRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP 7.0
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
class ParentClass
{
}
class AlreadyHasReturnDoc extends ParentClass
{
/**
* @return parent
*/
public function getParent(): parent
{
return $this;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
class ParentClass
{
}
class AlreadyHasReturnDoc extends ParentClass
{
/**
* @return parent
*/
public function getParent()
{
return $this;
}
}
?>

View File

@ -0,0 +1,38 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
class ParentClass
{
}
class Fixture extends ParentClass
{
public function getParent(): parent
{
return $this;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
class ParentClass
{
}
class Fixture extends ParentClass
{
/**
* @return parent
*/
public function getParent()
{
return $this;
}
}
?>

View File

@ -0,0 +1,53 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
interface SomeInterface
{
public function run(): parent;
}
class ParentClass
{
}
class SomeClass extends ParentClass implements SomeInterface
{
public function run(): parent
{
return $this;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
interface SomeInterface
{
/**
* @return parent
*/
public function run();
}
class ParentClass
{
}
class SomeClass extends ParentClass implements SomeInterface
{
/**
* @return parent
*/
public function run()
{
return $this;
}
}
?>

View File

@ -0,0 +1,50 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
trait SomeTrait
{
public function run(): parent
{
return $this;
}
}
class ParentClass
{
}
class SomeClass extends ParentClass
{
use SomeTrait;
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
trait SomeTrait
{
/**
* @return parent
*/
public function run()
{
return $this;
}
}
class ParentClass
{
}
class SomeClass extends ParentClass
{
use SomeTrait;
}
?>

View File

@ -0,0 +1,13 @@
<?php
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture;
class SkipNoParent
{
public function run()
{
return $this;
}
}
?>

View File

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
use Rector\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(DowngradeParentTypeDeclarationRector::class);
};

View File

@ -0,0 +1,127 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp70\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\ParentStaticType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\DowngradeParentTypeDeclarationRectorTest
*/
final class DowngradeParentTypeDeclarationRector extends AbstractRector
{
public function __construct(
private PhpDocTypeChanger $phpDocTypeChanger,
private ReflectionProvider $reflectionProvider
) {
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove "parent" return type, add a "@return parent" tag instead',
[
new CodeSample(
<<<'CODE_SAMPLE'
class ParentClass
{
}
class SomeClass extends ParentClass
{
public function foo(): parent
{
return $this;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class ParentClass
{
}
class SomeClass extends ParentClass
{
/**
* @return parent
*/
public function foo()
{
return $this;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @param ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
if (! $node->returnType instanceof Name) {
return null;
}
if (! $this->nodeNameResolver->isName($node->returnType, 'parent')) {
return null;
}
$node->returnType = null;
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
if ($phpDocInfo->hasByType(ReturnTagValueNode::class)) {
return $node;
}
$parentStaticType = $this->getType($node);
if (! $parentStaticType instanceof ParentStaticType) {
return $node;
}
$this->phpDocTypeChanger->changeReturnType($phpDocInfo, $parentStaticType);
return $node;
}
private function getType(ClassMethod $classMethod): ?ParentStaticType
{
$scope = $classMethod->getAttribute(AttributeKey::SCOPE);
if ($scope === null) {
// in a trait
$className = $classMethod->getAttribute(AttributeKey::CLASS_NAME);
$classReflection = $this->reflectionProvider->getClass($className);
} else {
$classReflection = $scope->getClassReflection();
}
if (! $classReflection instanceof ClassReflection) {
return null;
}
return new ParentStaticType($classReflection);
}
}