-
-
Notifications
You must be signed in to change notification settings - Fork 739
Nette extracted rules + GetToMethodCallRector #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
74ccc0a
[DeprecatedAnnotation] init IDEA
TomasVotruba d49abc4
[Nette] add few rules for 2.4
TomasVotruba 7c6346d
[MagicDisclosure] GetToMethodCallRector init
TomasVotruba 37c81fd
make tests pass
TomasVotruba 2cdb724
details
TomasVotruba c2677c0
simplify PropertyFetchAnalyzer
TomasVotruba 3284240
[MagicDisclosure] add SetToMethodCallRector
TomasVotruba b89abf1
fix conflicts
TomasVotruba 0a17e66
decoupling
TomasVotruba 5438eb2
misc
TomasVotruba b9006e5
[Nette] add get/set magic rectors
TomasVotruba ed747a4
[MagicDisclosure] IssetToMethodCallRector init
TomasVotruba 8fec7af
Merge Isset and Unset Rectors to one
TomasVotruba 64bc2c9
[Nette] add 2 rectors
TomasVotruba 73d7b76
merge Get and Set Rector
TomasVotruba 550d44a
improve cs
TomasVotruba b30dc87
typo
TomasVotruba d28af5f
spaces [ci skip]
TomasVotruba e423836
decouple ExpressionAnalyzer
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,7 @@ | ||
| # Deprecated Annotation | ||
|
|
||
| Idea to normalize and automate deprecations, thanks @JanTvrdik for pushing further | ||
|
|
||
| Inspire: | ||
|
|
||
| - https://hackernoon.com/how-kotlins-deprecated-relieves-pain-of-colossal-refactoring-8577545aaed | ||
This file contains hidden or 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,12 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Rector\DeprecatedAnnotation; | ||
|
|
||
| use Doctrine\Common\Annotations\Annotation; | ||
|
|
||
| /** | ||
| * @Annotation | ||
| */ | ||
| final class Deprecated | ||
| { | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,37 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Rector\NodeAnalyzer; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Assign; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use PhpParser\Node\Stmt\Expression; | ||
|
|
||
| final class ExpressionAnalyzer | ||
| { | ||
| public function resolvePropertyFetch(Node $node): ?PropertyFetch | ||
| { | ||
| if (! $node instanceof Expression) { | ||
| return null; | ||
| } | ||
|
|
||
| if (! $node->expr instanceof Assign) { | ||
| return null; | ||
| } | ||
|
|
||
| return $this->resolvePropertyFetchNodeFromAssignNode($node->expr); | ||
| } | ||
|
|
||
| private function resolvePropertyFetchNodeFromAssignNode(Assign $assignNode): ?PropertyFetch | ||
| { | ||
| if ($assignNode->expr instanceof PropertyFetch) { | ||
| return $assignNode->expr; | ||
| } | ||
|
|
||
| if ($assignNode->var instanceof PropertyFetch) { | ||
| return $assignNode->var; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or 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,59 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Rector\NodeAnalyzer; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use Rector\BetterReflection\Reflector\SmartClassReflector; | ||
| use Rector\Node\Attribute; | ||
| use ReflectionProperty; | ||
|
|
||
| final class PropertyFetchAnalyzer | ||
| { | ||
| /** | ||
| * @var string[][] | ||
| */ | ||
| private $publicPropertyNamesForType = []; | ||
|
|
||
| /** | ||
| * @var SmartClassReflector | ||
| */ | ||
| private $smartClassReflector; | ||
|
|
||
| public function __construct(SmartClassReflector $smartClassReflector) | ||
| { | ||
| $this->smartClassReflector = $smartClassReflector; | ||
| } | ||
|
|
||
| public function isMagicPropertyFetchOnType(Node $node, string $type): bool | ||
| { | ||
| if (! $node instanceof PropertyFetch) { | ||
| return false; | ||
| } | ||
|
|
||
| $variableNodeType = $node->var->getAttribute(Attribute::TYPE); | ||
| if ($variableNodeType !== $type) { | ||
| return false; | ||
| } | ||
|
|
||
| $nodePropertyName = $node->name->name; | ||
| $publicPropertyNames = $this->getPublicPropertyNamesForType($type); | ||
|
|
||
| return ! in_array($nodePropertyName, $publicPropertyNames, true); | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| private function getPublicPropertyNamesForType(string $type): array | ||
| { | ||
| if (isset($this->publicPropertyNamesForType[$type])) { | ||
| return $this->publicPropertyNamesForType[$type]; | ||
| } | ||
|
|
||
| $classReflection = $this->smartClassReflector->reflect($type); | ||
| $publicProperties = $classReflection->getProperties(ReflectionProperty::IS_PUBLIC); | ||
|
|
||
| return $this->publicPropertyNamesForType[$type] = array_keys($publicProperties); | ||
| } | ||
| } |
138 changes: 138 additions & 0 deletions
138
src/Rector/MagicDisclosure/GetAndSetToMethodCallRector.php
This file contains hidden or 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,138 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| namespace Rector\Rector\MagicDisclosure; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Assign; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Expr\PropertyFetch; | ||
| use PhpParser\Node\Stmt\Expression; | ||
| use Rector\Node\NodeFactory; | ||
| use Rector\NodeAnalyzer\ExpressionAnalyzer; | ||
| use Rector\NodeAnalyzer\PropertyFetchAnalyzer; | ||
| use Rector\Rector\AbstractRector; | ||
|
|
||
| /** | ||
| * __get/__set to specific call | ||
| * | ||
| * Example - from: | ||
| * - $someService = $container->someService; | ||
| * - $container->someService = $someService; | ||
| * | ||
| * To | ||
| * - $container->getService('someService'); | ||
| * - $container->setService('someService', $someService); | ||
| */ | ||
| final class GetAndSetToMethodCallRector extends AbstractRector | ||
| { | ||
| /** | ||
| * @var string[] | ||
| */ | ||
| private $typeToMethodCalls = []; | ||
|
|
||
| /** | ||
| * @var PropertyFetchAnalyzer | ||
| */ | ||
| private $propertyAccessAnalyzer; | ||
|
|
||
| /** | ||
| * @var NodeFactory | ||
| */ | ||
| private $nodeFactory; | ||
|
|
||
| /** | ||
| * @var string[] | ||
| */ | ||
| private $activeTransformation = []; | ||
|
|
||
| /** | ||
| * @var ExpressionAnalyzer | ||
| */ | ||
| private $expressionAnalyzer; | ||
|
|
||
| /** | ||
| * Type to method call() | ||
| * | ||
| * @param string[] $typeToMethodCalls | ||
| */ | ||
| public function __construct( | ||
| array $typeToMethodCalls, | ||
| PropertyFetchAnalyzer $propertyAccessAnalyzer, | ||
| NodeFactory $nodeFactory, | ||
| ExpressionAnalyzer $expressionAnalyzer | ||
| ) { | ||
| $this->typeToMethodCalls = $typeToMethodCalls; | ||
| $this->propertyAccessAnalyzer = $propertyAccessAnalyzer; | ||
| $this->nodeFactory = $nodeFactory; | ||
| $this->expressionAnalyzer = $expressionAnalyzer; | ||
| } | ||
|
|
||
| public function isCandidate(Node $node): bool | ||
| { | ||
| $this->activeTransformation = []; | ||
|
|
||
| $propertyFetchNode = $this->expressionAnalyzer->resolvePropertyFetch($node); | ||
| if ($propertyFetchNode === null) { | ||
| return false; | ||
| } | ||
|
|
||
| foreach ($this->typeToMethodCalls as $type => $transformation) { | ||
| if ($this->propertyAccessAnalyzer->isMagicPropertyFetchOnType($propertyFetchNode, $type)) { | ||
| $this->activeTransformation = $transformation; | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param Expression $expressionNode | ||
| */ | ||
| public function refactor(Node $expressionNode): ?Node | ||
| { | ||
| if ($expressionNode->expr->expr instanceof PropertyFetch) { | ||
| /** @var PropertyFetch $propertyFetchNode */ | ||
| $propertyFetchNode = $expressionNode->expr->expr; | ||
| $method = $this->activeTransformation['get']; | ||
| $expressionNode->expr->expr = $this->createMethodCallNodeFromPropertyFetchNode($propertyFetchNode, $method); | ||
|
|
||
| return $expressionNode; | ||
| } | ||
|
|
||
| /** @var Assign $assignNode */ | ||
| $assignNode = $expressionNode->expr; | ||
| $method = $this->activeTransformation['set']; | ||
| $expressionNode->expr = $this->createMethodCallNodeFromAssignNode($assignNode, $method); | ||
|
|
||
| return $expressionNode; | ||
| } | ||
|
|
||
| private function createMethodCallNodeFromPropertyFetchNode( | ||
| PropertyFetch $propertyFetchNode, | ||
| string $method | ||
| ): MethodCall { | ||
| $value = $propertyFetchNode->name->name; | ||
|
|
||
| return $this->nodeFactory->createMethodCallWithVariableAndArguments( | ||
| $propertyFetchNode->var, | ||
| $method, | ||
| [$value] | ||
| ); | ||
| } | ||
|
|
||
| private function createMethodCallNodeFromAssignNode(Assign $assignNode, string $method): MethodCall | ||
| { | ||
| /** @var PropertyFetch $propertyFetchNode */ | ||
| $propertyFetchNode = $assignNode->var; | ||
|
|
||
| $key = $propertyFetchNode->name->name; | ||
|
|
||
| return $this->nodeFactory->createMethodCallWithVariableAndArguments( | ||
| $propertyFetchNode->var, | ||
| $method, | ||
| [$key, $assignNode->expr] | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JanTvrdik I think this post shows the idea you have about smart deprecated messages. Implemented right in IDE :)
Bit better than dump annotation, but still not automated.