GimmeTokenParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 14 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 7
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decideCacheEnd() 0 4 1
A getTag() 0 4 1
A parse() 7 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Superdesk Web Publisher Templates System.
5
 *
6
 * Copyright 2015 Sourcefabric z.ú. and contributors.
7
 *
8
 * For the full copyright and license information, please see the
9
 * AUTHORS and LICENSE files distributed with this source code.
10
 *
11
 * @copyright 2015 Sourcefabric z.ú
12
 * @license http://www.superdesk.org/license
13
 */
14
15
namespace SWP\Component\TemplatesSystem\Twig\TokenParser;
16
17
use SWP\Component\TemplatesSystem\Twig\Node\GimmeNode;
18
19
/**
20
 * Parser for gimme/endgimme blocks.
21
 */
22
class GimmeTokenParser extends \Twig\TokenParser\AbstractTokenParser
23
{
24
    /**
25
     * @param \Twig\Token $token
26
     *
27
     * @return bool
28
     */
29
    public function decideCacheEnd(\Twig\Token $token)
30
    {
31
        return $token->test('endgimme');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getTag()
38
    {
39
        return 'gimme';
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function parse(\Twig\Token $token)
46
    {
47
        $lineno = $token->getLine();
48
        $stream = $this->parser->getStream();
49
50
        $annotation = $this->parser->getExpressionParser()->parseAssignmentExpression();
51
        $parameters = null;
52
        if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'with')) {
53
            $parameters = $this->parser->getExpressionParser()->parseExpression();
54
        }
55
56
        $ignoreContext = null;
57 View Code Duplication
        if ($stream->nextIf(\Twig\Token::NAME_TYPE, 'ignoreContext')) {
58
            if ($stream->test(\Twig\Token::PUNCTUATION_TYPE, '[')) {
59
                $ignoreContext = $this->parser->getExpressionParser()->parseExpression();
60
            } else {
61
                $ignoreContext = new \Twig\Node\Expression\ArrayExpression([], $token->getLine());
62
            }
63
        }
64
65
        $stream->expect(\Twig\Token::BLOCK_END_TYPE);
66
        $body = $this->parser->subparse([$this, 'decideCacheEnd'], true);
67
        $stream->expect(\Twig\Token::BLOCK_END_TYPE);
68
69
        return new GimmeNode($annotation, $parameters, $ignoreContext, $body, $lineno, $this->getTag());
70
    }
71
}
72