/**
     * Helper for subclasses' rewrite() methods. This checks if the call can
     * be rewritten at all and leaves a FIXME if it can't. If the variable's
     * key is not a string starting with MODULE_, the call will not be
     * considered rewritable.
     *
     * @return boolean
     */
    protected function tryRewrite(FunctionCallNode $call, TargetInterface $target)
    {
        $statement = $call->getStatement();
        $arguments = $call->getArguments();
        if ($arguments[0] instanceof StringNode) {
            $key = $arguments[0]->toValue();
            if (strPos($key, $target->id() . '_') === 0) {
                return TRUE;
            } else {
                $comment = <<<END
This looks like another module's variable. You'll need to rewrite this call
to ensure that it uses the correct configuration object.
END;
                $this->buildFixMe($comment)->prependTo($statement);
                return FALSE;
            }
        } else {
            $comment = <<<END
The correct configuration object could not be determined. You'll need to
rewrite this call manually.
END;
            $this->buildFixMe($comment)->prependTo($statement);
            return FALSE;
        }
    }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     if (sizeof($arguments) == 1) {
         return ObjectMethodCallNode::create(clone $arguments[0], 'save');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     $get = ClassMethodCallNode::create('\\Drupal', 'cache');
     if (sizeof($arguments) == 2) {
         $get->appendArgument(clone $arguments[1]);
     }
     return $get->appendMethodCall('get')->appendArgument(clone $arguments[0]);
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments()->toArray();
     $invoke = ClassMethodCallNode::create('\\Drupal', 'moduleHandler')->appendMethodCall('invoke')->appendArgument(array_shift($arguments)->remove())->appendArgument(array_shift($arguments)->remove());
     if ($arguments) {
         $invoke->appendArgument(ArrayNode::create($arguments));
     }
     return $invoke;
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments()->toArray();
     array_shift($arguments);
     if (sizeof($arguments) == 3) {
         array_pop($arguments);
     }
     return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('user.tempstore')->appendMethodCall('set')->appendArgument($arguments[0])->appendArgument($arguments[1]);
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     $rewritten = ObjectMethodCallNode::create(clone $arguments[0], 'loadInclude')->appendArgument(clone $arguments[2])->appendArgument(clone $arguments[1]);
     if (sizeof($arguments) == 4) {
         $rewritten->appendArgument(clone $arguments[3]);
     }
     return $rewritten;
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     $rewritten = ObjectMethodCallNode::create(clone $arguments[3], 'view');
     if (sizeof($arguments) >= 5) {
         $rewritten->appendArgument(clone $arguments[4]);
     }
     return $rewritten;
 }
 /**
  * {@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;
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     if ($arguments[0] instanceof StringNode) {
         $handler_type = $arguments[0]->toValue();
         if ($handler_type == 'validate' || $handler_type == 'submit') {
             return ClassMethodCallNode::create('\\Drupal', 'formBuilder')->appendMethodCall('execute' . ucFirst($handler_type) . 'Handlers')->appendArgument(clone $arguments[1])->appendArgument(clone $arguments[2]);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $manager = ClassMethodCallNode::create('\\Drupal', 'entityManager');
     $arguments = $call->getArguments();
     if ($arguments->isEmpty()) {
         return $manager->appendMethodCall('getDefinitions');
     } elseif (sizeof($arguments) == 1) {
         return $manager->appendMethodCall('getDefinition')->appendArgument(clone $arguments[0]);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $replacement = ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('plugin.manager.field.formatter');
     $arguments = $call->getArguments();
     if ($arguments->isEmpty()) {
         return $replacement->appendMethodCall('getDefinitions');
     } elseif (sizeof($arguments) == 1) {
         return $replacement->appendMethodCall('getDefinition')->appendArgument(clone $arguments[0]);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     // Change function name to array_combine().
     $call->setName('array_combine');
     // Duplicate the first $array argument twice (silly, but true).
     // Need to clone the argument to make a copy of it, since Pharborist works
     // on original tree elements.
     $arguments = $call->getArguments();
     return $call->appendArgument(clone $arguments[0]);
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     if (isset($arguments[1]) && $arguments[1] instanceof VariableNode) {
         $rewritten = ObjectMethodCallNode::create(clone $arguments[1], 'hasPermission');
     } else {
         $rewritten = ClassMethodCallNode::create('\\Drupal', 'currentUser')->appendMethodCall('hasPermission');
     }
     return $rewritten->appendArgument(clone $arguments[0]);
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $rewritten = ClassMethodCallNode::create('\\Drupal', 'database');
     $arguments = $call->getArguments();
     if (sizeof($arguments) == 3) {
         $key = $arguments[2] instanceof StringNode ? ArrayNode::create([clone $arguments[2]]) : clone $arguments[2];
         return $rewritten->appendMethodCall('merge')->appendArgument(clone $arguments[0])->appendMethodCall('fields')->appendArgument(clone $arguments[1])->appendMethodCall('key')->appendArgument($key)->appendMethodCall('execute');
     } else {
         return $rewritten->appendMethodCall('insert')->appendArgument(clone $arguments[0])->appendMethodCall('fields')->appendArgument(clone $arguments[1])->appendMethodCall('execute');
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     // If there were three arguments, the call is affecting the internal
     // comment_load() cache. Unfortunately, it's pretty much impossible to
     // reliably determine whether or not they wanted to reset the cache,
     // so let's just leave a FIXME.
     if (sizeof($arguments) == 2) {
         $this->buildFixMe('To reset the comment cache, use EntityStorageInterface::resetCache().')->insertBefore($call);
     }
     return ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument('comment')->appendMethodCall('load')->appendArgument(clone $arguments[0]);
 }
Esempio n. 16
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     if ($arguments[1] instanceof StringNode) {
         // Create a call to url() and let the parent class rewrite it like normal,
         // so we don't have to duplicate that code.
         $url = Parser::parseSnippet('url(' . $arguments[1] . ');')->firstChild();
         $url_rewritten = parent::rewrite($url, $target);
         if ($url_rewritten) {
             return ClassMethodCallNode::create('\\Drupal', 'l')->appendArgument($arguments[0])->appendArgument($url_rewritten);
         }
     }
 }
Esempio n. 17
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     if ($arguments[0] instanceof StringNode) {
         $path = $arguments[0]->toValue();
         // If the URL has a scheme (e.g., http://), it's external.
         if (parse_url($path, PHP_URL_SCHEME)) {
             return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromUri')->appendArgument(clone $arguments[0]);
         } elseif ($this->routeExists($path)) {
             $route = $this->routeProvider->getRoutesByPattern('/' . $path)->getIterator()->key();
             return ClassMethodCallNode::create('\\Drupal\\Core\\Url', 'fromRoute')->appendArgument(StringNode::fromValue($route));
         }
     }
 }
Esempio n. 18
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     $cache = ClassMethodCallNode::create('\\Drupal', 'cache');
     if (sizeof($arguments) > 2) {
         $cache->appendArgument(clone $arguments[2]);
     }
     $set = $cache->appendMethodCall('set')->appendArgument(clone $arguments[0])->appendArgument(clone $arguments[1]);
     // Include the expiration time, if given.
     if (sizeof($arguments) == 4) {
         $set->appendArgument(clone $arguments[3]);
     }
     return $set;
 }
Esempio n. 19
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     // If there were three arguments, the call is affecting the internal
     // node_load() cache. Unfortunately, it's pretty much impossible to
     // reliably determine whether or not they wanted to reset the cache,
     // so let's just leave a FIXME.
     if (sizeof($arguments) == 3) {
         $this->buildFixMe('To reset the node cache, use EntityStorageInterface::resetCache().')->insertBefore($call);
     }
     $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument('node');
     // If there's more than one argument, a revision ID was passed, which
     // means we call loadRevision($nid). Otherwise, call load($nid).
     if (sizeof($arguments) > 1) {
         return $rewritten->appendMethodCall('loadRevision')->appendArgument(clone $arguments[1]);
     } else {
         return $rewritten->appendMethodCall('load')->appendArgument(clone $arguments[0]);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     // If there were three arguments, the call is affecting the internal
     // entity cache. Unfortunately, it's pretty much impossible to reliably
     // determine whether or not they wanted to reset the cache, so let's just
     // leave a FIXME.
     if (sizeof($arguments) == 3) {
         $variables = ['!entity_type' => $this->pluginDefinition['entity_type']];
         $this->buildFixMe('To reset the !entity_type cache, use EntityStorageInterface::resetCache().', $variables)->insertBefore($call);
     }
     $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument($this->pluginDefinition['entity_type']);
     // If there's more than one argument, conditions were passed (not a
     // recommended practice, but modules might have done it anyway), in which
     // case we need to use loadByProperties(). Otherwise, loadMultiple().
     if (sizeof($arguments) > 1) {
         return $rewritten->appendMethodCall('loadByProperties')->appendArgument(clone $arguments[1]);
     } else {
         return $rewritten->appendMethodCall('loadMultiple')->appendArgument(clone $arguments[0]);
     }
 }
Esempio n. 21
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     // We'll call a specific method on the logger object, depending on the
     // severity passed in the original function call (if any). If there are
     // at least four arguments, a severity was passed. We check $arguments[3]
     // to ensure it's a valid severity constant, and if it's not, we default
     // to the notice() severity.
     //
     // @TODO Leave a FIXME for an invalid severity, since changing it to a
     // notice alters the intent of the original code.
     //
     if (sizeof($arguments) > 3 && $arguments[3] instanceof ConstantNode && in_array($arguments[3]->getConstantName()->getText(), static::$severityConstants)) {
         $method = strtolower(subStr($arguments[3], 9));
     } else {
         $method = 'notice';
     }
     // If there is a third argument, and it's an array, a context array
     // was passed.
     $context = sizeof($arguments) > 2 && $arguments[2] instanceof ArrayNode ? clone $arguments[2] : ArrayNode::create([]);
     return ClassMethodCallNode::create('\\Drupal', 'logger')->appendArgument(clone $arguments[0])->appendMethodCall($method)->appendArgument(clone $arguments[1])->appendArgument($context);
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     // If there were three arguments, the call is affecting the internal
     // entity cache. Unfortunately, it's pretty much impossible to reliably
     // determine whether or not they wanted to reset the cache, so let's just
     // leave a FIXME.
     if (sizeof($arguments) == 3) {
         $this->buildFixMe('To reset the entity cache, use EntityStorageInterface::resetCache().')->insertBefore($call);
     }
     $rewritten = ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument(clone $arguments[0]);
     // If there's a third argument, conditions were passed. Not a recommended
     // practice, but certain modules might have done it anyway. If we detect
     // conditions, use loadByProperties().
     if (sizeof($arguments) > 2) {
         return $rewritten->appendMethodCall('loadByProperties')->appendArgument(clone $arguments[2]);
     } else {
         $rewritten->appendMethodCall('load');
         if (sizeof($arguments) > 1 && $arguments[1] instanceof ArrayNode) {
             $rewritten->appendArgument(clone $arguments[1]);
         }
         return $rewritten;
     }
 }
Esempio n. 23
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     return $call->setName('t');
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments()->toArray();
     return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument(StringNode::fromValue('theme.registry'))->appendMethodCall($arguments && $arguments[0] instanceof FalseNode ? 'getRuntime' : 'get');
 }
 /**
  * Tests if the function call can be rewritten at all, which it will be
  * only if both arguments are strings, and the first argument is the machine
  * name of the target module.
  *
  * @param \Pharborist\Functions\FunctionCallNode $call
  *  The function call to test.
  * @param \Drupal\drupalmoduleupgrader\TargetInterface $target
  *  The target module.
  *
  * @return boolean
  */
 public function canRewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     return $arguments[0] instanceof StringNode && $arguments[0]->toValue() == $target->id() && $arguments[1] instanceof StringNode;
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     return ClassMethodCallNode::create('\\Drupal', 'service')->appendArgument('user.tempstore')->appendMethodCall('get')->appendArgument(clone $call->getArgumentList()->getItem(1));
 }
Esempio n. 27
0
 /**
  * Apply any function call, array and object deference.
  * @param Node $function_reference
  * @param bool $dynamic TRUE if the function call is dynamic
  * @return Node
  */
 private function functionCall(Node $function_reference, $dynamic = FALSE)
 {
     if ($dynamic) {
         $node = new CallbackCallNode();
         $node->addChild($function_reference, 'callback');
     } else {
         if ($function_reference instanceof NameNode && $function_reference->childCount() === 1 && $function_reference == 'define') {
             $node = new DefineNode();
         } else {
             $node = new FunctionCallNode();
         }
         $node->addChild($function_reference, 'name');
     }
     $this->functionCallParameterList($node);
     return $this->objectDereference($this->arrayDeference($node));
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     return ObjectMethodCallNode::create(clone $call->getArgumentList()->getItem(0), 'save');
 }
Esempio n. 29
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $table = $call->getArgumentList()->getItem(0);
     return $table instanceof StringNode && in_array($table->toValue(), self::$forbiddenTables) ? NULL : $call;
 }
 /**
  * {@inheritdoc}
  */
 public function rewrite(FunctionCallNode $call, TargetInterface $target)
 {
     $arguments = $call->getArguments();
     return ClassMethodCallNode::create('\\Drupal', 'entityManager')->appendMethodCall('getStorage')->appendArgument(clone $arguments[0])->appendMethodCall('create')->appendArgument(clone $arguments[1]);
 }