Completed
Pull Request — master (#69)
by Vladimir
05:36
created

TwigExtension   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 27.08 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 25.64%

Importance

Changes 0
Metric Value
dl 26
loc 96
c 0
b 0
f 0
wmc 18
lcom 2
cbo 5
rs 10
ccs 10
cts 39
cp 0.2564

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __call() 0 11 2
A parseMarkup() 0 4 1
A addFilters() 13 13 4
A addFunctions() 13 13 4
A getName() 0 4 1
A getFilters() 0 17 2
A getFunctions() 0 4 1
A getTokenParsers() 0 11 2

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
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Templating\Twig;
9
10
use allejo\stakx\MarkupEngine\MarkupEngineManager;
11
use allejo\stakx\RuntimeStatus;
12
use allejo\stakx\Service;
13
use allejo\stakx\Templating\Twig\Extension\AbstractTwigExtension;
14
use allejo\stakx\Templating\Twig\Extension\TwigFilterInterface;
15
use allejo\stakx\Templating\Twig\Extension\TwigFunctionInterface;
16
use allejo\stakx\Templating\Twig\MarkupBlock\TokenParser;
17
18
class TwigExtension extends \Twig_Extension
19
{
20
    private $filters = [];
21
    private $functions = [];
22
    private $markupManager;
23
24 54
    public function __construct(MarkupEngineManager $manager)
25
    {
26 54
        $this->markupManager = $manager;
27 54
    }
28
29
    public function __call($name, $arguments)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
30
    {
31
        $tag = lcfirst(str_replace('parseMarkup', '', $name));
32
33
        if (!$tag)
34
        {
35
            throw new \BadMethodCallException("No method $name found in this class.");
36
        }
37
38
        return $this->parseMarkup($arguments[0], $tag);
39
    }
40
41
    public function parseMarkup($content, $tag)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43
        return $this->markupManager->getEngineByTag($tag)->parse($content);
44
    }
45
46 18 View Code Duplication
    public function addFilters(/*iterable*/ $filters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
47
    {
48
        /** @var AbstractTwigExtension|TwigFilterInterface $filter */
49 18
        foreach ($filters as $filter)
50
        {
51 18
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $filter::disableInSafeMode())
52
            {
53
                continue;
54
            }
55
56 18
            $this->filters[] = $filter::get();
57
        }
58 18
    }
59
60 View Code Duplication
    public function addFunctions(/*iterable*/ $functions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        /** @var AbstractTwigExtension|TwigFunctionInterface $fxn */
63
        foreach ($functions as $fxn)
64
        {
65
            if (Service::hasRunTimeFlag(RuntimeStatus::IN_SAFE_MODE) && $fxn::disableInSafeMode())
66
            {
67
                continue;
68
            }
69
70
            $this->functions[] = $fxn::get();
71
        }
72
    }
73
74
    public function getFilters()
75
    {
76
        $filters = $this->filters;
77
78
        foreach ($this->markupManager->getTemplateTags() as $tag)
79
        {
80
            // Since we can't pass what tag/markup language we're using to the callable, let's make the callable to a
81
            // non-existent method that will be handled by __call()
82
            $filters[] = new \Twig_SimpleFilter(
83
                $tag,
84
                [$this, 'parseMarkup' . ucfirst($tag)],
85
                ['is_safe' => ['html']]
86
            );
87
        }
88
89
        return $filters;
90
    }
91
92
    public function getFunctions()
93
    {
94
        return $this->functions;
95
    }
96
97
    public function getTokenParsers()
98
    {
99
        $tokenParsers = [];
100
101
        foreach ($this->markupManager->getTemplateTags() as $tag)
102
        {
103
            $tokenParsers[] = new TokenParser($tag);
104
        }
105
106
        return $tokenParsers;
107
    }
108
109 54
    public function getName()
110
    {
111 54
        return 'stakx_twig_base_extension';
112
    }
113
}
114