2022-03-15 17:33:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\Class_;
|
2022-03-15 17:33:16 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\MethodName;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2022-03-15 17:33:16 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector\InlineConstructorDefaultToPropertyRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class InlineConstructorDefaultToPropertyRector extends AbstractRector
|
2022-03-15 17:33:16 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\ConstructorPropertyDefaultExprResolver
|
|
|
|
*/
|
|
|
|
private $constructorPropertyDefaultExprResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ConstructorPropertyDefaultExprResolver $constructorPropertyDefaultExprResolver)
|
2022-03-15 17:33:16 +00:00
|
|
|
{
|
|
|
|
$this->constructorPropertyDefaultExprResolver = $constructorPropertyDefaultExprResolver;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2022-03-15 17:33:16 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Move property default from constructor to property default', [new CodeSample(<<<'CODE_SAMPLE'
|
2022-03-15 17:33:16 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->name = 'John';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private $name = 'John';
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @return array<class-string<Node>>
|
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2022-03-15 17:33:16 +00:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Class_ $node
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2022-03-15 17:33:16 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
|
|
|
|
if (!$constructClassMethod instanceof ClassMethod) {
|
2022-03-15 17:33:16 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// resolve property defaults
|
|
|
|
$defaultPropertyExprAssigns = $this->constructorPropertyDefaultExprResolver->resolve($constructClassMethod);
|
|
|
|
if ($defaultPropertyExprAssigns === []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$hasChanged = \false;
|
|
|
|
foreach ($defaultPropertyExprAssigns as $defaultPropertyExprAssign) {
|
|
|
|
$property = $node->getProperty($defaultPropertyExprAssign->getPropertyName());
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$property instanceof Property) {
|
2022-03-15 17:33:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$propertyProperty = $property->props[0];
|
|
|
|
$propertyProperty->default = $defaultPropertyExprAssign->getDefaultExpr();
|
|
|
|
$hasChanged = \true;
|
|
|
|
$this->removeNode($defaultPropertyExprAssign->getAssignExpression());
|
|
|
|
}
|
|
|
|
if (!$hasChanged) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|