CacheFlagTokenParser::parse()   C
last analyzed

Complexity

Conditions 9
Paths 144

Size

Total Lines 87
Code Lines 66

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 66
c 1
b 0
f 0
nc 144
nop 1
dl 0
loc 87
rs 6.8929

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace mmikkel\cacheflag\twigextensions;
4
5
use Twig\Parser;
6
use Twig\Token;
7
use Twig\TokenParser\AbstractTokenParser;
8
9
/**
10
 * Class CacheFlagTokenParser
11
 * @package mmikkel\cacheflag\twigextensions
12
 */
13
class CacheFlagTokenParser extends AbstractTokenParser
14
{
15
    // Public Methods
16
    // =========================================================================
17
18
    /**
19
     * @return string
20
     */
21
    public function getTag(): string
22
    {
23
        return 'cacheflag';
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function parse(Token $token)
30
    {
31
        $lineno = $token->getLine();
32
        /** @var Parser $parser */
33
        $parser = $this->parser;
34
        $stream = $parser->getStream();
35
36
        $nodes = [];
37
38
        $attributes = [
39
            'global' => false,
40
            'durationNum' => null,
41
            'durationUnit' => null,
42
            'elements' => false,
43
        ];
44
45
        if ($stream->test(Token::NAME_TYPE, 'flagged')) {
46
            $stream->next();
47
            $nodes['flags'] = $parser->getExpressionParser()->parseExpression();
48
        }
49
50
        if ($stream->test(Token::NAME_TYPE, 'with')) {
51
            $stream->next();
52
            $stream->expect(Token::NAME_TYPE, 'elements');
53
            $attributes['elements'] = true;
54
        }
55
56
        if ($stream->test(Token::NAME_TYPE, 'globally')) {
57
            $attributes['global'] = true;
58
            $stream->next();
59
        }
60
61
        if ($stream->test(Token::NAME_TYPE, 'using')) {
62
            $stream->next();
63
            $stream->expect(Token::NAME_TYPE, 'key');
64
            $nodes['key'] = $parser->getExpressionParser()->parseExpression();
65
        }
66
67
        if ($stream->test(Token::NAME_TYPE, 'for')) {
68
            $stream->next();
69
            $attributes['durationNum'] = $stream->expect(Token::NUMBER_TYPE)->getValue();
70
            $attributes['durationUnit'] = $stream->expect(Token::NAME_TYPE,
71
                [
72
                    'sec',
73
                    'secs',
74
                    'second',
75
                    'seconds',
76
                    'min',
77
                    'mins',
78
                    'minute',
79
                    'minutes',
80
                    'hour',
81
                    'hours',
82
                    'day',
83
                    'days',
84
                    'fortnight',
85
                    'fortnights',
86
                    'forthnight',
87
                    'forthnights',
88
                    'month',
89
                    'months',
90
                    'year',
91
                    'years',
92
                    'week',
93
                    'weeks'
94
                ])->getValue();
95
        } else if ($stream->test(Token::NAME_TYPE, 'until')) {
96
            $stream->next();
97
            $nodes['expiration'] = $parser->getExpressionParser()->parseExpression();
98
        }
99
100
        if ($stream->test(Token::NAME_TYPE, 'if')) {
101
            $stream->next();
102
            $nodes['conditions'] = $parser->getExpressionParser()->parseExpression();
103
        } else if ($stream->test(Token::NAME_TYPE, 'unless')) {
104
            $stream->next();
105
            $nodes['ignoreConditions'] = $parser->getExpressionParser()->parseExpression();
106
        }
107
108
        $stream->expect(Token::BLOCK_END_TYPE);
109
        $nodes['body'] = $parser->subparse([
110
            $this,
111
            'decideCacheEnd'
112
        ], true);
113
        $stream->expect(Token::BLOCK_END_TYPE);
114
115
        return new CacheFlagNode($nodes, $attributes, $lineno, $this->getTag());
116
    }
117
118
    /**
119
     * @param Token $token
120
     * @return bool
121
     */
122
    public function decideCacheEnd(Token $token): bool
123
    {
124
        return $token->test('endcacheflag');
125
    }
126
}
127