public function testAddDocComment()
    {
        $original = <<<'EOF'
<?php
interface Test {
  public function test();
}
EOF;
        $expected = <<<'EOF'
<?php
interface Test {
  /**
   * Test
   */
  public function test();
}
EOF;
        $tree = Parser::parseSource($original);
        /** @var \Pharborist\Objects\InterfaceNode $interface */
        $interface = $tree->getStatements()[0];
        /** @var \Pharborist\Objects\InterfaceMethodNode $method */
        $method = $interface->getStatements()[0];
        $comment = DocCommentNode::create('Test');
        $method->setDocComment($comment);
        $this->assertEquals($expected, $tree->getText());
    }
 /**
  * @return \Pharborist\Objects\ClassNode
  */
 public function render()
 {
     if (empty($this->controller)) {
         $render = ['#theme' => 'dmu_form', '#module' => $this->target->id(), '#form_id' => $this->formID, '#class' => $this->toTitleCase($this->formID), '#config' => $this->isConfig];
         $source = \Drupal::service('renderer')->renderPlain($render);
         $this->controller = Parser::parseSource($source)->find(Filter::isClass($render['#class']))->get(0);
     }
     return $this->controller;
 }
    /**
     * @requires PHP 5.6
     */
    public function testIsVariadic()
    {
        $doc = <<<'END'
<?php
function foo($a, $b, ...$c) {
}
END;
        /** @var \Pharborist\Functions\FunctionDeclarationNode $func */
        $func = Parser::parseSource($doc)->children(Filter::isFunction('foo'))->get(0);
        $this->assertTrue($func->isVariadic());
        $doc = <<<'END'
<?php
function baz($a, $b, $c) {
}
END;
        $func = Parser::parseSource($doc)->children(Filter::isFunction('baz'))->get(0);
        $this->assertFalse($func->isVariadic());
    }
    public function testForeignStringKey()
    {
        $original = <<<'END'
<?php
variable_set('bar_wambooli', TRUE);
END;
        $expected = <<<'END'
<?php
// @FIXME
// This looks like another module's variable. You'll need to rewrite this call
// to ensure that it uses the correct configuration object.
variable_set('bar_wambooli', TRUE);
END;
        $snippet = Parser::parseSource($original);
        $function_call = $snippet->find(Filter::isFunctionCall('variable_set'))->get(0);
        $rewritten = $this->plugin->rewrite($function_call, $this->target);
        $this->assertNull($rewritten);
        $this->assertSame($expected, $snippet->getText());
    }
    public function testStringKeyAndUnextractableDefaultValue()
    {
        $original = <<<'END'
<?php
variable_get('foo_wambooli', array());
END;
        $expected = <<<'END'
<?php
// @FIXME
// Could not extract the default value because it is either indeterminate, or
// not scalar. You'll need to provide a default value in
// config/install/@module.settings.yml and config/schema/@module.schema.yml.
variable_get('foo_wambooli', array());
END;
        $snippet = Parser::parseSource($original);
        $function_call = $snippet->find(Filter::isFunctionCall('variable_get'))->get(0);
        $rewritten = $this->plugin->rewrite($function_call, $this->target);
        $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
        $this->assertEquals('\\Drupal::config(\'foo.settings\')->get(\'foo_wambooli\')', $rewritten->getText());
        $this->assertSame($expected, $snippet->getText());
    }
    /**
     * Test template file.
     */
    public function testTemplate()
    {
        $source = <<<'EOF'
<p>This is a template file</p>
<p>Hello, <?=$name?>. Welcome to <?=$lego . 'world'?>!</p>
<?php
code();
?><h1>End of template</h1><?php more_code();
EOF;
        $tree = Parser::parseSource($source);
        $this->assertEquals($source, $tree->getText());
        /** @var TemplateNode[] $templates */
        $templates = $tree->find(Filter::isInstanceOf('\\Pharborist\\TemplateNode'));
        $template = $templates[0];
        $this->assertEquals(5, $template->childCount());
        /** @var EchoTagStatementNode $echo_tag */
        $echo_tag = $template->firstChild()->next();
        $this->assertInstanceOf('\\Pharborist\\EchoTagStatementNode', $echo_tag);
        $this->assertEquals('<?=$name?>', $echo_tag->getText());
        $expressions = $echo_tag->getExpressions();
        $this->assertEquals('$name', $expressions[0]->getText());
        $template = $templates[1];
        $this->assertEquals('?><h1>End of template</h1><?php ', $template->getText());
    }
    public function testRoot()
    {
        $source = <<<'EOF'
<?php
use B\D, C\E as F;
test();
F();
new B();
new D();
new F();
B::foo();
D::foo();
my\foo();
B\foo();
A\B::foo();
new F\G();
new namespace\Level\MyClass();
EOF;
        /** @var RootNode $namespace */
        $root = Parser::parseSource($source);
        /** @var StatementNode[] $statements */
        $statements = $root->getStatements();
        /** @var ExpressionStatementNode $statement */
        $statement = $statements[1];
        /** @var FunctionCallNode $function_call */
        $function_call = $statement->getExpression();
        $name = $function_call->getName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('test', $name->getText());
        $this->assertEquals('\\test', $name->getAbsolutePath());
        $statement = $statements[2];
        $function_call = $statement->getExpression();
        $name = $function_call->getName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('F', $name->getText());
        $this->assertEquals('\\F', $name->getAbsolutePath());
        $statement = $statements[3];
        /** @var NewNode $new */
        $new = $statement->getExpression();
        $name = $new->getClassName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('B', $name->getText());
        $this->assertEquals('\\B', $name->getAbsolutePath());
        $statement = $statements[4];
        $new = $statement->getExpression();
        $name = $new->getClassName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('D', $name->getText());
        $this->assertEquals('\\B\\D', $name->getAbsolutePath());
        $statement = $statements[5];
        $new = $statement->getExpression();
        $name = $new->getClassName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('F', $name->getText());
        $this->assertEquals('\\C\\E', $name->getAbsolutePath());
        $statement = $statements[6];
        /** @var ClassMethodCallNode $class_method_call */
        $class_method_call = $statement->getExpression();
        $name = $class_method_call->getClassName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('B', $name->getText());
        $this->assertEquals('\\B', $name->getAbsolutePath());
        $statement = $statements[7];
        $class_method_call = $statement->getExpression();
        $name = $class_method_call->getClassName();
        $this->assertTrue($name->isUnqualified());
        $this->assertEquals('D', $name->getText());
        $this->assertEquals('\\B\\D', $name->getAbsolutePath());
        $statement = $statements[8];
        $function_call = $statement->getExpression();
        $name = $function_call->getName();
        $this->assertTrue($name->isQualified());
        $this->assertEquals('my\\foo', $name->getText());
        $this->assertEquals('\\my\\foo', $name->getAbsolutePath());
        $statement = $statements[9];
        $function_call = $statement->getExpression();
        $name = $function_call->getName();
        $this->assertTrue($name->isQualified());
        $this->assertEquals('B\\foo', $name->getText());
        $this->assertEquals('\\B\\foo', $name->getAbsolutePath());
        $statement = $statements[10];
        $class_method_call = $statement->getExpression();
        $name = $class_method_call->getClassName();
        $this->assertTrue($name->isQualified());
        $this->assertEquals('A\\B', $name->getText());
        $this->assertEquals('\\A\\B', $name->getAbsolutePath());
        $statement = $statements[11];
        $new = $statement->getExpression();
        $name = $new->getClassName();
        $this->assertTrue($name->isQualified());
        $this->assertEquals('F\\G', $name->getText());
        $this->assertEquals('\\C\\E\\G', $name->getAbsolutePath());
        $statement = $statements[12];
        $new = $statement->getExpression();
        $name = $new->getClassName();
        $this->assertTrue($name->isRelative());
        $this->assertEquals('namespace\\Level\\MyClass', $name->getText());
        $this->assertEquals('\\Level\\MyClass', $name->getAbsolutePath());
    }
    public function testGetTypes()
    {
        $source = <<<'EOF'
<?php
use MyNamespace\MyClass;
use MyNamespace\SomeClass as TestClass;

class Test {
  /**
   * A property using class.
   *
   * @var MyClass
   */
  private $a;

  /**
   * Property using class alias.
   *
   * @var TestClass
   */
  private $b;

  /**
   * An array property.
   *
   * @var array
   */
  private $data;

  /**
   * A callable property.
   *
   * @var callable
   */
  private $callback;

  /**
   * An integer property.
   *
   * @var int
   */
   private $num;

  /**
   * A generic property.
   */
  private $generic;
}
EOF;
        $tree = Parser::parseSource($source);
        /** @var ClassNode $class */
        $class = $tree->children(Filter::isInstanceOf('\\Pharborist\\Objects\\ClassNode'))[0];
        $properties = $class->getProperties();
        $this->assertEquals(['\\MyNamespace\\MyClass'], $properties[0]->getTypes());
        $this->assertEquals(['\\MyNamespace\\SomeClass'], $properties[1]->getTypes());
        $this->assertEquals(['array'], $properties[2]->getTypes());
        $this->assertEquals(['callable'], $properties[3]->getTypes());
        $this->assertEquals(['int'], $properties[4]->getTypes());
        $this->assertEquals(['mixed'], $properties[5]->getTypes());
    }
    /**
     * @requires PHP 5.6
     */
    public function testVariadic()
    {
        $source = <<<'EOF'
<?php
function foo($args) {
}
EOF;
        $tree = Parser::parseSource($source);
        /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
        $function = $tree->children(Filter::isInstanceOf('\\Pharborist\\Functions\\FunctionDeclarationNode'))[0];
        $parameter = $function->getParameter(0);
        $this->assertFalse($parameter->isVariadic());
        $parameter->setVariadic(TRUE);
        $this->assertTrue($parameter->isVariadic());
        $this->assertEquals('...$args', $parameter->getText());
    }
    public function testChainMethodCall()
    {
        $object = Token::variable('$object');
        $method_call = ObjectMethodCallNode::create($object, 'someMethod');
        $chained_call = $method_call->appendMethodCall('chained');
        $this->assertEquals('$object->someMethod()', $chained_call->getObject()->getText());
        $this->assertEquals('chained', $chained_call->getMethodName()->getText());
        $source = <<<'EOF'
<?php
$object->someMethod();
EOF;
        $tree = Parser::parseSource($source);
        /** @var ExpressionStatementNode $expr_statement */
        $expr_statement = $tree->firstChild()->next();
        /** @var ObjectMethodCallNode $method_call */
        $method_call = $expr_statement->getExpression();
        $method_call->appendMethodCall('chained');
        $expected = <<<'EOF'
<?php
$object->someMethod()->chained();
EOF;
        $this->assertEquals($expected, $tree->getText());
    }
예제 #11
0
    public function testRoot()
    {
        $source = <<<END
<?php
test();
END;
        $tree = Parser::parseSource($source);
        $node = $tree->firstChild()->next();
        $this->assertTrue($node->hasRoot());
        $this->assertSame($tree, $node->getRoot());
        $node = $this->createNode('orphan');
        $this->assertFalse($node->hasRoot());
        $this->assertNull($node->getRoot());
    }