2020-03-29 01:35:25 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Privatization\Rector\MethodCall;
|
2020-03-29 01:35:25 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-03-29 01:35:25 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector\PrivatizeLocalGetterToPropertyRectorTest
|
2020-03-29 01:35:25 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class PrivatizeLocalGetterToPropertyRector extends AbstractRector
|
2020-03-29 01:35:25 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-03-29 01:35:25 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Privatize getter of local property to property', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-03-29 01:35:25 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private $some;
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return $this->getSome() + 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSome()
|
|
|
|
{
|
|
|
|
return $this->some;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-03-29 01:35:25 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private $some;
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
return $this->some + 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSome()
|
|
|
|
{
|
|
|
|
return $this->some;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-03-29 01:35:25 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-03-29 01:35:25 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-03-29 01:35:25 +00:00
|
|
|
{
|
2023-06-09 17:42:49 +00:00
|
|
|
return [Class_::class];
|
2020-03-29 01:35:25 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-06-09 17:42:49 +00:00
|
|
|
* @param Class_ $node
|
2020-03-29 01:35:25 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-03-29 01:35:25 +00:00
|
|
|
{
|
2023-06-09 17:42:49 +00:00
|
|
|
$class = $node;
|
|
|
|
$hasChanged = \false;
|
2023-12-02 08:58:47 +00:00
|
|
|
$isFinal = $class->isFinal();
|
|
|
|
$this->traverseNodesWithCallable($node, function (Node $node) use($class, &$hasChanged, $isFinal) : ?PropertyFetch {
|
2023-06-09 17:42:49 +00:00
|
|
|
if (!$node instanceof MethodCall) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-02 13:42:50 +00:00
|
|
|
if ($node->isFirstClassCallable()) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
if (!$node->var instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($node->var, 'this')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$methodName = $this->getName($node->name);
|
|
|
|
if ($methodName === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$classMethod = $class->getMethod($methodName);
|
|
|
|
if (!$classMethod instanceof ClassMethod) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-02 08:58:47 +00:00
|
|
|
if (!$classMethod->isPrivate() && !$isFinal) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-19 11:00:56 +00:00
|
|
|
$propertyFetch = $this->matchLocalPropertyFetchInGetterMethod($classMethod);
|
|
|
|
if (!$propertyFetch instanceof PropertyFetch) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
$hasChanged = \true;
|
2023-06-19 11:00:56 +00:00
|
|
|
return $propertyFetch;
|
2023-06-09 17:42:49 +00:00
|
|
|
});
|
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
2020-03-29 01:35:25 +00:00
|
|
|
}
|
2023-06-09 17:42:49 +00:00
|
|
|
return null;
|
2020-03-29 01:35:25 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function matchLocalPropertyFetchInGetterMethod(ClassMethod $classMethod) : ?PropertyFetch
|
2020-03-29 01:35:25 +00:00
|
|
|
{
|
2020-12-20 21:05:48 +00:00
|
|
|
$stmts = (array) $classMethod->stmts;
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($stmts) !== 1) {
|
2020-03-29 01:35:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
2020-12-20 21:05:48 +00:00
|
|
|
$onlyStmt = $stmts[0] ?? null;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$onlyStmt instanceof Return_) {
|
2020-03-29 01:35:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-09 00:29:10 +00:00
|
|
|
$returnedExpr = $onlyStmt->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$returnedExpr instanceof PropertyFetch) {
|
2020-03-29 01:35:25 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-03-09 00:29:10 +00:00
|
|
|
return $returnedExpr;
|
2020-03-29 01:35:25 +00:00
|
|
|
}
|
|
|
|
}
|