SwitchTokenParser   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 42
c 1
b 0
f 1
dl 0
loc 85
rs 10
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTag() 0 3 1
A decideSwitchFork() 0 3 1
B parse() 0 60 8
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
namespace Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures;
7
8
use Twig_Error_Syntax;
9
use Twig_TokenParser;
10
use Twig_Token;
11
use Twig_Node;
12
13
/**
14
 * Class SwitchTokenParser
15
 *
16
 * @package Zicht\Bundle\FrameworkExtraBundle\Twig\ControlStructures
17
 */
18
class SwitchTokenParser extends Twig_TokenParser
19
{
20
    /**
21
     * @{inheritDoc}
22
     */
23
    public function getTag()
24
    {
25
        return 'switch';
26
    }
27
28
    /**
29
     * @{inheritDoc}
30
     */
31
    public function parse(Twig_Token $token)
32
    {
33
        $lineno = $token->getLine();
34
35
        $switchExpr = $this->parser->getExpressionParser()->parseExpression();
36
37
        /** @var $stream Twig_TokenStream */
38
        $stream = $this->parser->getStream();
39
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
40
41
42
        // skip whitespace between switch and first case
43
        while ($stream->test(Twig_Token::TEXT_TYPE)) {
44
            if (trim($stream->getCurrent()->getValue()) != '') {
45
                $content = $stream->getCurrent()->getValue();
46
                throw new Twig_Error_Syntax("Can not render content '$content' directly after switch", $stream->getCurrent()->getLine());
47
            }
48
            $stream->next();
49
        }
50
        $stream->expect(Twig_Token::BLOCK_START_TYPE);
51
52
        $tests = array();
53
54
        while (!$stream->test('endswitch')) {
55
            $token = $stream->expect(Twig_Token::NAME_TYPE, array('case', 'default'));
56
            switch ($token->getValue()) {
57
                case 'case':
58
                    $caseExpr   = array();
59
                    $caseExpr[] = $this->parser->getExpressionParser()->parseExpression();
60
                    while ($stream->test(Twig_Token::OPERATOR_TYPE, ',')) {
61
                        $stream->next();
62
                        $caseExpr[] = $this->parser->getExpressionParser()->parseExpression();
63
                    }
64
                    break;
65
                case 'default':
66
                    $caseExpr = null;
67
                    break;
68
            }
69
70
            $fallthrough = false;
71
            if ($stream->test('fallthrough')) {
72
                $stream->next();
73
                $fallthrough = true;
74
            }
75
            $stream->expect(Twig_Token::BLOCK_END_TYPE);
76
            $body = $this->parser->subparse(array($this, 'decideSwitchFork'));
77
78
            $tests[] = new SwitchCaseNode(
79
                $body,
0 ignored issues
show
Bug introduced by
It seems like $body can also be of type Twig\Node\Node; however, parameter $body of Zicht\Bundle\FrameworkEx...CaseNode::__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

79
                /** @scrutinizer ignore-type */ $body,
Loading history...
80
                $caseExpr,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $caseExpr does not seem to be defined for all execution paths leading up to this point.
Loading history...
81
                $fallthrough,
0 ignored issues
show
Bug introduced by
$fallthrough of type true is incompatible with the type integer expected by parameter $fallthrough of Zicht\Bundle\FrameworkEx...CaseNode::__construct(). ( Ignorable by Annotation )

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

81
                /** @scrutinizer ignore-type */ $fallthrough,
Loading history...
82
                $token->getLine(),
83
                $token->getValue()
84
            );
85
        }
86
87
        $stream->expect('endswitch');
88
        $stream->expect(Twig_Token::BLOCK_END_TYPE);
89
90
        return new SwitchNode(new Twig_Node($tests), $switchExpr, $lineno);
91
    }
92
93
94
    /**
95
     * Checks if the token is part of the current control structure.
96
     *
97
     * @param Twig_Token $token
98
     * @return mixed
99
     */
100
    public function decideSwitchFork($token)
101
    {
102
        return $token->test(array('case', 'default', 'endswitch'));
103
    }
104
}
105