Beispiel #1
0
 /**
  * @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;
 }
Beispiel #2
0
 /**
  * Determine if this node belongs to namespace.
  *
  * @param string|NamespaceNode $ns
  *   Either the absolute namespace path or a NamespaceNode.
  *
  * @return boolean
  *   TRUE if the node belongs to the given namespace.
  */
 public function inNamespace($ns)
 {
     if (is_string($ns)) {
         $namespace_node = $this->name->getNamespace();
         $namespace = $namespace_node === NULL ? '' : $namespace_node->getName()->getAbsolutePath();
         return $ns === $namespace;
     } elseif ($ns instanceof NamespaceNode) {
         return $this->name->getNamespace() === $ns;
     } else {
         throw new \InvalidArgumentException();
     }
 }
Beispiel #3
0
 /**
  * Create namespace path.
  *
  * @param string $name
  * @return NameNode
  */
 public static function create($name)
 {
     $parts = explode('\\', $name);
     $name_node = new NameNode();
     foreach ($parts as $i => $part) {
         $part = trim($part);
         if ($i > 0) {
             $name_node->append(Token::namespaceSeparator());
         }
         if ($part !== '') {
             $name_node->append(Token::identifier($part));
         }
     }
     return $name_node;
 }
Beispiel #4
0
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * 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;
 }
Beispiel #6
0
 /**
  * Name bounded inside namespace.
  *
  * @return string
  */
 public function getBoundedName()
 {
     if ($this->alias) {
         return $this->alias->getText();
     } else {
         return $this->name->lastChild()->getText();
     }
 }
Beispiel #7
0
 /**
  * @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;
 }
Beispiel #8
0
 /**
  * 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;
 }
Beispiel #9
0
 /**
  * Get the type of the parameter as defined by type hinting or doc comment.
  *
  * @return string[]
  *   The types as defined by phpdoc standard. Default is ['mixed'].
  */
 public function getTypes()
 {
     // If type hint is set then that is the type of the parameter.
     if ($this->typeHint) {
         if ($this->typeHint instanceof TokenNode) {
             if ($this->typeHint->getType() === T_ARRAY) {
                 $docTypes = $this->getDocTypes();
                 foreach ($docTypes as $docType) {
                     if ($docType !== 'array' && substr($docType, -2) !== '[]') {
                         return [$this->typeHint->getText()];
                     }
                 }
                 return $docTypes;
             } else {
                 return [$this->typeHint->getText()];
             }
         } else {
             return [$this->typeHint->getAbsolutePath()];
         }
     }
     return $this->getDocTypes();
 }
Beispiel #10
0
 /**
  * @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());
 }
Beispiel #12
0
 /**
  * @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;
 }