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());
 }
 /**
  * @return \Pharborist\Objects\ClassMethodNode
  */
 protected function addMethod(FunctionDeclarationNode $function, ClassNode $class, $alias = NULL)
 {
     $method = ClassMethodNode::fromFunction($function);
     if ($alias) {
         $method->setName($alias);
     }
     $class->appendMethod($method);
     // Add the parameters required for FormInterface conformance.
     $parameters = $method->getParameters()->toArray();
     if (empty($parameters)) {
         $parameters[0] = ParameterNode::create('$form');
         $method->appendParameter($parameters[0]);
     }
     if (sizeof($parameters) == 1) {
         $parameters[1] = ParameterNode::create('$form_state');
         $method->appendParameter($parameters[1]);
     }
     // The $form parameter must have the array type hint.
     $parameters[0]->setTypeHint('array');
     // The form state is never passed by reference.
     $parameters[1]->setReference(FALSE);
     // Additional parameters MUST have a default value of NULL in order to conform
     // to FormInterface.
     for ($i = 2; $i < sizeof($parameters); $i++) {
         $parameters[$i]->setValue(new TokenNode(T_STRING, 'NULL'));
     }
     $this->formStateRewriter->rewrite($parameters[1]);
     return $method;
 }
    public function testRewriteIsset()
    {
        $code = <<<'END'
function foo($baz) {
  if (isset($baz->title)) {
  }
}
END;
        /** @var \Pharborist\Functions\FunctionDeclarationNode $func */
        $func = Parser::parseSnippet($code);
        $this->plugin->rewrite($func->getParameterAtIndex(0));
        $expected = <<<'END'
function foo($baz) {
  if (!$baz->getTitle()) {
  }
}
END;
        $this->assertEquals($expected, $func->getText());
    }
 /**
  * Parametrically rewrites a function.
  *
  * @param \Drupal\drupalmoduleupgrader\RewriterInterface $rewriter
  *  A fully configured parametric rewriter.
  * @param \Pharborist\Functions\ParameterNode $parameter
  *  The parameter upon which to base the rewrite.
  * @param TargetInterface $target
  *  The target module.
  * @param boolean $recursive
  *  If TRUE, rewriting will recurse into called functions which are passed
  *  the rewritten parameter as an argument.
  */
 protected function rewriteFunction(RewriterInterface $rewriter, ParameterNode $parameter, TargetInterface $target, $recursive = TRUE)
 {
     $rewriter->rewrite($parameter);
     $target->save($parameter);
     // Find function calls within the rewritten function which are called
     // with the rewritten parameter.
     $indexer = $target->getIndexer('function');
     $next = $parameter->getFunction()->find(new FunctionCallArgumentFilter($parameter->getName()))->filter(function (FunctionCallNode $call) use($indexer) {
         return $indexer->has($call->getName()->getText());
     });
     /** @var \Pharborist\Functions\FunctionCallNode $call */
     foreach ($next as $call) {
         /** @var \Pharborist\Functions\FunctionDeclarationNode $function */
         $function = $indexer->get($call->getName()->getText());
         foreach ($call->getArguments() as $index => $argument) {
             if ($argument instanceof VariableNode && $argument->getName() == $parameter->getName()) {
                 $this->rewriteFunction($rewriter, $function->getParameterAtIndex($index), $target, $recursive);
                 break;
             }
         }
     }
 }