Completed
Push — master ( f036f0...b950dc )
by Rafał
02:16
created

GimmeTokenParser   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 14 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A decideCacheEnd() 0 4 1
A getTag() 0 4 1
B 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
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')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
            if ($stream->test(\Twig_Token::PUNCTUATION_TYPE, '[')) {
59
                $ignoreContext = $this->parser->getExpressionParser()->parseExpression();
60
            } else {
61
                $ignoreContext = new \Twig_Node_Expression_Array([], $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