public function testHasKey()
 {
     /** @var ArrayNode $array */
     $array = Parser::parseExpression('array("a", "b", "c")');
     $this->assertTrue($array->hasKey(0));
     $array = Parser::parseExpression('array("a" => "apple", "b" => "bear", "c" => "cauldron")');
     $this->assertTrue($array->hasKey('a'));
     $this->assertFalse($array->hasKey('d'));
     $array = Parser::parseExpression('array(0 => "foo", 1 => "baz", 2 => array(0 => "a", 1 => "b", 2 => "c"))');
     $this->assertTrue($array->hasKey(1));
     $this->assertFalse($array->hasKey('2'));
     $array = Parser::parseExpression('array(0 => "foo", 1 => array(0 => "a", 1 => "b", 2 => "c"))');
     $this->assertTrue($array->hasKey(1));
     $this->assertTrue($array->hasKey(2));
     $this->assertFalse($array->hasKey(2, FALSE));
     $array = Parser::parseExpression('array($key => "hurrr")');
     $this->assertFalse($array->hasKey('$key'));
     $var = Token::variable('$key');
     $this->assertTrue($array->hasKey($var));
 }
 public function testCreate()
 {
     $lookup = ArrayLookupNode::create(Token::variable('$form_state'), new StringNode(T_CONSTANT_ENCAPSED_STRING, "'storage'"));
     $this->assertEquals('$form_state[\'storage\']', $lookup->getText());
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function rewrite(ParameterNode $parameter)
 {
     parent::rewrite($parameter);
     $function = $parameter->getFunction();
     $form_state = Token::variable('$' . $parameter->getName());
     $set_errors = $function->find(Filter::isFunctionCall('form_set_error', 'form_error'));
     /** @var \Pharborist\Functions\FunctionCallNode $set_error */
     foreach ($set_errors as $set_error) {
         $arguments = $set_error->getArguments();
         $method = $set_error->getName()->getText() == 'form_set_error' ? 'setErrorByName' : 'setError';
         $rewrite = ObjectMethodCallNode::create(clone $form_state, $method)->appendArgument(clone $arguments[0])->appendArgument(clone $arguments[1]);
         $set_error->replaceWith($rewrite);
     }
     // form_clear_error() --> $form_state->clearErrors().
     $clear_errors = $function->find(Filter::isFunctionCall('form_clear_error'));
     foreach ($clear_errors as $clear_error) {
         $clear_error->replaceWith(ObjectMethodCallNode::create(clone $form_state, 'clearErrors'));
     }
     // form_get_errors() --> $form_state->getErrors()
     $get_errors = $function->find(Filter::isFunctionCall('form_get_errors'));
     foreach ($get_errors as $get_error) {
         $get_error->replaceWith(ObjectMethodCallNode::create(clone $form_state, 'getErrors'));
     }
     // form_get_error() --> $form_state->getError()
     $get_errors = $function->find(Filter::isFunctionCall('form_get_error'));
     /** @var \Pharborist\Functions\FunctionCallNode $get_error */
     foreach ($get_errors as $get_error) {
         $rewrite = ObjectMethodCallNode::create(clone $form_state, 'getError')->appendArgument($get_error->getArguments()->get(0));
         $get_error->replaceWith($rewrite);
     }
 }
    public function testChainMethodCall()
    {
        $object = Token::variable('$object');
        $method_call = ObjectMethodCallNode::create($object, 'someMethod');
        $chained_call = $method_call->appendMethodCall('chained');
        $this->assertEquals('$object->someMethod()', $chained_call->getObject()->getText());
        $this->assertEquals('chained', $chained_call->getMethodName()->getText());
        $source = <<<'EOF'
<?php
$object->someMethod();
EOF;
        $tree = Parser::parseSource($source);
        /** @var ExpressionStatementNode $expr_statement */
        $expr_statement = $tree->firstChild()->next();
        /** @var ObjectMethodCallNode $method_call */
        $method_call = $expr_statement->getExpression();
        $method_call->appendMethodCall('chained');
        $expected = <<<'EOF'
<?php
$object->someMethod()->chained();
EOF;
        $this->assertEquals($expected, $tree->getText());
    }
 /**
  * @return \Pharborist\Objects\ClassNode
  */
 public function build()
 {
     $controller = $this->render();
     $builder = $this->addMethod($this->builder, $controller, 'buildForm');
     if ($this->isConfig) {
         $builder->find(Filter::isFunctionCall('system_settings_form'))->each(function (FunctionCallNode $call) {
             $call->setName('parent::buildForm')->appendArgument(Token::variable('$form_state'));
         });
     }
     if ($this->validator) {
         $this->addMethod($this->validator, $controller, 'validateForm')->getParameterAtIndex(0)->setReference(TRUE)->setTypeHint('array');
     }
     if ($this->submitHandler) {
         $this->addMethod($this->submitHandler, $controller, $this->isConfig ? '_submitForm' : 'submitForm')->getParameterAtIndex(0)->setReference(TRUE)->setTypeHint('array');
     }
     return $controller;
 }
 /**
  * @expectedException \OutOfBoundsException
  */
 public function testInsertOutOfBoundsEmpty()
 {
     /** @var AnonymousFunctionNode $func */
     $func = Parser::parseExpression('function () {}');
     $func->insertLexicalVariable(Token::variable('$a'), 1);
 }