/** * Create method from function declaration. * * @param FunctionDeclarationNode $function_node * @return ClassMethodNode */ public static function fromFunction(FunctionDeclarationNode $function_node) { $method_name = $function_node->getName()->getText(); $parameters = $function_node->getParameterList()->getText(); $body = $function_node->getBody()->getText(); /** @var ClassNode $class_node */ $class_node = Parser::parseSnippet("class Method {public function {$method_name}({$parameters}) {$body}}"); FormatterFactory::format($class_node); $method_node = $class_node->getStatements()[0]->remove(); return $method_node; }
public function testMatchReflector() { $function = FunctionDeclarationNode::create('array_walk'); $reflector = new \ReflectionFunction('array_walk'); // $function->getReference() should return a TokenNode or NULL, which will // loosely evaluate to TRUE or FALSE $this->assertEquals($function->getReference(), $reflector->returnsReference()); }
public function __construct(TargetInterface $target, $form_id, RewriterInterface $rewriter) { $indexer = $target->getIndexer('function'); $this->target = $target; $this->formID = $form_id; $this->builder = $indexer->get($form_id); $validator = $form_id . '_validate'; if ($indexer->has($validator)) { $this->validator = $indexer->get($validator); } $submit_handler = $form_id . '_submit'; if ($indexer->has($submit_handler)) { $this->submitHandler = $indexer->get($submit_handler); } $this->isConfig = $this->builder->has(Filter::isFunctionCall('system_settings_form')); $this->formStateRewriter = $rewriter; }
/** * {@inheritdoc} */ public function execute() { $this->moduleHandler->loadInclude($this->configuration['module'], 'php', 'api'); $hook = $this->configuration['hook']; $function = FunctionDeclarationNode::create($this->target->id() . '_' . $hook); $function->setDocComment(DocCommentNode::create('Implements hook_' . $hook)); $reflector = new \ReflectionFunction('hook_' . $hook); $function->matchReflector($reflector); $module = $this->target->getPath('.module'); $doc = $this->target->open($module)->append($function); $this->target->save($doc); }
/** * @param FunctionDeclarationNode|ClassMethodNode|InterfaceMethodNode $node */ protected function handleParameterWrapping($node) { $parameter_list = $node->getParameterList(); $parameter_wrapped = $parameter_list->children(Filter::isNewline())->isNotEmpty(); if ($parameter_wrapped) { $this->newlineAfter($parameter_list); if (!$node instanceof InterfaceMethodNode && $node->getBody()) { $this->spaceBefore($node->getBody()); } } }
/** * Parse function/method body. * * @param FunctionDeclarationNode|ClassMethodNode $function * Function or method. */ private function body($function) { $function->addChild($this->innerStatementBlock(), 'body'); }
public function testFunctionDeclaration() { $expected = 'function test($a, $b) {}'; $function = FunctionDeclarationNode::create('test', ['$a', '$b']); $this->assertEquals($expected, $function->getText()); $function = FunctionDeclarationNode::create('badoink'); $function->appendParameter(ParameterNode::create('badonk')); $this->assertEquals('function badoink($badonk) {}', $function->getText()); }
/** * Determine if the node belongs to this namespace. * * @param ClassNode|InterfaceNode|TraitNode|FunctionDeclarationNode|ConstantDeclarationNode $node * Node to test if owned by namespace. * * @return bool */ public function owns($node) { return $this === $node->getName()->getNamespace(); }
/** * Creates an empty implementation of a hook. * * @param TargetInterface $target * The target module. * @param string $hook * The hook to implement, without the hook_ prefix. * * @return \Pharborist\Functions\FunctionDeclarationNode * The hook implementation, appended to the main module file. */ protected function implement(TargetInterface $target, $hook) { $function = FunctionDeclarationNode::create($target->id() . '_' . $hook); $function->setDocComment(DocCommentNode::create('Implements hook_' . $hook . '().')); $module_file = $target->getPath('.module'); $target->open($module_file)->append($function); WhitespaceNode::create("\n")->insertBefore($function); WhitespaceNode::create("\n")->insertAfter($function); return $function; }