public function testRewriteAccount()
 {
     $function_call = Parser::parseExpression('user_access("be exceptional", $account)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$account->hasPermission("be exceptional")', $rewritten->getText());
 }
示例#2
0
 /**
  * @param string $property
  *   Property name.
  * @return ClassMemberListNode
  */
 public static function create($property)
 {
     /** @var ClassNode $class_node */
     $class_node = Parser::parseSnippet("class Property {private \${$property};}");
     $property = $class_node->getStatements()[0]->remove();
     return $property;
 }
 public function testRewriteWithName()
 {
     $function_call = Parser::parseExpression('form_load_include($form_state, "inc", "mod_foo", "bazzle")');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$form_state->loadInclude("mod_foo", "inc", "bazzle")', $rewritten->getText());
 }
    public function testUseDeclarations()
    {
        $snippet = <<<'EOF'
namespace Test {
  use const Other\MY_CONST;
  use function Other\my_func;
  use Other\MyClass;
  use Other\OtherClass as Bind;
  class TestClass {}
}
EOF;
        /** @var NamespaceNode $namespace */
        $namespace = Parser::parseSnippet($snippet);
        $declarations = $namespace->getUseDeclarations();
        $this->assertCount(4, $declarations);
        $aliases = $namespace->getClassAliases();
        $this->assertCount(2, $aliases);
        $this->assertArrayHasKey('MyClass', $aliases);
        $this->assertEquals('\\Other\\MyClass', $aliases['MyClass']);
        $this->assertArrayHasKey('Bind', $aliases);
        $this->assertEquals('\\Other\\OtherClass', $aliases['Bind']);
        $class_node = $namespace->find(Filter::isClass('TestClass'))[0];
        $this->assertTrue($namespace->owns($class_node));
        $class_node = ClassNode::create('Dummy');
        $this->assertFalse($namespace->owns($class_node));
    }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('entity_create("node", $node_values)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::entityManager()->getStorage("node")->create($node_values)', $rewritten->getText());
 }
 public function testRewriteSpecificBin()
 {
     $function_call = Parser::parseExpression('cache_get("baz", "foo")');
     $rewritten = $this->getPlugin()->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::cache("foo")->get("baz")', $rewritten->getText());
 }
示例#7
0
 /**
  * @param string $method_name
  * @return InterfaceMethodNode
  */
 public static function create($method_name)
 {
     /** @var InterfaceNode $interface_node */
     $interface_node = Parser::parseSnippet("interface Method {public function {$method_name}();}");
     $method_node = $interface_node->getStatements()[0]->remove();
     return $method_node;
 }
 public function testRewriteNoVariablesUnknownSeverity()
 {
     $function_call = Parser::parseExpression('watchdog("foo", "Ba-zing!", NULL, WATCHDOG_FOO)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::logger("foo")->notice("Ba-zing!", [])', $rewritten->getText());
 }
示例#9
0
 /**
  * Create namespace declaration.
  *
  * @param NameNode|string $name
  *   Namespace path.
  *
  * @return NamespaceNode
  */
 public static function create($name)
 {
     $name = (string) $name;
     $name = ltrim($name, '\\');
     $namespace_node = Parser::parseSnippet("namespace {$name};");
     return $namespace_node;
 }
 public function testSetVisibilityTokenText()
 {
     /** @var ClassNode $class_node */
     $class_node = Parser::parseSnippet('class Foo { public function wrassle() {} }');
     /** @var ClassMethodNode $method */
     $method = $class_node->getStatements()[0];
     $method->setVisibility('private');
     $this->assertSame('private', $method->getVisibility()->getText());
     $method->setVisibility('protected');
     $this->assertSame('protected', $method->getVisibility()->getText());
     $method->setVisibility('public');
     $this->assertSame('public', $method->getVisibility()->getText());
     $method->setVisibility(T_PRIVATE);
     $this->assertSame('private', $method->getVisibility()->getText());
     $method->setVisibility(T_PROTECTED);
     $this->assertSame('protected', $method->getVisibility()->getText());
     $method->setVisibility(T_PUBLIC);
     $this->assertSame('public', $method->getVisibility()->getText());
     $method->setVisibility(Token::_private());
     $this->assertSame('private', $method->getVisibility()->getText());
     $method->setVisibility(Token::_protected());
     $this->assertSame('protected', $method->getVisibility()->getText());
     $method->setVisibility(Token::_public());
     $this->assertSame('public', $method->getVisibility()->getText());
 }
 public function execute()
 {
     $ns = $this->extractNS($this->configuration['class']);
     $class = $this->extractLocal($this->configuration['class']);
     $doc = RootNode::create($ns);
     $ns = $doc->getNamespace($ns);
     Token::newline()->insertBefore($ns);
     Token::newline()->appendTo($ns);
     $class = ClassNode::create($class);
     if ($parent = $this->configuration['parent']) {
         Parser::parseSnippet('use ' . ltrim($parent, '\\') . ';')->appendTo($ns)->after(Token::newline());
         $class->setExtends($this->extractLocal($parent));
     }
     $interfaces = (array) $this->configuration['interfaces'];
     foreach ($interfaces as $interface) {
         Parser::parseSnippet('use ' . ltrim($interface, '\\') . ';')->appendTo($ns)->after(Token::newline());
     }
     $class->setImplements(array_map([$this, 'extractLocal'], $interfaces));
     if (isset($this->configuration['doc'])) {
         $class->setDocComment(DocCommentNode::create($this->configuration['doc']));
     }
     $class->appendTo($ns)->before(Token::newline());
     $destination = $this->getUnaliasedPath($this->configuration['destination']);
     $dir = subStr($destination, 0, strrPos($destination, '/'));
     $this->fs->mkdir($dir);
     file_put_contents($destination, $doc->getText());
     // Need to store the class' local name as its index identifier because
     // \Pharborist\Filter::isClass() doesn't support lookup by qualified path.
     $this->target->getIndexer('class')->addFile($destination);
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('drupal_get_title()');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::service(\'title_resolver\')->getTitle(\\Drupal::request(), \\Drupal::routeMatch()->getRouteObject())', $rewritten->getText());
 }
示例#13
0
function formatFile($filename)
{
    $formatter = FormatterFactory::getDefaultFormatter();
    $tree = Parser::parseFile($filename);
    $formatter->format($tree);
    file_put_contents($filename, $tree->getText());
}
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('form_set_value($element, $value, $form_state)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$form_state->setValueForElement($element, $value)', $rewritten->getText());
 }
 public function testRewriteInsert()
 {
     $function_call = Parser::parseExpression('drupal_write_record("foobar", $record)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::database()->insert("foobar")->fields($record)->execute()', $rewritten->getText());
 }
    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());
    }
 public function testRewriteEntityType()
 {
     $function_call = Parser::parseExpression('entity_get_info("node")');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::entityManager()->getDefinition("node")', $rewritten->getText());
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('drupal_is_cli()');
     $rewritten = $this->getPlugin()->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\ExpressionNode', $rewritten);
     $this->assertEquals('(PHP_SAPI === "cli")', $rewritten->getText());
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('ctools_object_cache_get("foo", "baz")');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::service(\'user.tempstore\')->get("baz")', $rewritten->getText());
 }
 public function testStringKeyAndExtractableDefaultValue()
 {
     $function_call = Parser::parseExpression('variable_get("foo_wambooli", 30)');
     $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());
 }
 public function testRewriteFieldType()
 {
     $function_call = Parser::parseExpression('field_info_field_types("text")');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::service(\'plugin.manager.field.field_type\')->getDefinition("text")', $rewritten->getText());
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('form_state_values_clean($form_state)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$form_state->cleanValues()', $rewritten->getText());
 }
 public function testRewriteDisplayOptions()
 {
     $function_call = Parser::parseExpression('field_view_field("node", $node, "field_foo", array("type" => "some_formatter"), $langcode)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$node->field_foo->view(array("type" => "some_formatter"))', $rewritten->getText());
 }
 public function testRewriteWithArguments()
 {
     $function_call = Parser::parseExpression('module_invoke_all("menu_alter", $menu)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::moduleHandler()->invokeAll("menu_alter", [$menu])', $rewritten->getText());
 }
 public function testRewriteSpecificBinWithExpiration()
 {
     $function_call = Parser::parseExpression('cache_set("foo", array(), "bar", 67890)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::cache("bar")->set("foo", array(), 67890)', $rewritten->getText());
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('field_update_field($field)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$field->save()', $rewritten->getText());
 }
 public function testCreate()
 {
     $expr = Parser::parseExpression('empty($foo)');
     $not = BooleanNotNode::fromExpression($expr);
     $this->assertInstanceOf('\\Pharborist\\Operators\\BooleanNotNode', $not);
     $this->assertSame($expr, $not->getOperand());
     $this->assertEquals('!empty($foo)', $not->getText());
 }
 public function testVisibility()
 {
     /** @var ClassNode $class_node */
     $class_node = Parser::parseSnippet('class Foo { public $bar; }');
     /** @var ClassMemberNode $a */
     $a = $class_node->getProperties()[0];
     $this->assertEquals('private $bar;', $a->setVisibility('private')->parent()->parent()->getText());
 }
示例#29
0
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('st("I translate thee!")');
     /** @var \Pharborist\Functions\FunctionCallNode $rewritten */
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertSame($function_call, $rewritten);
     $this->assertEquals('t("I translate thee!")', $rewritten->getText());
 }
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('drupal_map_assoc(array(0, 1, 2, 3))');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Functions\\FunctionCallNode', $rewritten);
     $this->assertSame($rewritten, $function_call);
     $this->assertEquals('array_combine(array(0, 1, 2, 3), array(0, 1, 2, 3))', $rewritten->getText());
 }