constructorPropertyDefaultExprResolver = $constructorPropertyDefaultExprResolver; } public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition { return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Move property default from constructor to property default', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE' final class SomeClass { private $name; public function __construct() { $this->name = 'John'; } } CODE_SAMPLE , <<<'CODE_SAMPLE' final class SomeClass { private $name = 'John'; } CODE_SAMPLE )]); } /** * @return array> */ public function getNodeTypes() : array { return [\PhpParser\Node\Stmt\Class_::class]; } /** * @param Class_ $node */ public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node { $constructClassMethod = $node->getMethod(\Rector\Core\ValueObject\MethodName::CONSTRUCT); if (!$constructClassMethod instanceof \PhpParser\Node\Stmt\ClassMethod) { return null; } // resolve property defaults $defaultPropertyExprAssigns = $this->constructorPropertyDefaultExprResolver->resolve($constructClassMethod); if ($defaultPropertyExprAssigns === []) { return null; } $hasChanged = \false; foreach ($defaultPropertyExprAssigns as $defaultPropertyExprAssign) { $property = $node->getProperty($defaultPropertyExprAssign->getPropertyName()); if (!$property instanceof \PhpParser\Node\Stmt\Property) { continue; } $propertyProperty = $property->props[0]; $propertyProperty->default = $defaultPropertyExprAssign->getDefaultExpr(); $hasChanged = \true; $this->removeNode($defaultPropertyExprAssign->getAssignExpression()); } if (!$hasChanged) { return null; } return $node; } }