Esempio n. 1
0
 /**
  * @param string|TokenNode $name
  *
  * @return $this
  */
 public function setName($name)
 {
     if (is_string($name)) {
         $name = Token::identifier($name);
     }
     $this->name->replaceWith($name);
     return $this;
 }
Esempio n. 2
0
 /**
  * Creates a method call on an object with an empty argument list.
  *
  * @param Node $object
  *  The expression that is an object.
  * @param string $method_name
  *  The name of the called method.
  *
  * @return static
  */
 public static function create(Node $object, $method_name)
 {
     /** @var ObjectMethodCallNode $node */
     $node = new static();
     $node->addChild($object, 'object');
     $node->addChild(Token::objectOperator(), 'operator');
     $node->addChild(Token::identifier($method_name), 'methodName');
     $node->addChild(Token::openParen(), 'openParen');
     $node->addChild(new CommaListNode(), 'arguments');
     $node->addChild(Token::closeParen(), 'closeParen');
     return $node;
 }
Esempio n. 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;
 }
Esempio n. 4
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;
 }
 public function testSetAlias()
 {
     /** @var \Pharborist\Namespaces\UseDeclarationBlockNode $declaration_block */
     $declaration_block = Parser::parseSnippet('use Foobar;');
     $declaration = $declaration_block->getDeclarationStatements()[0]->getDeclarations()[0];
     $this->assertFalse($declaration->hasAlias());
     $alias = Token::identifier('TestAlias');
     $declaration->setAlias($alias);
     $this->assertTrue($declaration->hasAlias());
     $this->assertEquals('TestAlias', $declaration->getAlias()->getText());
     $this->assertEquals('Foobar as TestAlias', $declaration->getText());
     $declaration->setAlias('Overridden');
     $this->assertTrue($declaration->hasAlias());
     $this->assertEquals('Overridden', $declaration->getAlias()->getText());
     $this->assertEquals('Foobar as Overridden', $declaration->getText());
     $declaration->setAlias(NULL);
     $this->assertFalse($declaration->hasAlias());
     $this->assertEquals('Foobar', $declaration->getText());
 }
 public function testIndex()
 {
     $first = Token::identifier('hello');
     $second = Token::identifier('world');
     $not_found = Token::identifier('notfound');
     $collection = new NodeCollection([$first, $second], FALSE);
     $this->assertEquals(0, $collection->indexOf(Filter::is($first)));
     $this->assertEquals(1, $collection->indexOf(Filter::is($second)));
     $this->assertEquals(-1, $collection->indexOf(Filter::is($not_found)));
 }