/** * @param string|TokenNode $name * * @return $this */ public function setName($name) { if (is_string($name)) { $name = Token::identifier($name); } $this->name->replaceWith($name); return $this; }
/** * @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; }
/** * @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; }
/** * Sets the imported item's alias. If NULL is passed, the alias is removed. * * @param \Pharborist\TokenNode|string|NULL $alias * * @return $this */ public function setAlias($alias) { if (is_string($alias)) { $alias = new TokenNode(T_STRING, $alias); } if ($alias instanceof TokenNode) { if ($this->hasAlias()) { $this->alias->replaceWith($alias); } else { $this->alias = $alias; $this->addChild(WhitespaceNode::create(' ')); $this->addChild(Token::_as()); $this->addChild(WhitespaceNode::create(' ')); $this->addChild($alias, 'alias'); } } elseif ($alias === NULL && $this->hasAlias()) { $this->alias->previousUntil(Filter::isInstanceOf('\\Pharborist\\Namespaces\\NameNode'))->remove(); $this->alias->remove(); $this->alias = NULL; } else { throw new \InvalidArgumentException(); } return $this; }