2020-12-10 00:40:38 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\Php80\NodeResolver;
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
use PhpParser\Node;
|
2020-12-10 00:40:38 +01:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-12-27 02:04:31 +01:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-12-10 00:40:38 +01:00
|
|
|
use PhpParser\Node\Param;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
2020-12-27 02:04:31 +01:00
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
2020-12-10 00:40:38 +01:00
|
|
|
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
|
|
|
|
use Rector\Core\ValueObject\MethodName;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
|
|
|
use Rector\Php80\ValueObject\PropertyPromotionCandidate;
|
|
|
|
|
|
|
|
final class PromotedPropertyResolver
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var NodeNameResolver
|
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BetterStandardPrinter
|
|
|
|
*/
|
|
|
|
private $betterStandardPrinter;
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @var BetterNodeFinder
|
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
NodeNameResolver $nodeNameResolver,
|
|
|
|
BetterStandardPrinter $betterStandardPrinter,
|
|
|
|
BetterNodeFinder $betterNodeFinder
|
|
|
|
) {
|
2020-12-10 00:40:38 +01:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->betterStandardPrinter = $betterStandardPrinter;
|
2020-12-27 02:04:31 +01:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return PropertyPromotionCandidate[]
|
|
|
|
*/
|
|
|
|
public function resolveFromClass(Class_ $class): array
|
|
|
|
{
|
|
|
|
$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $constructClassMethod instanceof ClassMethod) {
|
2020-12-10 00:40:38 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$propertyPromotionCandidates = [];
|
|
|
|
foreach ($class->getProperties() as $property) {
|
2020-12-25 01:22:45 +01:00
|
|
|
if (count($property->props) !== 1) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$propertyPromotionCandidate = $this->matchPropertyPromotionCandidate($property, $constructClassMethod);
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $propertyPromotionCandidate instanceof PropertyPromotionCandidate) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$propertyPromotionCandidates[] = $propertyPromotionCandidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $propertyPromotionCandidates;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function matchPropertyPromotionCandidate(
|
|
|
|
Property $property,
|
|
|
|
ClassMethod $constructClassMethod
|
|
|
|
): ?PropertyPromotionCandidate {
|
|
|
|
$onlyProperty = $property->props[0];
|
2020-12-27 02:04:31 +01:00
|
|
|
|
2020-12-10 00:40:38 +01:00
|
|
|
$propertyName = $this->nodeNameResolver->getName($onlyProperty);
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
$firstParamAsVariable = $this->resolveFirstParamUses($constructClassMethod);
|
|
|
|
|
2020-12-10 00:40:38 +01:00
|
|
|
// match property name to assign in constructor
|
|
|
|
foreach ((array) $constructClassMethod->stmts as $stmt) {
|
|
|
|
if ($stmt instanceof Expression) {
|
|
|
|
$stmt = $stmt->expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $stmt instanceof Assign) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$assign = $stmt;
|
|
|
|
if (! $this->nodeNameResolver->isLocalPropertyFetchNamed($assign->var, $propertyName)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. is param
|
|
|
|
// @todo 2. is default value
|
|
|
|
$assignedExpr = $assign->expr;
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
if (! $assignedExpr instanceof Variable) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-10 00:40:38 +01:00
|
|
|
$matchedParam = $this->matchClassMethodParamByAssignedVariable($constructClassMethod, $assignedExpr);
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $matchedParam instanceof Param) {
|
2020-12-10 00:40:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
// is param used above assign?
|
|
|
|
if ($this->isParamUsedBeforeAssign($assignedExpr, $firstParamAsVariable)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-10 00:40:38 +01:00
|
|
|
return new PropertyPromotionCandidate($property, $assign, $matchedParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, int>
|
|
|
|
*/
|
|
|
|
private function resolveFirstParamUses(ClassMethod $classMethod): array
|
|
|
|
{
|
|
|
|
$paramByFirstUsage = [];
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
$paramName = $this->nodeNameResolver->getName($param);
|
|
|
|
|
|
|
|
$firstParamVariable = $this->betterNodeFinder->findFirst($classMethod->stmts, function (Node $node) use (
|
|
|
|
$paramName
|
|
|
|
): bool {
|
|
|
|
if (! $node instanceof Variable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nodeNameResolver->isName($node, $paramName);
|
|
|
|
});
|
|
|
|
|
2021-01-20 18:41:35 +07:00
|
|
|
if (! $firstParamVariable instanceof Node) {
|
2020-12-27 02:04:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$paramByFirstUsage[$paramName] = $firstParamVariable->getStartTokenPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $paramByFirstUsage;
|
|
|
|
}
|
|
|
|
|
2021-01-22 19:47:02 +01:00
|
|
|
private function matchClassMethodParamByAssignedVariable(
|
|
|
|
ClassMethod $classMethod,
|
|
|
|
Variable $variable
|
|
|
|
): ?Param {
|
|
|
|
foreach ($classMethod->params as $param) {
|
|
|
|
if (! $this->betterStandardPrinter->areNodesEqual($variable, $param->var)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $param;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-27 02:04:31 +01:00
|
|
|
/**
|
|
|
|
* @param array<string, int> $firstParamAsVariable
|
|
|
|
*/
|
|
|
|
private function isParamUsedBeforeAssign(Variable $variable, array $firstParamAsVariable): bool
|
|
|
|
{
|
|
|
|
$variableName = $this->nodeNameResolver->getName($variable);
|
|
|
|
|
|
|
|
$firstVariablePosition = $firstParamAsVariable[$variableName] ?? null;
|
|
|
|
if ($firstVariablePosition === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $firstVariablePosition < $variable->getStartTokenPos();
|
|
|
|
}
|
2020-12-10 00:40:38 +01:00
|
|
|
}
|