WithTokenParser::decideWithEnd()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures;
8
9
use Twig_Token;
10
use Twig_NodeInterface;
11
12
/**
13
 * The 'with' tag allows a scope-shift into a defined array. The format is as follows:
14
 *
15
 * {% with expr [as localName] [, expr2 [as localName2], [....]]  {sandboxed|merged} %}
16
 *     content
17
 * {% endwith %}
18
 */
19
class WithTokenParser extends \Twig_TokenParser
20
{
21
    private $options = array(
22
        'merged',
23
        'sandboxed',
24
        'always'
25
    );
26
27
    /**
28
     * Gets the tag name associated with this token parser.
29
     *
30
     * @return string
31
     */
32
    public function getTag()
33
    {
34
        return 'with';
35
    }
36
37
    /**
38
     * Parses a token and returns a node.
39
     *
40
     * @param Twig_Token $token A Twig_Token instance
41
     * @return Twig_NodeInterface A Twig_NodeInterface instance
42
     */
43
    public function parse(Twig_Token $token)
44
    {
45
        $options = array();
46
        $stream = $this->parser->getStream();
47
        $start = $stream->getCurrent();
48
        $arguments = array();
49
        do {
50
            $value = $this->parser->getExpressionParser()->parseExpression();
51
            if ($stream->test('as')) {
52
                $stream->expect('as');
53
                $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
54
            } else {
55
                $name = null;
56
            }
57
            $arguments[] = array('name' => $name, 'value' => $value);
58
59
            $end = !$stream->test(Twig_Token::PUNCTUATION_TYPE, ',');
60
            if (!$end) {
61
                $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',');
62
            }
63
        } while (!$end);
64
65
        while ($stream->test($this->options)) {
66
            $options[] = $stream->expect($this->options)->getValue();
67
        }
68
69
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
70
        $body = $this->parser->subparse(array($this, 'decideWithEnd'), true);
71
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
72
        return new WithNode($arguments, $body, $options, $start->getLine(), $start->getValue());
0 ignored issues
show
Bug introduced by
$options of type array|string[] is incompatible with the type integer expected by parameter $options of Zicht\Bundle\FrameworkEx...WithNode::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        return new WithNode($arguments, $body, /** @scrutinizer ignore-type */ $options, $start->getLine(), $start->getValue());
Loading history...
Bug introduced by
It seems like $body can also be of type Twig\Node\Node; however, parameter $body of Zicht\Bundle\FrameworkEx...WithNode::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

72
        return new WithNode($arguments, /** @scrutinizer ignore-type */ $body, $options, $start->getLine(), $start->getValue());
Loading history...
73
    }
74
75
76
    /**
77
     * Checks for the end of the control structure.
78
     *
79
     * @param Twig_Token $token
80
     * @return bool
81
     */
82
    public function decideWithEnd($token)
83
    {
84
        return $token->test('endwith');
85
    }
86
}
87