mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-24 11:44:14 +01:00
add Property ValueObject
This commit is contained in:
parent
deb96928b0
commit
407e85fa68
@ -5,7 +5,7 @@ namespace Rector\Builder\Class_;
|
||||
final class ClassPropertyCollector
|
||||
{
|
||||
/**
|
||||
* @var mixed[][]
|
||||
* @var Property[][]
|
||||
*/
|
||||
private $classProperties = [];
|
||||
|
||||
@ -14,14 +14,11 @@ final class ClassPropertyCollector
|
||||
*/
|
||||
public function addPropertyForClass(string $class, array $propertyTypes, string $propertyName): void
|
||||
{
|
||||
$this->classProperties[$class][] = [
|
||||
'name' => $propertyName,
|
||||
'types' => $propertyTypes,
|
||||
];
|
||||
$this->classProperties[$class][] = Property::createFromNameAndTypes($propertyName, $propertyTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
* @return Property[]
|
||||
*/
|
||||
public function getPropertiesForClass(string $class): array
|
||||
{
|
||||
|
46
src/Builder/Class_/Property.php
Normal file
46
src/Builder/Class_/Property.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\Builder\Class_;
|
||||
|
||||
final class Property
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $types = [];
|
||||
|
||||
/**
|
||||
* @param string[] $types
|
||||
*/
|
||||
private function __construct(string $name, array $types)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->types = $types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $propertyTypes
|
||||
*/
|
||||
public static function createFromNameAndTypes(string $propertyName, array $propertyTypes): self
|
||||
{
|
||||
return new self($propertyName, $propertyTypes);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getTypes(): array
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ use PhpParser\Builder\Param;
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use Rector\Builder\Class_\Property;
|
||||
use Rector\Node\NodeFactory;
|
||||
|
||||
final class ConstructorMethodBuilder
|
||||
@ -39,22 +40,22 @@ final class ConstructorMethodBuilder
|
||||
/**
|
||||
* @param string[] $propertyTypes
|
||||
*/
|
||||
public function addPropertyAssignToClass(Class_ $classNode, array $propertyTypes, string $propertyName): void
|
||||
public function addPropertyAssignToClass(Class_ $classNode, Property $property): void
|
||||
{
|
||||
$constructorMethod = $classNode->getMethod('__construct') ?: null;
|
||||
|
||||
$propertyAssignNode = $this->nodeFactory->createPropertyAssignment($propertyName);
|
||||
$propertyAssignNode = $this->nodeFactory->createPropertyAssignment($property->getName());
|
||||
|
||||
/** @var ClassMethod $constructorMethod */
|
||||
if ($constructorMethod) {
|
||||
// has parameter already?
|
||||
foreach ($constructorMethod->params as $constructorParameter) {
|
||||
if ($constructorParameter->var->name === $propertyName) {
|
||||
if ($constructorParameter->var->name === $property->getName()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
$constructorMethod->params[] = $this->createParameter($propertyTypes, $propertyName)
|
||||
$constructorMethod->params[] = $this->createParameter($property->getTypes(), $property->getName())
|
||||
->getNode();
|
||||
|
||||
$constructorMethod->stmts[] = $propertyAssignNode;
|
||||
@ -65,7 +66,7 @@ final class ConstructorMethodBuilder
|
||||
/** @var Method $constructorMethod */
|
||||
$constructorMethod = $this->builderFactory->method('__construct')
|
||||
->makePublic()
|
||||
->addParam($this->createParameter($propertyTypes, $propertyName))
|
||||
->addParam($this->createParameter($property->getTypes(), $property->getName()))
|
||||
->addStmts([$propertyAssignNode]);
|
||||
|
||||
$this->statementGlue->addAsFirstMethod($classNode, $constructorMethod->getNode());
|
||||
|
@ -5,7 +5,8 @@ namespace Rector\Builder;
|
||||
use PhpParser\BuilderFactory;
|
||||
use PhpParser\Comment\Doc;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\Property;
|
||||
use PhpParser\Node\Stmt\Property as PhpParserProperty;
|
||||
use Rector\Builder\Class_\Property;
|
||||
|
||||
final class PropertyBuilder
|
||||
{
|
||||
@ -25,16 +26,13 @@ final class PropertyBuilder
|
||||
$this->statementGlue = $statementGlue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $propertyTypes
|
||||
*/
|
||||
public function addPropertyToClass(Class_ $classNode, array $propertyTypes, string $propertyName): void
|
||||
public function addPropertyToClass(Class_ $classNode, Property $property): void
|
||||
{
|
||||
if ($this->doesPropertyAlreadyExist($classNode, $propertyName)) {
|
||||
if ($this->doesPropertyAlreadyExist($classNode, $property)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$propertyNode = $this->buildPrivatePropertyNode($propertyTypes, $propertyName);
|
||||
$propertyNode = $this->buildPrivatePropertyNode($property);
|
||||
|
||||
$this->statementGlue->addAsFirstMethod($classNode, $propertyNode);
|
||||
}
|
||||
@ -42,11 +40,11 @@ final class PropertyBuilder
|
||||
/**
|
||||
* @param string[] $propertyTypes
|
||||
*/
|
||||
private function buildPrivatePropertyNode(array $propertyTypes, string $propertyName): Property
|
||||
private function buildPrivatePropertyNode(Property $property): PhpParserProperty
|
||||
{
|
||||
$docComment = $this->createDocWithVarAnnotation($propertyTypes);
|
||||
$docComment = $this->createDocWithVarAnnotation($property->getTypes());
|
||||
|
||||
$propertyBuilder = $this->builderFactory->property($propertyName)
|
||||
$propertyBuilder = $this->builderFactory->property($property->getName())
|
||||
->makePrivate()
|
||||
->setDocComment($docComment);
|
||||
|
||||
@ -63,16 +61,16 @@ final class PropertyBuilder
|
||||
. PHP_EOL . ' */');
|
||||
}
|
||||
|
||||
private function doesPropertyAlreadyExist(Class_ $classNode, string $propertyName): bool
|
||||
private function doesPropertyAlreadyExist(Class_ $classNode, Property $property): bool
|
||||
{
|
||||
foreach ($classNode->stmts as $inClassNode) {
|
||||
if (! $inClassNode instanceof Property) {
|
||||
if (! $inClassNode instanceof PhpParserProperty) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$classPropertyName = (string) $inClassNode->props[0]->name;
|
||||
|
||||
if ($classPropertyName === $propertyName) {
|
||||
if ($classPropertyName === $property->getName()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -65,12 +65,8 @@ final class PropertyToClassAdder extends NodeVisitorAbstract
|
||||
}
|
||||
|
||||
foreach ($propertiesForClass as $property) {
|
||||
$this->constructorMethodBuilder->addPropertyAssignToClass(
|
||||
$classNode,
|
||||
$property['types'],
|
||||
$property['name']
|
||||
);
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $property['types'], $property['name']);
|
||||
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $property);
|
||||
$this->propertyBuilder->addPropertyToClass($classNode, $property);
|
||||
}
|
||||
|
||||
return $classNode;
|
||||
|
Loading…
x
Reference in New Issue
Block a user