1: <?php
2: namespace Pharborist\Functions;
3:
4: use Pharborist\Objects\ObjectMethodCallNode;
5: use Pharborist\ParentNode;
6: use Pharborist\ParenTrait;
7:
8: /**
9: * Base class of any function or method call, including:
10: *
11: * ```
12: * foobar();
13: * $foo->bar();
14: * Foo::bar();
15: * $foo('bar');
16: * ```
17: */
18: abstract class CallNode extends ParentNode {
19: use ArgumentTrait;
20: use ParenTrait;
21:
22: /**
23: * Allows you to append a method call to this one, building a chain of method
24: * calls.
25: *
26: * For example:
27: * ```
28: * // \Drupal::entityManager()
29: * $classCall = ClassMethodCallNode::create('\Drupal', 'entityManager');
30: *
31: * $methodCall = $classCall->appendMethodCall('getDefinitions');
32: * echo $methodCall->getText(); // \Drupal::entityManager()->getDefinitions()
33: * echo $methodCall->getObject(); // \Drupal::entityManager()
34: * echo $methodCall->getMethodName(); // getDefinitions
35: *
36: * // You can chain yet another call, and keep going as long as you want.
37: *
38: * $methodCall = $methodCall->appendMethodCall('clearCache')
39: * echo $methodCall->getText(); // \Drupal::entityManager()->getDefinitions()->clearCache()
40: *
41: * // These methods are chainable themselves, so you can build an entire call chain
42: * // in one fell swoop.
43: *
44: * $chain = ClassMethodCallNode::create('Foo', 'bar')->appendMethodCall('baz')->appendMethodCall('zorg');
45: * echo $chain->getText(); // Foo::bar()->baz()->zorg()
46: * ```
47: *
48: * @param string $method_name
49: * The name of the method to call.
50: *
51: * @return \Pharborist\Objects\ObjectMethodCallNode
52: * The newly-created method call, in which every previous part of the chain will be the
53: * "object", and $method_name will be the "method". The call will be created without
54: * arguments, but you can add some using appendArgument().
55: */
56: public function appendMethodCall($method_name) {
57: $method_call = ObjectMethodCallNode::create(clone $this, $method_name);
58: $this->replaceWith($method_call);
59: return $method_call;
60: }
61: }
62: