rename Property to VariableInfo, few method name changes

This commit is contained in:
TomasVotruba 2018-03-05 00:48:28 +01:00
parent 14b490ffab
commit f5d1e24905
6 changed files with 32 additions and 39 deletions

View File

@ -5,7 +5,7 @@ namespace Rector\Builder\Class_;
final class ClassPropertyCollector final class ClassPropertyCollector
{ {
/** /**
* @var Property[][] * @var VariableInfo[][]
*/ */
private $classProperties = []; private $classProperties = [];
@ -14,11 +14,11 @@ final class ClassPropertyCollector
*/ */
public function addPropertyForClass(string $class, array $propertyTypes, string $propertyName): void public function addPropertyForClass(string $class, array $propertyTypes, string $propertyName): void
{ {
$this->classProperties[$class][] = Property::createFromNameAndTypes($propertyName, $propertyTypes); $this->classProperties[$class][] = VariableInfo::createFromNameAndTypes($propertyName, $propertyTypes);
} }
/** /**
* @return Property[] * @return VariableInfo[]
*/ */
public function getPropertiesForClass(string $class): array public function getPropertiesForClass(string $class): array
{ {

View File

@ -2,7 +2,7 @@
namespace Rector\Builder\Class_; namespace Rector\Builder\Class_;
final class Property final class VariableInfo
{ {
/** /**
* @var string * @var string
@ -24,11 +24,11 @@ final class Property
} }
/** /**
* @param string[] $propertyTypes * @param string[] $types
*/ */
public static function createFromNameAndTypes(string $propertyName, array $propertyTypes): self public static function createFromNameAndTypes(string $name, array $types): self
{ {
return new self($propertyName, $propertyTypes); return new self($name, $types);
} }
public function getName(): string public function getName(): string

View File

@ -10,7 +10,7 @@ use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Expression;
use Rector\Builder\Class_\Property; use Rector\Builder\Class_\VariableInfo;
use Rector\Node\NodeFactory; use Rector\Node\NodeFactory;
final class ConstructorMethodBuilder final class ConstructorMethodBuilder
@ -37,15 +37,15 @@ final class ConstructorMethodBuilder
$this->nodeFactory = $nodeFactory; $this->nodeFactory = $nodeFactory;
} }
/** public function addPropertyWithExpression(
* @todo optimize with the other method Class_ $classNode,
*/ VariableInfo $argument,
public function addPropertyWithExpression(Class_ $classNode, Property $argument, Expr $exprNode, Property $assignProperty): void Expr $exprNode,
{ VariableInfo $assignProperty
$constructorMethod = $classNode->getMethod('__construct') ?: null; ): void {
$propertyAssignNode = $this->nodeFactory->createPropertyAssignmentWithExpr($assignProperty->getName(), $exprNode); $propertyAssignNode = $this->nodeFactory->createPropertyAssignmentWithExpr($assignProperty->getName(), $exprNode);
$constructorMethod = $classNode->getMethod('__construct') ?: null;
/** @var ClassMethod $constructorMethod */ /** @var ClassMethod $constructorMethod */
if ($constructorMethod) { if ($constructorMethod) {
// has parameter already? // has parameter already?
@ -63,21 +63,15 @@ final class ConstructorMethodBuilder
return; return;
} }
/** @var Method $constructorMethod */ $constructorMethod = $this->createMethodWithPropertyAndAssign('__construct', $argument, $propertyAssignNode);
$constructorMethod = $this->builderFactory->method('__construct')
->makePublic()
->addParam($this->createParameter($argument->getTypes(), $argument->getName()))
->addStmts([$propertyAssignNode]);
$this->statementGlue->addAsFirstMethod($classNode, $constructorMethod->getNode()); $this->statementGlue->addAsFirstMethod($classNode, $constructorMethod->getNode());
} }
public function addPropertyAssignToClass(Class_ $classNode, Property $property): void public function addPropertyAssignToClass(Class_ $classNode, VariableInfo $property): void
{ {
$constructorMethod = $classNode->getMethod('__construct') ?: null;
$propertyAssignNode = $this->nodeFactory->createPropertyAssignment($property->getName()); $propertyAssignNode = $this->nodeFactory->createPropertyAssignment($property->getName());
$constructorMethod = $classNode->getMethod('__construct') ?: null;
/** @var ClassMethod $constructorMethod */ /** @var ClassMethod $constructorMethod */
if ($constructorMethod) { if ($constructorMethod) {
// has parameter already? // has parameter already?
@ -95,8 +89,7 @@ final class ConstructorMethodBuilder
return; return;
} }
$constructorMethod = $this->createMethodsWithArgumentsAndAssign('__construct', $property, $propertyAssignNode); $constructorMethod = $this->createMethodWithPropertyAndAssign('__construct', $property, $propertyAssignNode);
$this->statementGlue->addAsFirstMethod($classNode, $constructorMethod->getNode()); $this->statementGlue->addAsFirstMethod($classNode, $constructorMethod->getNode());
} }
@ -107,17 +100,17 @@ final class ConstructorMethodBuilder
{ {
$paramBuild = $this->builderFactory->param($propertyName); $paramBuild = $this->builderFactory->param($propertyName);
foreach ($propertyTypes as $propertyType) { foreach ($propertyTypes as $propertyType) {
$paramBuild->setTypeHint($this->nodeFactory->createTypeNamespace($propertyType)); $paramBuild->setTypeHint($this->nodeFactory->createTypeName($propertyType));
} }
return $paramBuild; return $paramBuild;
} }
private function createMethodsWithArgumentsAndAssign(string $name, Property $property, Expression $expressionNode): Method private function createMethodWithPropertyAndAssign(string $name, VariableInfo $variable, Expression $expressionNode): Method
{ {
return $this->builderFactory->method($name) return $this->builderFactory->method($name)
->makePublic() ->makePublic()
->addParam($this->createParameter($property->getTypes(), $property->getName())) ->addParam($this->createParameter($variable->getTypes(), $variable->getName()))
->addStmts([$expressionNode]); ->addStmts([$expressionNode]);
} }
} }

View File

@ -6,7 +6,7 @@ use PhpParser\BuilderFactory;
use PhpParser\Comment\Doc; use PhpParser\Comment\Doc;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property as PhpParserProperty; use PhpParser\Node\Stmt\Property as PhpParserProperty;
use Rector\Builder\Class_\Property; use Rector\Builder\Class_\VariableInfo;
use Rector\Naming\PropertyNaming; use Rector\Naming\PropertyNaming;
final class PropertyBuilder final class PropertyBuilder
@ -27,7 +27,7 @@ final class PropertyBuilder
$this->statementGlue = $statementGlue; $this->statementGlue = $statementGlue;
} }
public function addPropertyToClass(Class_ $classNode, Property $property): void public function addPropertyToClass(Class_ $classNode, VariableInfo $property): void
{ {
if ($this->doesPropertyAlreadyExist($classNode, $property)) { if ($this->doesPropertyAlreadyExist($classNode, $property)) {
return; return;
@ -38,7 +38,7 @@ final class PropertyBuilder
$this->statementGlue->addAsFirstMethod($classNode, $propertyNode); $this->statementGlue->addAsFirstMethod($classNode, $propertyNode);
} }
private function buildPrivatePropertyNode(Property $property): PhpParserProperty private function buildPrivatePropertyNode(VariableInfo $property): PhpParserProperty
{ {
$docComment = $this->createDocWithVarAnnotation($property->getTypes()); $docComment = $this->createDocWithVarAnnotation($property->getTypes());
@ -59,7 +59,7 @@ final class PropertyBuilder
. PHP_EOL . ' */'); . PHP_EOL . ' */');
} }
private function doesPropertyAlreadyExist(Class_ $classNode, Property $property): bool private function doesPropertyAlreadyExist(Class_ $classNode, VariableInfo $property): bool
{ {
foreach ($classNode->stmts as $inClassNode) { foreach ($classNode->stmts as $inClassNode) {
if (! $inClassNode instanceof PhpParserProperty) { if (! $inClassNode instanceof PhpParserProperty) {

View File

@ -222,7 +222,7 @@ final class NodeFactory
return new StaticCall(new Name($class), new Identifier($method), $arguments); return new StaticCall(new Name($class), new Identifier($method), $arguments);
} }
public function createTypeNamespace(string $name): Name public function createTypeName(string $name): Name
{ {
if (PropertyNaming::isPhpReservedType($name)) { if (PropertyNaming::isPhpReservedType($name)) {
return new Name($name); return new Name($name);

View File

@ -7,7 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Variable; use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\Class_;
use Rector\Builder\Class_\Property; use Rector\Builder\Class_\VariableInfo;
use Rector\Builder\ConstructorMethodBuilder; use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\PropertyBuilder; use Rector\Builder\PropertyBuilder;
use Rector\Node\Attribute; use Rector\Node\Attribute;
@ -59,13 +59,13 @@ final class RemoveParentDoctrineRepositoryRector extends AbstractRector
$node->extends = null; $node->extends = null;
// add $repository property // add $repository property
$property = Property::createFromNameAndTypes('repository', ['Doctrine\ORM\EntityRepository']); $parameterInfo = VariableInfo::createFromNameAndTypes('repository', ['Doctrine\ORM\EntityRepository']);
$this->propertyBuilder->addPropertyToClass($node, $property); $this->propertyBuilder->addPropertyToClass($node, $parameterInfo);
// add repository to constuctor // add repository to constuctor
$methodCall = new MethodCall(new Variable('entityManager'), 'getRepository'); $methodCall = new MethodCall(new Variable('entityManager'), 'getRepository');
$argument = Property::createFromNameAndTypes('entityManager', ['Doctrine\ORM\EntityManager']); $propertyInfo = VariableInfo::createFromNameAndTypes('entityManager', ['Doctrine\ORM\EntityManager']);
$this->constructorMethodBuilder->addPropertyWithExpression($node, $argument, $methodCall, $property); $this->constructorMethodBuilder->addPropertyWithExpression($node, $propertyInfo, $methodCall, $parameterInfo);
return $node; return $node;
} }