[new \Rector\Transform\ValueObject\PropertyFetchToMethodCall('SomeObject', 'property', 'getProperty', 'setProperty')]]; $secondConfiguration = [self::PROPERTIES_TO_METHOD_CALLS => [new \Rector\Transform\ValueObject\PropertyFetchToMethodCall('SomeObject', 'property', 'getConfig', null, ['someArg'])]]; return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Replaces properties assign calls be defined methods.', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE' $result = $object->property; $object->property = $value; CODE_SAMPLE , <<<'CODE_SAMPLE' $result = $object->getProperty(); $object->setProperty($value); CODE_SAMPLE , $firstConfiguration), new \Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample(<<<'CODE_SAMPLE' $result = $object->property; CODE_SAMPLE , <<<'CODE_SAMPLE' $result = $object->getProperty('someArg'); CODE_SAMPLE , $secondConfiguration)]); } /** * @return array> */ public function getNodeTypes() : array { return [\PhpParser\Node\Expr\Assign::class, \PhpParser\Node\Expr\PropertyFetch::class]; } /** * @param PropertyFetch|Assign $node */ public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node { if ($node instanceof \PhpParser\Node\Expr\Assign && $node->var instanceof \PhpParser\Node\Expr\PropertyFetch) { return $this->processSetter($node); } if ($node instanceof \PhpParser\Node\Expr\PropertyFetch) { return $this->processGetter($node); } return null; } /** * @param array $configuration */ public function configure(array $configuration) : void { $propertiesToMethodCalls = $configuration[self::PROPERTIES_TO_METHOD_CALLS] ?? []; \RectorPrefix20210813\Webmozart\Assert\Assert::allIsInstanceOf($propertiesToMethodCalls, \Rector\Transform\ValueObject\PropertyFetchToMethodCall::class); $this->propertiesToMethodCalls = $propertiesToMethodCalls; } private function processSetter(\PhpParser\Node\Expr\Assign $assign) : ?\PhpParser\Node { /** @var PropertyFetch $propertyFetchNode */ $propertyFetchNode = $assign->var; $propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode); if (!$propertyToMethodCall instanceof \Rector\Transform\ValueObject\PropertyFetchToMethodCall) { return null; } if ($propertyToMethodCall->getNewSetMethod() === null) { throw new \Rector\Core\Exception\ShouldNotHappenException(); } $args = $this->nodeFactory->createArgs([$assign->expr]); /** @var Variable $variable */ $variable = $propertyFetchNode->var; return $this->nodeFactory->createMethodCall($variable, $propertyToMethodCall->getNewSetMethod(), $args); } private function processGetter(\PhpParser\Node\Expr\PropertyFetch $propertyFetch) : ?\PhpParser\Node { $propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetch); if (!$propertyToMethodCall instanceof \Rector\Transform\ValueObject\PropertyFetchToMethodCall) { return null; } // simple method name if ($propertyToMethodCall->getNewGetMethod() !== '') { $methodCall = $this->nodeFactory->createMethodCall($propertyFetch->var, $propertyToMethodCall->getNewGetMethod()); if ($propertyToMethodCall->getNewGetArguments() !== []) { $args = $this->nodeFactory->createArgs($propertyToMethodCall->getNewGetArguments()); $methodCall->args = $args; } return $methodCall; } return $propertyFetch; } private function matchPropertyFetchCandidate(\PhpParser\Node\Expr\PropertyFetch $propertyFetch) : ?\Rector\Transform\ValueObject\PropertyFetchToMethodCall { foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) { if (!$this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldObjectType())) { continue; } if (!$this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) { continue; } return $propertyToMethodCall; } return null; } }