/** * 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; }
/** * Add property to class. * * @param string|ClassMemberListNode $property * @return $this */ public function appendProperty($property) { if (is_string($property)) { $property = ClassMemberListNode::create($property); } $properties = $this->statements->children(Filter::isInstanceOf('\\Pharborist\\ClassMemberListNode')); if ($properties->count() === 0) { $this->statements->firstChild()->after($property); } else { $properties->last()->after($property); } FormatterFactory::format($this); return $this; }
/** * Adds a method to interface. * * @param InterfaceMethodNode|string $method * The method to append. Can either be an existing method, or a string (a * new public method will be created with that name). * * @return $this */ public function appendMethod($method) { if (is_string($method)) { $method = InterfaceMethodNode::create($method); } $this->statements->lastChild()->before($method); FormatterFactory::format($this); return $this; }