/** * Adds a method to a class/trait. * * @param \Pharborist\Functions\FunctionDeclarationNode|\Pharborist\Objects\ClassMethodNode|string $method * The method to append. Can either be an existing method, a function (which * will be converted to a public method), or a string (a new public method * will be created with that name). * * @return $this */ public function appendMethod($method) { if ($method instanceof FunctionDeclarationNode) { $method = ClassMethodNode::fromFunction($method); } elseif (is_string($method)) { $method = ClassMethodNode::create($method); } $this->statements->lastChild()->before($method); FormatterFactory::format($this); return $this; }
/** * @return \Pharborist\Objects\ClassMethodNode */ protected function addMethod(FunctionDeclarationNode $function, ClassNode $class, $alias = NULL) { $method = ClassMethodNode::fromFunction($function); if ($alias) { $method->setName($alias); } $class->appendMethod($method); // Add the parameters required for FormInterface conformance. $parameters = $method->getParameters()->toArray(); if (empty($parameters)) { $parameters[0] = ParameterNode::create('$form'); $method->appendParameter($parameters[0]); } if (sizeof($parameters) == 1) { $parameters[1] = ParameterNode::create('$form_state'); $method->appendParameter($parameters[1]); } // The $form parameter must have the array type hint. $parameters[0]->setTypeHint('array'); // The form state is never passed by reference. $parameters[1]->setReference(FALSE); // Additional parameters MUST have a default value of NULL in order to conform // to FormInterface. for ($i = 2; $i < sizeof($parameters); $i++) { $parameters[$i]->setValue(new TokenNode(T_STRING, 'NULL')); } $this->formStateRewriter->rewrite($parameters[1]); return $method; }
public function testFunctionToMethod() { $class = <<<END class DefaultController extends ControllerBase {} END; $function = <<<'END' function diff_diffs_overview($node) { drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH); if ($cond) { test(); } return drupal_get_form('diff_node_revisions', $node); } END; $expected = <<<'END' class DefaultController extends ControllerBase { public function diff_diffs_overview($node) { drupal_set_title(t('Revisions for %title', ['%title' => $node->title]), PASS_THROUGH); if ($cond) { test(); } return drupal_get_form('diff_node_revisions', $node); } } END; /** @var ClassNode $class */ $class = Parser::parseSnippet($class); $function = Parser::parseSnippet($function); $class->appendMethod(ClassMethodNode::fromFunction($function)); $this->assertEquals($expected, $class->getText()); }
/** * Creates a class method from this function and add it to the given * class definition. * * @param \Pharborist\Objects\ClassNode $class * The class to add the new method to. * * @return \Pharborist\Objects\ClassMethodNode * The newly created method. */ public function cloneAsMethodOf(ClassNode $class) { $clone = ClassMethodNode::fromFunction($this); $class->appendMethod($clone); return $clone; }