Skip to content

Commit

Permalink
re-use setter factory node
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 2, 2021
1 parent 62a3331 commit f6c4a55
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
namespace Rector\RemovingStatic\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\PropertyNaming;
use Symplify\Astral\ValueObject\NodeBuilder\MethodBuilder;
use Symplify\Astral\ValueObject\NodeBuilder\ParamBuilder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -163,14 +159,7 @@ function (Node $node) use ($objectType): bool {
}

$variableName = $this->propertyNaming->fqnToVariableName($objectType);

$paramBuilder = new ParamBuilder($variableName);
$paramBuilder->setType(new FullyQualified($staticType));
$param = $paramBuilder->getNode();

$assign = $this->nodeFactory->createPropertyAssignment($variableName);

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

$entityFactoryProperty = $this->nodeFactory->createPrivateProperty($variableName);

Expand All @@ -193,20 +182,4 @@ private function isEntityFactoryStaticCall(Node $node, ObjectType $objectType):

return $this->isObjectType($node->class, $objectType);
}

private function createSetEntityFactoryClassMethod(
string $variableName,
Param $param,
Assign $assign
): ClassMethod {
$setMethodName = 'set' . ucfirst($variableName);

$setEntityFactoryMethodBuilder = new MethodBuilder($setMethodName);
$setEntityFactoryMethodBuilder->makePublic();
$setEntityFactoryMethodBuilder->addParam($param);
$setEntityFactoryMethodBuilder->setReturnType('void');
$setEntityFactoryMethodBuilder->addStmt($assign);

return $setEntityFactoryMethodBuilder->getNode();
}
}
21 changes: 14 additions & 7 deletions src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,7 @@ public function createPublicClassConst(string $name, $value): ClassConst

public function createGetterClassMethod(string $propertyName, Type $type): ClassMethod
{
$getterMethod = 'get' . ucfirst($propertyName);

$methodBuilder = new MethodBuilder($getterMethod);
$methodBuilder = new MethodBuilder('get' . ucfirst($propertyName));
$methodBuilder->makePublic();

$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
Expand All @@ -420,13 +418,13 @@ public function createGetterClassMethod(string $propertyName, Type $type): Class

public function createSetterClassMethod(string $propertyName, Type $type): ClassMethod
{
$getterMethod = 'set' . ucfirst($propertyName);
$methodBuilder = new MethodBuilder('set' . ucfirst($propertyName));
$methodBuilder->makePublic();

$variable = new Variable($propertyName);

$methodBuilder = new MethodBuilder($getterMethod);
$methodBuilder->makePublic();
$methodBuilder->addParam(new Param($variable));
$param = $this->createParamWithType($variable, $type);
$methodBuilder->addParam($param);

$propertyFetch = new PropertyFetch(new Variable(self::THIS), $propertyName);
$assign = new Assign($propertyFetch, $variable);
Expand Down Expand Up @@ -753,4 +751,13 @@ private function createBooleanAndFromNodes(array $exprs): BooleanAnd
/** @var BooleanAnd $booleanAnd */
return $booleanAnd;
}

private function createParamWithType(Variable $variable, Type $type): Param
{
$param = new Param($variable);

$phpParserTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($type);
$param->type = $phpParserTypeNode;
return $param;
}
}

0 comments on commit f6c4a55

Please sign in to comment.