/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if (sizeof($arguments) == 1) { return ObjectMethodCallNode::create(clone $arguments[0], 'save'); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $rewritten = ObjectMethodCallNode::create(clone $arguments[0], 'loadInclude')->appendArgument(clone $arguments[2])->appendArgument(clone $arguments[1]); if (sizeof($arguments) == 4) { $rewritten->appendArgument(clone $arguments[3]); } return $rewritten; }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $rewritten = ObjectMethodCallNode::create(clone $arguments[3], 'view'); if (sizeof($arguments) >= 5) { $rewritten->appendArgument(clone $arguments[4]); } return $rewritten; }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); if (isset($arguments[1]) && $arguments[1] instanceof VariableNode) { $rewritten = ObjectMethodCallNode::create(clone $arguments[1], 'hasPermission'); } else { $rewritten = ClassMethodCallNode::create('\\Drupal', 'currentUser')->appendMethodCall('hasPermission'); } return $rewritten->appendArgument(clone $arguments[0]); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $property = $arguments[2] instanceof StringNode ? $arguments[2]->toValue() : clone $arguments[2]; $rewritten = ObjectMethodCallNode::create(Parser::parseExpression($arguments[1] . '->' . $property), 'view'); if (sizeof($arguments) >= 4) { $rewritten->appendArgument(clone $arguments[3]); } return $rewritten; }
/** * Allows you to append a method call to this one, building a chain of method * calls. * * For example: * ``` * // \Drupal::entityManager() * $classCall = ClassMethodCallNode::create('\Drupal', 'entityManager'); * * $methodCall = $classCall->appendMethodCall('getDefinitions'); * echo $methodCall->getText(); // \Drupal::entityManager()->getDefinitions() * echo $methodCall->getObject(); // \Drupal::entityManager() * echo $methodCall->getMethodName(); // getDefinitions * * // You can chain yet another call, and keep going as long as you want. * * $methodCall = $methodCall->appendMethodCall('clearCache') * echo $methodCall->getText(); // \Drupal::entityManager()->getDefinitions()->clearCache() * * // These methods are chainable themselves, so you can build an entire call chain * // in one fell swoop. * * $chain = ClassMethodCallNode::create('Foo', 'bar')->appendMethodCall('baz')->appendMethodCall('zorg'); * echo $chain->getText(); // Foo::bar()->baz()->zorg() * ``` * * @param string $method_name * The name of the method to call. * * @return \Pharborist\Objects\ObjectMethodCallNode * The newly-created method call, in which every previous part of the chain will be the * "object", and $method_name will be the "method". The call will be created without * arguments, but you can add some using appendArgument(). */ public function appendMethodCall($method_name) { $method_call = ObjectMethodCallNode::create(clone $this, $method_name); $this->replaceWith($method_call); return $method_call; }
/** * Rewrites an assignment expression as a property setter. Returns NULL if * the expression cannot be rewritten. * * @param \Pharborist\ExpressionNode $expr * The expression to rewrite. * @param string $property * The property being used in the expression. * @param \Pharborist\Operators\AssignNode $assignment * The entire assignment expression being rewritten. * * @return \Pharborist\ExpressionNode|NULL */ public function rewriteAsSetter(ExpressionNode $expr, $property, AssignNode $assignment) { if ($expr instanceof ObjectPropertyNode) { // Should be getRootObject() or getLookupRoot(). // @see Pharborist issue #191 $object = clone $expr->getObject(); } elseif ($expr instanceof ArrayLookupNode) { $object = clone $expr->getRootArray(); } if (isset($object) && isset($this->pluginDefinition['properties'][$property]['set'])) { return ObjectMethodCallNode::create($object, $this->pluginDefinition['properties'][$property]['set'])->appendArgument(clone $assignment->getRightOperand()); } }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { return ObjectMethodCallNode::create(clone $call->getArgumentList()->getItem(0), 'save'); }
/** * {@inheritdoc} */ public function rewriteAsSetter(ExpressionNode $expr, $property, AssignNode $assignment) { /** @var \Pharborist\ArrayLookupNode $expr */ $object = clone $expr->getRootArray(); $keys = $expr->getKeys(); $value = clone $assignment->getRightOperand(); // $form_state['values']['baz'] = 'foo' --> $form_state->setValue(['baz'], 'foo') if ($property == 'values') { array_shift($keys); return ObjectMethodCallNode::create($object, 'setValue')->appendArgument(ArrayNode::create($keys))->appendArgument($value); } elseif (isset($this->pluginDefinition['properties'][$property]['set'])) { return parent::rewriteAsSetter($expr, $property, $assignment); } else { return ObjectMethodCallNode::create($object, 'set')->appendArgument(ArrayNode::create($keys))->appendArgument($value); } }
public function testChainMethodCall() { $object = Token::variable('$object'); $method_call = ObjectMethodCallNode::create($object, 'someMethod'); $chained_call = $method_call->appendMethodCall('chained'); $this->assertEquals('$object->someMethod()', $chained_call->getObject()->getText()); $this->assertEquals('chained', $chained_call->getMethodName()->getText()); $source = <<<'EOF' <?php $object->someMethod(); EOF; $tree = Parser::parseSource($source); /** @var ExpressionStatementNode $expr_statement */ $expr_statement = $tree->firstChild()->next(); /** @var ObjectMethodCallNode $method_call */ $method_call = $expr_statement->getExpression(); $method_call->appendMethodCall('chained'); $expected = <<<'EOF' <?php $object->someMethod()->chained(); EOF; $this->assertEquals($expected, $tree->getText()); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); $object = strPos($call->getName()->getText(), 'entity_') === 0 ? $arguments[1] : $arguments[0]; return ObjectMethodCallNode::create(clone $object, $this->pluginDefinition['method']); }
/** * {@inheritdoc} */ public function rewrite(FunctionCallNode $call, TargetInterface $target) { $arguments = $call->getArguments(); return ObjectMethodCallNode::create($arguments[2]->remove(), 'setValueForElement')->appendArgument(clone $arguments[0])->appendArgument(clone $arguments[1]); }