/**
     * {@inheritdoc}
     */
    public function convert(TargetInterface $target)
    {
        /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
        $function = $target->getIndexer('function')->get('hook_user_login');
        // The $edit parameter is defunct in Drupal 8, but we'll leave it in
        // there as an empty array to prevent errors, and move it to the back
        // of the line.
        /** @var \Pharborist\Functions\ParameterNode $edit */
        $edit = $function->getParameterList()->shift()->setReference(FALSE)->setValue(ArrayNode::create([]));
        $function->appendParameter($edit);
        // Slap a FIXME on the hook implementation, informing the developer that
        // $edit and $category are dead.
        $comment = $function->getDocComment();
        $comment_text = $comment ? $comment->getCommentText() : '';
        if ($comment_text) {
            $comment_text .= "\n\n";
        }
        $comment_text .= <<<'END'
@FIXME
The $edit parameter is gone in Drupal 8. It has been left here in order to
prevent 'undefined variable' errors, but it will never actually be passed to
this hook. You'll need to modify this function and remove every reference to it.
END;
        $function->setDocComment(DocCommentNode::create($comment_text));
        $rewriter = $this->rewriters->createInstance('_rewriter:user');
        $this->rewriteFunction($rewriter, $function->getParameterAtIndex(0), $target);
        $target->save($function);
    }
 /**
  * {@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)
 {
     $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();
     // 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);
 }
Exemple #5
0
 private function updateArray(ArrayNode $node, $data)
 {
     $i = 0;
     foreach ($node->getElements() as $el) {
         if ($el instanceof ArrayPairNode) {
             $k = (string) $el->getKey();
             $k = trim($k, '"\'');
             $v = $el->getValue();
             if (!isset($data[$k])) {
                 $this->cleanAround($el);
             } else {
                 if ($v instanceof ArrayNode && is_array($data[$k])) {
                     $v = $this->updateArray($v, $data[$k]);
                     unset($data[$k]);
                 } else {
                     $v->replaceWith(Parser::parseExpression($data[$k]));
                     unset($data[$k]);
                 }
             }
         } elseif ($el instanceof ArrayNode && (!isset($data[$i]) || is_array($data[$i]))) {
             if (!isset($data[$i])) {
                 $this->cleanAround($el);
             } else {
                 $this->updateArray($el, $data[$i]);
                 unset($data[$i]);
                 $i++;
             }
         } else {
             if (!isset($data[$i])) {
                 $this->cleanAround($el);
             } else {
                 $el->replaceWith(Parser::parseExpression($data[$i]));
                 unset($data[$i]);
                 $i++;
             }
         }
     }
     foreach ($data as $key => $val) {
         $v = Parser::parseExpression(self::var_codify($val));
         if (!is_integer($key)) {
             $v = ArrayPairNode::create(Node::fromValue($key), $v);
         }
         $comma = false;
         $list = $node->getElementList();
         $children = [];
         foreach ($list->children() as $child) {
             $children[] = $child;
         }
         $prev = end($children);
         if ($prev) {
             do {
                 if ((string) $prev === ',') {
                     $comma = true;
                     break;
                 }
             } while (is_object($prev) && ($prev = $prev->previous()) instanceof WhitespaceNode);
         } else {
             $comma = true;
         }
         $indent = 0;
         $prev = end($children);
         while ($prev && strpos($prev, "\n") === false) {
             $prev = $prev->previous();
         }
         $indent = '';
         if ($prev) {
             $prev = explode("\n", (string) $prev);
             $prev = array_pop($prev);
             for ($i = 0; $i < strlen($prev); $i++) {
                 if (in_array($prev[$i], ["\t", ' '])) {
                     $indent .= $prev[$i];
                 } else {
                     break;
                 }
             }
         }
         if (!$comma) {
             $list->append(Token::comma());
         }
         $list->append(Token::newline());
         if ($indent) {
             $list->append(WhitespaceNode::create($indent));
         }
         $list->append($v);
     }
 }
Exemple #6
0
 public function endArrayNode(ArrayNode $node)
 {
     $nested = $this->nodeData[$node];
     unset($this->nodeData[$node]);
     if ($nested) {
         $this->indentLevel--;
     }
     $is_wrapped = $node->getElementList()->children(Filter::isNewline())->isNotEmpty();
     if ($is_wrapped) {
         // Enforce trailing comma after last element.
         $node->getElementList()->append(Token::comma());
         // Newline before closing ) or ].
         $this->newlineBefore($node->lastChild(), !$nested);
     }
 }
Exemple #7
0
 /**
  * Parse static array pair list.
  * @param ArrayNode $node Array node to add elements to
  * @param int|string $terminator Token type that terminates the array pair list
  */
 private function staticArrayPairList(ArrayNode $node, $terminator)
 {
     $elements = new CommaListNode();
     do {
         if ($this->currentType === $terminator) {
             break;
         }
         $this->matchHidden($elements);
         $value = $this->staticScalar();
         if ($this->currentType === T_DOUBLE_ARROW) {
             $pair = new ArrayPairNode();
             $pair->addChild($value, 'key');
             $this->mustMatch(T_DOUBLE_ARROW, $pair);
             $pair->addChild($this->staticScalar(), 'value');
             $elements->addChild($pair);
         } else {
             $elements->addChild($value);
         }
     } while ($this->tryMatch(',', $elements, NULL, TRUE));
     $node->addChild($elements, 'elements');
 }
 /**
  * {@inheritdoc}
  */
 public function rewriteAsSetter(ExpressionNode $expr, $property, AssignNode $assignment)
 {
     /** @var \Pharborist\ArrayLookupNode $expr */
     $object = clone $expr->getRootArray();
     $keys = $expr->getKeys();
     $value = clone $assignment->getRightOperand();
     // $form_state['values']['baz'] = 'foo' --> $form_state->setValue(['baz'], 'foo')
     if ($property == 'values') {
         array_shift($keys);
         return ObjectMethodCallNode::create($object, 'setValue')->appendArgument(ArrayNode::create($keys))->appendArgument($value);
     } elseif (isset($this->pluginDefinition['properties'][$property]['set'])) {
         return parent::rewriteAsSetter($expr, $property, $assignment);
     } else {
         return ObjectMethodCallNode::create($object, 'set')->appendArgument(ArrayNode::create($keys))->appendArgument($value);
     }
 }
Exemple #9
0
 /**
  * Creates a Node from a php value.
  *
  * @param string|integer|float|boolean|array|null $value
  *  The value to create a node for.
  *
  * @return FloatNode|IntegerNode|StringNode|BooleanNode|NullNode|ArrayNode
  *
  * @throws \InvalidArgumentException if $value is not a scalar.
  */
 public static function fromValue($value)
 {
     if (is_array($value)) {
         $elements = [];
         foreach ($value as $k => $v) {
             $elements[] = ArrayPairNode::create(static::fromValue($k), static::fromValue($v));
         }
         return ArrayNode::create($elements);
     } elseif (is_string($value)) {
         return StringNode::create(var_export($value, TRUE));
     } elseif (is_integer($value)) {
         return new IntegerNode(T_LNUMBER, $value);
     } elseif (is_float($value)) {
         return new FloatNode(T_DNUMBER, $value);
     } elseif (is_bool($value)) {
         return BooleanNode::create($value);
     } elseif (is_null($value)) {
         return NullNode::create('NULL');
     } else {
         throw new \InvalidArgumentException();
     }
 }