-
-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DowngradePhp70] Add DowngradeParentTypeDeclarationRector (#755)
Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
885ef76
commit f9ee33b
Showing
9 changed files
with
369 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...sMethod/DowngradeParentTypeDeclarationRector/DowngradeParentTypeDeclarationRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...r/ClassMethod/DowngradeParentTypeDeclarationRector/Fixture/already_has_return_doc.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
?> |
38 changes: 38 additions & 0 deletions
38
...radePhp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
?> |
53 changes: 53 additions & 0 deletions
53
...hp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector/Fixture/in_interface.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
?> |
50 changes: 50 additions & 0 deletions
50
...adePhp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector/Fixture/in_trait.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector/Fixture/skip_no_parent.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DowngradePhp70\Rector\ClassMethod\DowngradeParentTypeDeclarationRector\Fixture; | ||
|
||
class SkipNoParent | ||
{ | ||
public function run() | ||
{ | ||
return $this; | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...ePhp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
127 changes: 127 additions & 0 deletions
127
rules/DowngradePhp70/Rector/ClassMethod/DowngradeParentTypeDeclarationRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |