2021-01-18 10:44:07 +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\Transform\Rector\New_;
|
2021-01-18 10:44:07 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\MethodCall;
|
|
|
|
use PhpParser\Node\Expr\New_;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PHPStan\Type\ObjectType;
|
|
|
|
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Naming\Naming\PropertyNaming;
|
|
|
|
use Rector\Naming\ValueObject\ExpectedName;
|
|
|
|
use Rector\PostRector\Collector\PropertyToAddCollector;
|
|
|
|
use Rector\PostRector\ValueObject\PropertyMetadata;
|
|
|
|
use Rector\Transform\NodeFactory\PropertyFetchFactory;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2023-06-01 08:56:46 +00:00
|
|
|
use RectorPrefix202306\Webmozart\Assert\Assert;
|
2021-01-18 10:44:07 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\Transform\Rector\New_\NewToConstructorInjectionRector\NewToConstructorInjectionRectorTest
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class NewToConstructorInjectionRector extends AbstractRector implements ConfigurableRectorInterface
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-02-27 02:13:22 +00:00
|
|
|
* @var ObjectType[]
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
2021-02-27 02:13:22 +00:00
|
|
|
private $constructorInjectionObjectTypes = [];
|
2021-01-18 10:44:07 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Transform\NodeFactory\PropertyFetchFactory
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
|
|
|
private $propertyFetchFactory;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Naming\PropertyNaming
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
|
|
|
private $propertyNaming;
|
2021-07-25 11:42:11 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-07-25 11:42:11 +00:00
|
|
|
* @var \Rector\PostRector\Collector\PropertyToAddCollector
|
|
|
|
*/
|
|
|
|
private $propertyToAddCollector;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(PropertyFetchFactory $propertyFetchFactory, PropertyNaming $propertyNaming, PropertyToAddCollector $propertyToAddCollector)
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
|
|
|
$this->propertyFetchFactory = $propertyFetchFactory;
|
|
|
|
$this->propertyNaming = $propertyNaming;
|
2021-07-25 11:42:11 +00:00
|
|
|
$this->propertyToAddCollector = $propertyToAddCollector;
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change defined new type to constructor injection', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2021-01-18 10:44:07 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$validator = new Validator();
|
|
|
|
$validator->validate(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-01-18 10:44:07 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Validator
|
|
|
|
*/
|
|
|
|
private $validator;
|
|
|
|
|
|
|
|
public function __construct(Validator $validator)
|
|
|
|
{
|
|
|
|
$this->validator = $validator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$this->validator->validate(1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-12-06 21:12:03 +00:00
|
|
|
, ['Validator'])]);
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [New_::class, Assign::class, MethodCall::class, Expression::class];
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-04 22:24:14 +00:00
|
|
|
* @param New_|Assign|MethodCall|Expression $node
|
2021-01-18 10:44:07 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof MethodCall) {
|
2021-01-18 10:44:07 +00:00
|
|
|
return $this->refactorMethodCall($node);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof Expression) {
|
2022-05-04 22:24:14 +00:00
|
|
|
$nodeExpr = $node->expr;
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($nodeExpr instanceof Assign) {
|
2022-05-04 22:24:14 +00:00
|
|
|
$this->refactorAssignExpression($node, $nodeExpr);
|
|
|
|
}
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($node instanceof New_) {
|
2021-01-18 10:44:07 +00:00
|
|
|
$this->refactorNew($node);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-27 02:13:22 +00:00
|
|
|
/**
|
2021-11-28 17:01:20 +00:00
|
|
|
* @param mixed[] $configuration
|
2021-02-27 02:13:22 +00:00
|
|
|
*/
|
2021-12-10 10:22:23 +00:00
|
|
|
public function configure(array $configuration) : void
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2022-02-18 01:46:17 +00:00
|
|
|
$typesToConstructorInjections = $configuration;
|
2022-06-07 08:22:29 +00:00
|
|
|
Assert::allString($typesToConstructorInjections);
|
2021-03-05 10:55:40 +00:00
|
|
|
foreach ($typesToConstructorInjections as $typeToConstructorInjection) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->constructorInjectionObjectTypes[] = new ObjectType($typeToConstructorInjection);
|
2021-02-27 02:13:22 +00:00
|
|
|
}
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorMethodCall(MethodCall $methodCall) : ?MethodCall
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2021-02-27 02:13:22 +00:00
|
|
|
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$methodCall->var instanceof Variable) {
|
2021-01-18 10:47:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isObjectType($methodCall->var, $constructorInjectionObjectType)) {
|
2021-01-18 10:44:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeTypeResolver->isObjectType($methodCall->var, $constructorInjectionObjectType)) {
|
2021-02-28 07:47:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$methodCall->var = $this->propertyFetchFactory->createFromType($constructorInjectionObjectType);
|
2021-01-18 10:44:07 +00:00
|
|
|
return $methodCall;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorAssignExpression(Expression $expression, Assign $assign) : void
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$currentAssign = $assign->expr instanceof Assign ? $assign->expr : $assign;
|
|
|
|
if (!$currentAssign->expr instanceof New_) {
|
2021-01-18 10:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-25 21:20:10 +00:00
|
|
|
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
|
2022-05-04 22:24:14 +00:00
|
|
|
if (!$this->isObjectType($currentAssign->expr, $constructorInjectionObjectType)) {
|
2021-01-18 10:44:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-04 22:24:14 +00:00
|
|
|
$this->removeNode($expression);
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function refactorNew(New_ $new) : void
|
2021-01-18 10:44:07 +00:00
|
|
|
{
|
2021-02-27 02:13:22 +00:00
|
|
|
foreach ($this->constructorInjectionObjectTypes as $constructorInjectionObjectType) {
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->isObjectType($new->class, $constructorInjectionObjectType)) {
|
2021-01-18 10:44:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$classLike = $this->betterNodeFinder->findParentType($new, Class_::class);
|
|
|
|
if (!$classLike instanceof Class_) {
|
2021-01-18 10:44:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-27 02:13:22 +00:00
|
|
|
$expectedPropertyName = $this->propertyNaming->getExpectedNameFromType($constructorInjectionObjectType);
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$expectedPropertyName instanceof ExpectedName) {
|
2021-01-18 10:44:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$propertyMetadata = new PropertyMetadata($expectedPropertyName->getName(), $constructorInjectionObjectType, Class_::MODIFIER_PRIVATE);
|
2021-07-25 11:42:11 +00:00
|
|
|
$this->propertyToAddCollector->addPropertyToClass($classLike, $propertyMetadata);
|
2021-01-18 10:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|