Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:

allow_failures:
- name: ECS
- name: Rector
- name: Standalone Run
- name: Documentation
- os: windows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ private function createNewProperties(array $fetchedLocalPropertyNameToTypes, arr
continue;
}

$propertyBuilder = $this->builderFactory->property($propertyName);
$propertyBuilder->makePublic();
$property = $propertyBuilder->getNode();
$property = $this->nodeFactory->createPublicProperty($propertyName);

if ($this->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
$phpStanNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($propertyType);
Expand Down
23 changes: 10 additions & 13 deletions packages/Doctrine/src/NodeFactory/EntityUuidNodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Doctrine\NodeFactory;

use Nette\Utils\Strings;
use PhpParser\BuilderFactory;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
Expand All @@ -18,14 +17,10 @@
use Ramsey\Uuid\Uuid;
use Rector\Doctrine\PhpDocParser\Ast\PhpDoc\PhpDocTagNodeFactory;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\PhpParser\Node\NodeFactory;

final class EntityUuidNodeFactory
{
/**
* @var BuilderFactory
*/
private $builderFactory;

/**
* @var PhpDocTagNodeFactory
*/
Expand All @@ -36,22 +31,24 @@ final class EntityUuidNodeFactory
*/
private $docBlockManipulator;

/**
* @var NodeFactory
*/
private $nodeFactory;

public function __construct(
BuilderFactory $builderFactory,
PhpDocTagNodeFactory $phpDocTagNodeFactory,
DocBlockManipulator $docBlockManipulator
DocBlockManipulator $docBlockManipulator,
NodeFactory $nodeFactory
) {
$this->builderFactory = $builderFactory;
$this->phpDocTagNodeFactory = $phpDocTagNodeFactory;
$this->docBlockManipulator = $docBlockManipulator;
$this->nodeFactory = $nodeFactory;
}

public function createTemporaryUuidProperty(): Property
{
$uuidPropertyBuilder = $this->builderFactory->property('uuid');
$uuidPropertyBuilder->makePrivate();

$uuidProperty = $uuidPropertyBuilder->getNode();
$uuidProperty = $this->nodeFactory->createPrivateProperty('uuid');

$this->decoratePropertyWithUuidAnnotations($uuidProperty, true, false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ final class CheckoutEntityFactory
* @var SomeStaticClass
*/
private $someStaticClass;

public function setSomeStaticClass(SomeStaticClass $someStaticClass)
{
$this->someStaticClass = $someStaticClass;
}

public function run()
{
return $this->someStaticClass->go();
}
}
}
PHP
,
[
Expand Down Expand Up @@ -153,10 +153,7 @@ function (Node $node) use ($objectType): bool {

$setEntityFactoryMethod = $this->createSetEntityFactoryClassMethod($variableName, $param, $assign);

$entityFactoryPropertyBuilder = $this->builderFactory->property($variableName);
$entityFactoryPropertyBuilder->makePrivate();
$entityFactoryProperty = $entityFactoryPropertyBuilder->getNode();

$entityFactoryProperty = $this->nodeFactory->createPrivateProperty($variableName);
$this->docBlockManipulator->changeVarTag($entityFactoryProperty, $objectType);

$class->stmts = array_merge([$entityFactoryProperty, $setEntityFactoryMethod], $class->stmts);
Expand Down
15 changes: 10 additions & 5 deletions packages/RemovingStatic/src/UniqueObjectFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Rector\Naming\PropertyNaming;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\NodeTypeResolver\StaticTypeMapper;
use Rector\PhpParser\Node\NodeFactory;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class UniqueObjectFactoryFactory
Expand Down Expand Up @@ -51,18 +52,25 @@ final class UniqueObjectFactoryFactory
*/
private $staticTypeMapper;

/**
* @var NodeFactory
*/
private $nodeFactory;

public function __construct(
NameResolver $nameResolver,
BuilderFactory $builderFactory,
PropertyNaming $propertyNaming,
DocBlockManipulator $docBlockManipulator,
StaticTypeMapper $staticTypeMapper
StaticTypeMapper $staticTypeMapper,
NodeFactory $nodeFactory
) {
$this->nameResolver = $nameResolver;
$this->builderFactory = $builderFactory;
$this->propertyNaming = $propertyNaming;
$this->docBlockManipulator = $docBlockManipulator;
$this->staticTypeMapper = $staticTypeMapper;
$this->nodeFactory = $nodeFactory;
}

public function createFactoryClass(Class_ $class, ObjectType $objectType): Class_
Expand Down Expand Up @@ -110,11 +118,8 @@ private function createPropertiesFromTypes(ObjectType $objectType): array
$properties = [];

$propertyName = $this->propertyNaming->fqnToVariableName($objectType);
$propertyBuilder = $this->builderFactory->property($propertyName);
$propertyBuilder->makePrivate();

$property = $propertyBuilder->getNode();

$property = $this->nodeFactory->createPrivateProperty($propertyName);
$this->docBlockManipulator->changeVarTag($property, $objectType);

$properties[] = $property;
Expand Down
16 changes: 4 additions & 12 deletions packages/Symfony/src/Rector/Class_/MakeCommandLazyRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Property;
use PHPStan\Type\StringType;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -77,7 +76,10 @@ public function refactor(Node $node): ?Node
return null;
}

$defaultNameProperty = $this->createDefaultNameProperty($commandName);
$defaultNameProperty = $this->nodeFactory->createStaticProtectedPropertyWithDefault(
'defaultName',
$commandName
);

$node->stmts = array_merge([$defaultNameProperty], (array) $node->stmts);

Expand Down Expand Up @@ -126,16 +128,6 @@ private function resolveCommandNameAndRemove(Class_ $class): ?Node
return $commandName;
}

private function createDefaultNameProperty(Node $commandNameNode): Property
{
$propertyBuilder = $this->builderFactory->property('defaultName');
$propertyBuilder->makeProtected();
$propertyBuilder->makeStatic();
$propertyBuilder->setDefault($commandNameNode);

return $propertyBuilder->getNode();
}

private function matchCommandNameNodeInConstruct(Expr $expr): ?Node
{
if (! $expr instanceof MethodCall && ! $expr instanceof StaticCall) {
Expand Down
50 changes: 49 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\BuilderFactory;
use PhpParser\BuilderHelpers;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
Expand Down Expand Up @@ -167,7 +168,11 @@ public function createPrivatePropertyFromNameAndType(string $name, ?Type $type):
$propertyBuilder->setDocComment($docComment);
}

return $propertyBuilder->getNode();
$property = $propertyBuilder->getNode();

$this->decorateParentPropertyProperty($property);

return $property;
}

/**
Expand Down Expand Up @@ -223,6 +228,42 @@ public function createParentConstructWithParams(array $params): StaticCall
);
}

public function createPrivateProperty(string $name): Property
{
$propertyBuilder = $this->builderFactory->property($name);
$propertyBuilder->makePrivate();
$property = $propertyBuilder->getNode();

$this->decorateParentPropertyProperty($property);

return $property;
}

public function createStaticProtectedPropertyWithDefault(string $name, Node $node): Property
{
$propertyBuilder = $this->builderFactory->property($name);
$propertyBuilder->makeProtected();
$propertyBuilder->makeStatic();
$propertyBuilder->setDefault($node);

$property = $propertyBuilder->getNode();

$this->decorateParentPropertyProperty($property);

return $property;
}

public function createPublicProperty(string $name): Property
{
$propertyBuilder = $this->builderFactory->property($name);
$propertyBuilder->makePublic();
$property = $propertyBuilder->getNode();

$this->decorateParentPropertyProperty($property);

return $property;
}

/**
* @param string|int|null $key
*/
Expand Down Expand Up @@ -285,4 +326,11 @@ private function convertParamNodesToArgNodes(array $paramNodes): array

return $args;
}

private function decorateParentPropertyProperty(Property $property): void
{
// complete property property parent, needed for other operations
$propertyProperty = $property->props[0];
$propertyProperty->setAttribute(AttributeKey::PARENT_NODE, $property);
}
}