Example #1
0
 /**
  * @param Node $key
  *   Array element's key.
  * @param Node $value
  *   Array element's value.
  *
  * @return ArrayPairNode
  */
 public static function create($key, $value)
 {
     $node = new ArrayPairNode();
     $node->addChild($key, 'key');
     $node->addChild(Token::space());
     $node->addChild(Token::doubleArrow());
     $node->addChild(Token::space());
     $node->addChild($value, 'value');
     return $node;
 }
Example #2
0
 /**
  * @param boolean $is_static
  *
  * @return $this
  */
 public function setStatic($is_static)
 {
     if ($is_static) {
         if (!isset($this->static)) {
             $this->static = Token::_static();
             $this->visibility->after([Token::space(), $this->static]);
         }
     } else {
         if (isset($this->static)) {
             // Remove whitespace after static keyword.
             $this->static->next()->remove();
             // Remove static keyword.
             $this->static->remove();
         }
     }
     return $this;
 }
Example #3
0
 /**
  * @param boolean $is_static
  * @return $this
  */
 public function setStatic($is_static)
 {
     if ($is_static) {
         if (!isset($this->static)) {
             // Insert before T_FUNCTION.
             $function_token = $this->name->previous()->previous();
             $this->static = Token::_static();
             $function_token->before([$this->static, Token::space()]);
         }
     } else {
         if (isset($this->static)) {
             // Remove whitespace after static keyword.
             $this->static->next()->remove();
             // Remove static keyword.
             $this->static->remove();
         }
     }
     return $this;
 }
Example #4
0
 /**
  * @param string|integer|TokenNode|NULL $visibility
  * @return $this
  */
 public function setVisibility($visibility)
 {
     if ($visibility === NULL) {
         $this->removeVisibility();
     } else {
         if ($visibility === 'private' || $visibility === T_PRIVATE) {
             $visibility = Token::_private();
         } elseif ($visibility === 'protected' || $visibility === T_PROTECTED) {
             $visibility = Token::_protected();
         } elseif ($visibility === 'public' || $visibility === T_PUBLIC) {
             $visibility = Token::_public();
         }
         if (isset($this->visibility)) {
             $this->visibility->replaceWith($visibility);
         } else {
             /** @var \Pharborist\ParentNode $this */
             $this->prepend([$visibility, Token::space()]);
             $this->visibility = $visibility;
         }
     }
     return $this;
 }
Example #5
0
 /**
  * @param ExpressionNode|NULL $node
  * @return $this
  */
 public function setValue($node)
 {
     if ($node === NULL) {
         if (isset($this->value)) {
             $this->value->previousUntil(Filter::isInstanceOf('\\Pharborist\\Variables\\VariableNode'))->remove();
             $this->value->remove();
         }
     } else {
         if (isset($this->value)) {
             /** @var Node $node */
             $this->value->replaceWith($node);
         } else {
             $this->append([Token::space(), Token::assign(), Token::space(), $node]);
         }
     }
     return $this;
 }
Example #6
0
 /**
  * Set a single space after a node.
  *
  * @param Node $node
  *   Node to set space after.
  */
 protected function spaceAfter(Node $node)
 {
     $next = $node->nextToken();
     if ($next instanceof WhitespaceNode) {
         $next->setText(' ');
     } else {
         $node->after(Token::space());
     }
 }
Example #7
0
 /**
  * Creates an empty lexical variables list if it does not already exist.
  */
 protected function createLexicalVariables()
 {
     if (!$this->hasLexicalVariables()) {
         $this->lexicalUse = Token::_use();
         $this->lexicalOpenParen = Token::openParen();
         $this->lexicalVariables = new CommaListNode();
         $this->lexicalCloseParen = Token::closeParen();
         $this->closeParen->after([Token::space(), $this->lexicalUse, Token::space(), $this->lexicalOpenParen, $this->lexicalVariables, $this->lexicalCloseParen]);
     }
 }
Example #8
0
 /**
  * @param boolean $is_final
  * @return $this
  */
 public function setFinal($is_final)
 {
     if ($is_final) {
         if (!isset($this->final)) {
             $this->final = Token::_final();
             $this->prepend([$this->final, Token::space()]);
             $this->setAbstract(FALSE);
         }
     } else {
         if (isset($this->final)) {
             // Remove whitespace.
             $this->final->next()->remove();
             // Remove final.
             $this->final->remove();
         }
     }
     return $this;
 }
Example #9
0
 /**
  * Insert item before index.
  *
  * @param Node $item
  * @param int $index
  * @throws \OutOfBoundsException
  *   Index out of bounds.
  * @return $this
  */
 public function insertItem(Node $item, $index)
 {
     $items = $this->getItems();
     if ($items->isEmpty()) {
         if ($index !== 0) {
             throw new \OutOfBoundsException('index out of bounds');
         }
         $this->append($item);
     } else {
         $max_index = count($items) - 1;
         if ($index < 0 || $index > $max_index) {
             throw new \OutOfBoundsException('index out of bounds');
         }
         $items[$index]->before([$item, Token::comma(), Token::space()]);
     }
     return $this;
 }
Example #10
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;
 }
Example #11
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;
 }