/** * Create a new NullNode. * * @param string $name * Parameter is ignored. * * @return NullNode */ public static function create($name = 'null') { $is_upper = FormatterFactory::getDefaultFormatter()->getConfig('boolean_null_upper'); $node = new NullNode(); $node->addChild(NameNode::create($is_upper ? 'NULL' : 'null'), 'constantName'); return $node; }
/** * @param string|NameNode|TokenNode $type_hint * @return $this */ public function setTypeHint($type_hint) { if (is_string($type_hint)) { $type = $type_hint; switch ($type) { case 'array': $type_hint = Token::_array(); break; case 'callable': $type_hint = Token::_callable(); break; default: $type_hint = NameNode::create($type); break; } } if ($type_hint) { if (isset($this->typeHint)) { $this->typeHint->replaceWith($type_hint); } else { $this->typeHint = $type_hint; $this->prepend([$this->typeHint, Token::space()]); } } else { $this->typeHint->remove(); } return $this; }
/** * Create a new TrueNode. * * @param boolean $boolean * Parameter is ignored. * * @return TrueNode */ public static function create($boolean = TRUE) { $is_upper = FormatterFactory::getDefaultFormatter()->getConfig('boolean_null_upper'); $node = new TrueNode(); $node->addChild(NameNode::create($is_upper ? 'TRUE' : 'true'), 'constantName'); return $node; }
/** * @param string|\Pharborist\Namespaces\NameNode $name * @return $this */ public function setName($name) { if (is_string($name)) { $name = NameNode::create($name); } $this->name->replaceWith($name); $this->name = $name; return $this; }
/** * @param string|NameNode $name * * @return static */ public static function create($name) { if (is_string($name)) { $name = NameNode::create($name); } /** @var ConstantNode $node */ $node = new static(); $node->addChild($name, 'constantName'); return $node; }
/** * Creates a method call on a class with an empty argument list. * * @param Node|string $class_name * The class node which is typically NameNode of class. * @param string $method_name * The name of the called method. * * @return static */ public static function create($class_name, $method_name) { if (is_string($class_name)) { $class_name = NameNode::create($class_name); } /** @var ClassMethodCallNode $node */ $node = new static(); $node->addChild($class_name, 'className'); $node->addChild(Token::doubleColon()); $node->addChild(Token::identifier($method_name), 'methodName'); $node->addChild(Token::openParen(), 'openParen'); $node->addChild(new CommaListNode(), 'arguments'); $node->addChild(Token::closeParen(), 'closeParen'); return $node; }
/** * @param string|\Pharborist\Namespaces\NameNode $extends * @return $this */ public function setExtends($extends) { if ($extends === NULL) { if (isset($this->extends)) { // Remove whitespace after extends keyword. $this->extends->previous()->remove(); // Remove extends keyword. $this->extends->previous()->remove(); // Remove whitespace before extends keyword. $this->extends->previous()->remove(); // Remove extends namespace. $this->extends->remove(); $this->extends = NULL; } } else { if (!is_array($extends)) { $extends = [$extends]; } $extendsList = new CommaListNode(); foreach ($extends as $extend) { if (is_string($extend)) { $extendsList->appendItem(NameNode::create($extend)); } elseif ($extend instanceof NameNode) { $extendsList->appendItem($extend); } else { throw new \InvalidArgumentException('Invalid $extends argument'); } } $extends = $extendsList; if (isset($this->extends)) { $this->extends->replaceWith($extends); } else { $this->name->after([Token::space(), Token::_extends(), Token::space(), $extends]); } $this->extends = $extends; } return $this; }
public function testCreate() { $namespace = NameNode::create('MyNamespace'); $this->assertCount(1, $namespace->children()); $this->assertEquals('MyNamespace', $namespace->firstChild()->getText()); $namespace = NameNode::create('Top\\Sub'); /** @var Node[] $children */ $children = $namespace->children(); $this->assertCount(3, $children); $this->assertEquals('Top', $children[0]->getText()); $this->assertEquals('\\', $children[1]->getText()); $this->assertEquals('Sub', $children[2]->getText()); $namespace = NameNode::create('\\Top\\Sub'); $children = $namespace->children(); $this->assertCount(4, $children); $this->assertEquals('\\', $children[0]->getText()); $this->assertEquals('Top', $children[1]->getText()); $this->assertEquals('\\', $children[2]->getText()); $this->assertEquals('Sub', $children[3]->getText()); }
/** * @param string|NameNode|CommaListNode|array|NULL $implements * @throws \InvalidArgumentException * @return $this */ public function setImplements($implements) { if ($implements === NULL) { if (isset($this->implements)) { // Remove whitespace after implements keyword. $this->implements->previous()->remove(); // Remove implements keyword $this->implements->previous()->remove(); // Remove whitespace before implements keyword. $this->implements->previous()->remove(); // Remove implements list. $this->implements->remove(); $this->implements = NULL; } } else { // Type conversions. if (is_string($implements)) { $implements = NameNode::create($implements); } if ($implements instanceof NameNode) { $implementList = new CommaListNode(); $implementList->append($implements); $implements = $implementList; } if (is_array($implements)) { $implementList = new CommaListNode(); foreach ($implements as $implement) { if (is_string($implement)) { $implementList->appendItem(NameNode::create($implement)); } elseif ($implement instanceof NameNode) { $implementList->appendItem($implement); } else { throw new \InvalidArgumentException('Invalid $implements argument'); } } $implements = $implementList; } // Set implements. if (isset($this->implements)) { $this->implements->replaceWith($implements); } else { $after = isset($this->extends) ? $this->extends : $this->name; $after->after([Token::space(), Token::_implements(), Token::space(), $implements]); } $this->implements = $implements; } return $this; }