This commit is contained in:
TomasVotruba 2017-08-08 02:30:29 +02:00
parent 7d5a1250eb
commit 70bb18f099
14 changed files with 60 additions and 85 deletions

View File

@ -11,9 +11,7 @@ final class ClassPropertyCollector
public function addPropertyForClass(string $class, string $propertyType, string $propertyName): void
{
$this->classProperties[$class] = [
$propertyType => $propertyName,
];
$this->classProperties[$class][$propertyType] = $propertyName;
}
/**

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\DependencyInjection\NamedServicesToConstructor;
namespace Rector\NodeVisitor\DependencyInjection;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
@ -8,7 +8,6 @@ use PhpParser\NodeVisitorAbstract;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\PropertyBuilder;
use Rector\NodeTraverser\TokenSwitcher;
/**
* Add new propertis to class and to contructor.
@ -30,21 +29,14 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
*/
private $newClassPropertyCollector;
/**
* @var TokenSwitcher
*/
private $tokenSwitcher;
public function __construct(
ConstructorMethodBuilder $constructorMethodBuilder,
PropertyBuilder $propertyBuilder,
ClassPropertyCollector $newClassPropertyCollector,
TokenSwitcher $tokenSwitcher
ClassPropertyCollector $newClassPropertyCollector
) {
$this->constructorMethodBuilder = $constructorMethodBuilder;
$this->propertyBuilder = $propertyBuilder;
$this->newClassPropertyCollector = $newClassPropertyCollector;
$this->tokenSwitcher = $tokenSwitcher;
}
/**
@ -53,24 +45,24 @@ final class AddPropertiesToClassNodeVisitor extends NodeVisitorAbstract
*/
public function afterTraverse(array $nodes): array
{
foreach ($nodes as $node) {
foreach ($nodes as $key => $node) {
if ($node instanceof Class_) {
$this->reconstruct($node, (string) $node->name);
break;
$nodes[$key] = $this->reconstruct($node, (string) $node->name);
}
}
return $nodes;
}
private function reconstruct(Class_ $classNode, string $className): void
private function reconstruct(Class_ $classNode, string $className): Class_
{
$propertiesForClass = $this->newClassPropertyCollector->getPropertiesforClass($className);
foreach ($propertiesForClass as $propertyType => $propertyName) {
$this->tokenSwitcher->enable();
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
$this->propertyBuilder->addPropertyToClass($classNode, $propertyType, $propertyName);
}
return $classNode;
}
}

View File

@ -1,15 +1,17 @@
<?php declare(strict_types=1);
namespace Rector\NodeVisitor\DependencyInjection;
namespace Rector\NodeVisitor\DependencyInjection\InjectAnnotationToConstructor;
use PhpCsFixer\DocBlock\DocBlock;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeVisitorAbstract;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\NodeTraverser\TokenSwitcher;
final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
final class PropertyRector extends NodeVisitorAbstract
{
/**
* @var string
@ -21,19 +23,36 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
*/
private $tokenSwitcher;
// /**
// * @var ConstructorMethodBuilder
// */
// private $constructorMethodBuilder;
//
// public function __construct(ConstructorMethodBuilder $constructorMethodBuilder)
// {
// $this->constructorMethodBuilder = $constructorMethodBuilder;
// }
/**
* @var ClassPropertyCollector
*/
private $classPropertyCollector;
public function __construct(TokenSwitcher $tokenSwitcher)
/**
* @var string
*/
private $className;
public function __construct(TokenSwitcher $tokenSwitcher, ClassPropertyCollector $classPropertyCollector)
{
$this->tokenSwitcher = $tokenSwitcher;
$this->classPropertyCollector = $classPropertyCollector;
}
/**
* @param Node[] $nodes
* @return null|array
*/
public function beforeTraverse(array $nodes): ?array
{
dump('1');
foreach ($nodes as $node) {
if ($node instanceof Class_) {
$this->className = (string) $node->name;
}
}
return null;
}
public function enterNode(Node $node): ?Node
@ -42,6 +61,8 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
return null;
}
dump('2');
return $this->reconstructProperty($node);
}
@ -59,43 +80,14 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
return true;
}
// private function reconstruct(Class_ $classNode): Node
// {
// dump($classNode);
// die;
//
// foreach ($classNode->stmts as $classElementStatement) {
// if (! $classElementStatement instanceof Property) {
// continue;
// }
//
// $propertyNode = $classElementStatement;
//
// $propertyDocBlock = $this->createDocBlockFromNode($propertyNode);
// $injectAnnotations = $propertyDocBlock->getAnnotationsOfType(self::ANNOTATION_INJECT);
// if (! $injectAnnotations) {
// continue;
// }
//
// $this->removeInjectAnnotationFromProperty($propertyNode, $propertyDocBlock);
// $this->makePropertyPrivate($propertyNode);
//
// $propertyType = $propertyDocBlock->getAnnotationsOfType('var')[0]
// ->getTypes()[0];
// $propertyName = (string) $propertyNode->props[0]->name;
//
// $this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
// }
//
// return $classNode;
// }
private function reconstructProperty($propertyNode): Property
{
$propertyDocBlock = $this->createDocBlockFromNode($propertyNode);
$propertyNode = $this->removeInjectAnnotationFromProperty($propertyNode, $propertyDocBlock);
// $propertyNode->flags = Class_::MODIFIER_PRIVATE;
$propertyNode->flags = Class_::MODIFIER_PRIVATE;
$this->addPropertyToCollector($propertyNode, $propertyDocBlock);
return $propertyNode;
}
@ -115,7 +107,6 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
private function removeInjectAnnotationFromProperty(Property $propertyNode, DocBlock $propertyDocBlock): Property
{
$injectAnnotations = $propertyDocBlock->getAnnotationsOfType(self::ANNOTATION_INJECT);
foreach ($injectAnnotations as $injectAnnotation) {
$injectAnnotation->remove();
}
@ -124,4 +115,14 @@ final class InjectAnnotationToConstructorRector extends NodeVisitorAbstract
return $propertyNode;
}
private function addPropertyToCollector(Property $propertyNode, DocBlock $propertyDocBlock): void
{
$propertyType = $propertyDocBlock->getAnnotationsOfType('var')[0]
->getTypes()[0];
$propertyName = (string)$propertyNode->props[0]->name;
$this->classPropertyCollector->addPropertyForClass($this->className, $propertyType, $propertyName);
}
}

View File

@ -13,7 +13,7 @@ use PhpParser\NodeVisitorAbstract;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
use Rector\Builder\Naming\NameResolver;
use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source\LocalKernel;
use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRector\Source\LocalKernel;
/**
* Converts all:
@ -22,7 +22,7 @@ use Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRecon
* into:
* $this->someService # where "someService" is type of the service
*/
final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
final class GetterToPropertyRector extends NodeVisitorAbstract
{
/**
* @var string
@ -69,25 +69,10 @@ final class GetterToPropertyNodeVisitor extends NodeVisitorAbstract
return null;
}
/**
* Return value semantics:
* * null
* => $node stays as-is
* * NodeTraverser::DONT_TRAVERSE_CHILDREN
* => Children of $node are not traversed. $node stays as-is
* * NodeTraverser::STOP_TRAVERSAL
* => Traversal is aborted. $node stays as-is
* * otherwise
* => $node is set to the return value.
*
* @return null|int|Node
*/
public function enterNode(Node $node)
public function enterNode(Node $node): ?Node
{
if ($this->isCandidate($node)) {
$this->reconstruct($node);
return $node;
return $this->reconstruct($node);
}
return null;

View File

@ -6,7 +6,6 @@ class ClassWithInjects
* @var stdClass
*/
private $property;
/**
* @var DateTimeInterface
*/

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor\Source;
namespace Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRector\Source;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;

View File

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorReconstructor;
namespace Rector\Tests\NodeVisitor\DependencyInjection\NamedServicesToConstructorRector;
use Rector\Testing\PHPUnit\AbstractReconstructorTestCase;