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 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());
 }
 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());
 }
 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 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());
 }
 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('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 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 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 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 testRewriteWithoutCacheReset()
 {
     $function_call = Parser::parseExpression('user_load(30)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::entityManager()->getStorage(\'user\')->load(30)', $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 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 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 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 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 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 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());
 }
 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 testRewriteArbitraryKeyAsSetter()
 {
     /** @var \Pharborist\Operators\AssignNode $expr */
     $expr = Parser::parseExpression('$form_state["foo"]["baz"] = "bar"');
     $rewritten = $this->plugin->rewriteAsSetter($expr->getLeftOperand(), 'foo', $expr);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('$form_state->set(["foo", "baz"], "bar")', $rewritten->getText());
 }
 public function testClearArguments()
 {
     /** @var \Pharborist\Functions\FunctionCallNode $call */
     $call = Parser::parseExpression('foo(42)');
     $this->assertCount(1, $call->getArguments());
     $call->clearArguments();
     $this->assertTrue($call->getArguments()->isEmpty());
 }
 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 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());
 }
예제 #24
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());
 }
 /**
  * @depends testGetRootProperty
  */
 public function testGetPropertyName()
 {
     /** @var ObjectPropertyNode $property */
     $property = Parser::parseExpression('$node->field_foo');
     $this->assertEquals('field_foo', $property->getPropertyName());
     $property = Parser::parseExpression('$node->$field["value"]');
     $this->assertNull($property->getPropertyName());
 }
예제 #26
0
 public function testRewrite()
 {
     $function_call = Parser::parseExpression('get_t()');
     /** @var \Pharborist\Types\StringNode $rewritten */
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Types\\StringNode', $rewritten);
     $this->assertEquals('t', $rewritten->toValue());
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     $property = $arguments[2] instanceof StringNode ? $arguments[2]->toValue() : clone $arguments[2];
     $rewritten = ObjectMethodCallNode::create(Parser::parseExpression($arguments[1] . '->' . $property), 'view');
     if (sizeof($arguments) >= 4) {
         $rewritten->appendArgument(clone $arguments[3]);
     }
     return $rewritten;
 }
 public function testRewriteArgument()
 {
     $function_call = Parser::parseExpression('theme_get_registry(FALSE)');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::service(\'theme.registry\')->getRuntime()', $rewritten->getText());
     $function_call = Parser::parseExpression('theme_get_registry("foo")');
     $rewritten = $this->plugin->rewrite($function_call, $this->target);
     $this->assertInstanceOf('\\Pharborist\\Objects\\ObjectMethodCallNode', $rewritten);
     $this->assertEquals('\\Drupal::service(\'theme.registry\')->get()', $rewritten->getText());
 }
 public function testGetRoot()
 {
     /** @var ArrayLookupNode $lookup */
     $lookup = Parser::parseExpression('$foo["bar"]["baz"][0]');
     $this->assertInstanceOf('\\Pharborist\\ArrayLookupNode', $lookup);
     $root = $lookup->getRootArray();
     $this->assertInstanceOf('\\Pharborist\\Variables\\VariableNode', $root);
     $this->assertEquals('$foo', $root->getText());
     $lookup = Parser::parseExpression('foo()["bar"]');
     $this->assertInstanceOf('\\Pharborist\\ArrayLookupNode', $lookup);
     $root = $lookup->getRootArray();
     $this->assertInstanceOf('\\Pharborist\\Functions\\FunctionCallNode', $root);
     $this->assertEquals('foo', $root->getName()->getText());
 }
예제 #30
0
 /**
  * {@inheritdoc}
  */
 public function convert(TargetInterface $target)
 {
     foreach ($this->configuration['function_calls'] as $function => $replace_with) {
         $function_calls = $target->getIndexer('function_call')->get($function);
         foreach ($function_calls as $function_call) {
             $rewritten = str_ireplace($function, $replace_with, $function_call->getText());
             $node = Parser::parseExpression($rewritten);
             $function_call->replaceWith($node);
             $target->save($node);
         }
     }
     // Flush other open syntax trees to ensure that other plugins don't clobber
     // our changes later.
     $target->flush();
     foreach ($target->getFinder() as $file) {
         // Load in the entire contents of the module. This is criminally inefficient
         // and wasteful of memory and should eventually be refactored into something
         // a little more...I dunno, sustainable.
         /** @var \Symfony\Component\Finder\SplFileInfo $file */
         $search = array_keys($this->targets);
         $replace = array_values($this->targets);
         file_put_contents($file->getPathname(), str_replace($search, $replace, $file->getContents()));
     }
 }