Overview

Namespaces

  • Pharborist
    • Constants
    • ControlStructures
    • Exceptions
    • Functions
    • Generators
    • Namespaces
    • Objects
    • Operators
    • Types
    • Variables

Classes

  • Pharborist\Variables\CompoundVariableNode
  • Pharborist\Variables\GlobalStatementNode
  • Pharborist\Variables\ReferenceVariableNode
  • Pharborist\Variables\StaticVariableNode
  • Pharborist\Variables\StaticVariableStatementNode
  • Pharborist\Variables\VariableNode
  • Pharborist\Variables\VariableVariableNode

Interfaces

  • Pharborist\Variables\VariableExpressionNode
  • Overview
  • Namespace
  • Class
 1: <?php
 2: namespace Pharborist\Functions;
 3: 
 4: use Pharborist\Objects\ClassMethodNode;
 5: use Pharborist\Objects\ClassNode;
 6: use Pharborist\Namespaces\IdentifierNameTrait;
 7: use Pharborist\Namespaces\NameNode;
 8: use Pharborist\Node;
 9: use Pharborist\Parser;
10: use Pharborist\StatementBlockNode;
11: use Pharborist\StatementNode;
12: use Pharborist\TokenNode;
13: 
14: /**
15:  * A function declaration.
16:  */
17: class FunctionDeclarationNode extends StatementNode {
18:   use IdentifierNameTrait;
19:   use FunctionTrait;
20: 
21:   /**
22:    * Create a function declaration.
23:    *
24:    * @param NameNode|string $function_name
25:    *   The function name.
26:    * @param array $parameters
27:    *   (Optional) List of parameters.
28:    *
29:    * @return FunctionDeclarationNode
30:    */
31:   public static function create($function_name, $parameters = NULL) {
32:     /** @var FunctionDeclarationNode $function */
33:     $function = Parser::parseSnippet("function $function_name() {}");
34:     if (is_array($parameters)) {
35:       foreach ($parameters as $parameter) {
36:         if (is_string($parameter)) {
37:           $parameter = ParameterNode::create($parameter);
38:         }
39:         $function->appendParameter($parameter);
40:       }
41:     }
42:     return $function;
43:   }
44: 
45:   /**
46:    * @var StatementBlockNode
47:    */
48:   protected $body;
49: 
50:   /**
51:    * Set the name of the declared function.
52:    *
53:    * @param string $name
54:    *   New function name.
55:    * @return $this
56:    */
57:   public function setName($name) {
58:     /** @var TokenNode $function_name */
59:     $function_name = $this->getName()->firstChild();
60:     $function_name->setText($name);
61:     return $this;
62:   }
63: 
64:   /**
65:    * @return StatementBlockNode
66:    */
67:   public function getBody() {
68:     return $this->body;
69:   }
70: 
71:   /**
72:    * Creates a class method from this function and add it to the given
73:    * class definition.
74:    *
75:    * @param \Pharborist\Objects\ClassNode $class
76:    *  The class to add the new method to.
77:    *
78:    * @return \Pharborist\Objects\ClassMethodNode
79:    *  The newly created method.
80:    */
81:   public function cloneAsMethodOf(ClassNode $class) {
82:     $clone = ClassMethodNode::fromFunction($this);
83:     $class->appendMethod($clone);
84:     return $clone;
85:   }
86: 
87:   protected function childInserted(Node $node) {
88:     if ($node instanceof TokenNode && $node->getType() === '&') {
89:       $this->reference = $node;
90:     }
91:   }
92: }
93: 
Pharborist API documentation generated by ApiGen