property;
$object->property = $value;
$bare = $object->bareProperty;
CODE_SAMPLE
, <<<'CODE_SAMPLE'
$result = $object->getProperty();
$object->setProperty($value);
$bare = $object->getConfig('someArg');
CODE_SAMPLE
, [new PropertyFetchToMethodCall('SomeObject', 'property', 'getProperty', 'setProperty'), new PropertyFetchToMethodCall('SomeObject', 'bareProperty', 'getConfig', null, ['someArg'])])]);
}
/**
* @return array>
*/
public function getNodeTypes() : array
{
return [Assign::class, PropertyFetch::class];
}
/**
* @param PropertyFetch|Assign $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof Assign && $node->var instanceof PropertyFetch) {
return $this->processSetter($node);
}
if ($node instanceof PropertyFetch) {
return $this->processGetter($node);
}
return null;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, PropertyFetchToMethodCall::class);
$this->propertiesToMethodCalls = $configuration;
}
private function processSetter(Assign $assign) : ?Node
{
/** @var PropertyFetch $propertyFetchNode */
$propertyFetchNode = $assign->var;
$propertyToMethodCall = $this->matchPropertyFetchCandidate($propertyFetchNode);
if (!$propertyToMethodCall instanceof PropertyFetchToMethodCall) {
return null;
}
if ($propertyToMethodCall->getNewSetMethod() === null) {
throw new ShouldNotHappenException();
}
$args = $this->nodeFactory->createArgs([$assign->expr]);
/** @var Variable $variable */
$variable = $propertyFetchNode->var;
return $this->nodeFactory->createMethodCall($variable, $propertyToMethodCall->getNewSetMethod(), $args);
}
private function processGetter(PropertyFetch $propertyFetch) : ?Node
{
$propertyFetchToMethodCall = $this->matchPropertyFetchCandidate($propertyFetch);
if (!$propertyFetchToMethodCall instanceof PropertyFetchToMethodCall) {
return null;
}
// simple method name
if ($propertyFetchToMethodCall->getNewGetMethod() !== '') {
$methodCall = $this->nodeFactory->createMethodCall($propertyFetch->var, $propertyFetchToMethodCall->getNewGetMethod());
if ($propertyFetchToMethodCall->getNewGetArguments() !== []) {
$methodCall->args = $this->nodeFactory->createArgs($propertyFetchToMethodCall->getNewGetArguments());
}
return $methodCall;
}
return $propertyFetch;
}
private function matchPropertyFetchCandidate(PropertyFetch $propertyFetch) : ?PropertyFetchToMethodCall
{
foreach ($this->propertiesToMethodCalls as $propertyToMethodCall) {
if (!$this->isName($propertyFetch, $propertyToMethodCall->getOldProperty())) {
continue;
}
if (!$this->isObjectType($propertyFetch->var, $propertyToMethodCall->getOldObjectType())) {
continue;
}
return $propertyToMethodCall;
}
return null;
}
}