mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-19 06:52:51 +02:00
Added NewObjectToFactoryCreateRector
This commit is contained in:
parent
ffd914ce26
commit
a78d88a87c
@ -0,0 +1,126 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Rector\Architecture\Factory;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use Rector\NodeTypeResolver\Node\Attribute;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\ConfiguredCodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
use ReflectionClass;
|
||||
|
||||
final class NewObjectToFactoryCreateRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @var string[][]
|
||||
*/
|
||||
private $objectToFactoryMethod = [];
|
||||
|
||||
/**
|
||||
* @param string[][] $objectToFactoryMethod
|
||||
*/
|
||||
public function __construct(array $objectToFactoryMethod)
|
||||
{
|
||||
$this->objectToFactoryMethod = $objectToFactoryMethod;
|
||||
}
|
||||
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('Replaces creating object instances with "new" keyword with factory method.', [
|
||||
new ConfiguredCodeSample(
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
public function example() {
|
||||
new MyClass($argument);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
<<<'CODE_SAMPLE'
|
||||
class SomeClass
|
||||
{
|
||||
/**
|
||||
* @var \MyClassFactory
|
||||
*/
|
||||
private $myClassFactory;
|
||||
|
||||
public function example() {
|
||||
$this->myClassFactory->create($argument);
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
,
|
||||
['MyClass' => [
|
||||
'class' => 'MyClassFactory',
|
||||
'method' => 'create',
|
||||
]]
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [New_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param New_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
foreach ($this->objectToFactoryMethod as $object => $factoryInfo) {
|
||||
if (! $this->isType($node, $object)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Class_ $classNode */
|
||||
$classNode = $node->getAttribute(Attribute::CLASS_NODE);
|
||||
$factoryClass = $factoryInfo['class'];
|
||||
$factoryMethod = $factoryInfo['method'];
|
||||
$propertyName = $this->getExistingFactoryPropertyName($classNode, $factoryClass);
|
||||
|
||||
if ($propertyName === null) {
|
||||
$propertyName = $this->getFactoryPropertyName($factoryClass);
|
||||
|
||||
$this->addPropertyToClass($classNode, $factoryClass, $propertyName);
|
||||
}
|
||||
|
||||
return new MethodCall(
|
||||
new PropertyFetch(new Variable('this'), $propertyName),
|
||||
$factoryMethod,
|
||||
$node->args
|
||||
);
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function getExistingFactoryPropertyName(Class_ $classNode, string $factoryClass): ?string
|
||||
{
|
||||
foreach ($classNode->stmts as $node) {
|
||||
if ($node instanceof Property && $this->isType($node, $factoryClass)) {
|
||||
return (string) $node->props[0]->name;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private function getFactoryPropertyName(string $factoryFullQualifiedName): string
|
||||
{
|
||||
$className = (new ReflectionClass($factoryFullQualifiedName))->getShortName();
|
||||
|
||||
return Strings::firstLower($className);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass;
|
||||
|
||||
final class SomeController
|
||||
{
|
||||
public function default()
|
||||
{
|
||||
new MyClass('abcd');
|
||||
$class = new MyClass('abcd');
|
||||
}
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass;
|
||||
|
||||
final class SomeController
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory
|
||||
*/
|
||||
private $myClassFactory;
|
||||
public function __construct(\Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory $myClassFactory)
|
||||
{
|
||||
$this->myClassFactory = $myClassFactory;
|
||||
}
|
||||
public function default()
|
||||
{
|
||||
$this->myClassFactory->create('abcd');
|
||||
$class = $this->myClassFactory->create('abcd');
|
||||
}
|
||||
}
|
||||
?>
|
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass;
|
||||
|
||||
final class SomeOtherController
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory
|
||||
*/
|
||||
private $mySomeFactory;
|
||||
public function __construct(\Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory $mySomeFactory)
|
||||
{
|
||||
$this->mySomeFactory = $mySomeFactory;
|
||||
}
|
||||
public function default()
|
||||
{
|
||||
new MyClass('abcd');
|
||||
$class = new MyClass('abcd');
|
||||
}
|
||||
}
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Fixture;
|
||||
|
||||
use Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass;
|
||||
|
||||
final class SomeOtherController
|
||||
{
|
||||
/**
|
||||
* @var \Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory
|
||||
*/
|
||||
private $mySomeFactory;
|
||||
public function __construct(\Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory $mySomeFactory)
|
||||
{
|
||||
$this->mySomeFactory = $mySomeFactory;
|
||||
}
|
||||
public function default()
|
||||
{
|
||||
$this->mySomeFactory->create('abcd');
|
||||
$class = $this->mySomeFactory->create('abcd');
|
||||
}
|
||||
}
|
||||
?>
|
@ -0,0 +1,18 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector;
|
||||
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class NewObjectToFactoryCreateRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc', __DIR__ . '/Fixture/fixture2.php.inc']);
|
||||
}
|
||||
|
||||
protected function provideConfig(): string
|
||||
{
|
||||
return __DIR__ . '/config.yml';
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source;
|
||||
|
||||
final class MyClass
|
||||
{
|
||||
public function __construct(string $argument)
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source;
|
||||
|
||||
final class MyClassFactory
|
||||
{
|
||||
public function create(string $argument): MyClass
|
||||
{
|
||||
return new MyClass($argument);
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
services:
|
||||
Rector\Rector\Architecture\Factory\NewObjectToFactoryCreateRector:
|
||||
Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClass:
|
||||
class: Rector\Tests\Rector\Architecture\Factory\NewObjectToFactoryCreateRector\Source\MyClassFactory
|
||||
method: create
|
Loading…
x
Reference in New Issue
Block a user