Completed
Push — master ( 5bbfc4...6ddfd6 )
by Vladimir
02:33
created

TwigStakxBridge   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 115
Duplicated Lines 22.61 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 60.52%

Importance

Changes 0
Metric Value
dl 26
loc 115
c 0
b 0
f 0
wmc 12
lcom 2
cbo 5
ccs 23
cts 38
cp 0.6052
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setLogger() 0 4 1
A setGlobalVariable() 0 4 1
A getAssortmentDependencies() 12 12 1
A clearTemplateCache() 0 4 1
A createTemplate() 0 13 2
A getTemplateImportDependencies() 14 14 2
A hasProfiler() 0 4 1
A setProfiler() 0 4 1
A getProfilerOutput() 0 7 1

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\Compiler;
11
use allejo\stakx\Templating\TemplateBridgeInterface;
12
use Psr\Log\LoggerInterface;
13
14
class TwigStakxBridge implements TemplateBridgeInterface
15
{
16
    private $twig;
17
    private $logger;
18
19
    /** @var \Twig_Profiler_Profile */
20
    private $profiler;
21
22 76
    public function __construct(\Twig_Environment $twig)
23
    {
24 76
        $this->twig = $twig;
25 76
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setLogger(LoggerInterface $logger)
31
    {
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 12
    public function setGlobalVariable($key, $value)
39
    {
40 12
        $this->twig->addGlobal($key, $value);
41 12
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function clearTemplateCache()
47
    {
48
        $this->twig->clearTemplateCache();
0 ignored issues
show
Deprecated Code introduced by
The method Twig_Environment::clearTemplateCache() has been deprecated with message: since 1.18.3 (to be removed in 2.0)

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 12
    public function createTemplate($templateContent)
55
    {
56
        try
57
        {
58 12
            $template = $this->twig->createTemplate($templateContent);
59
        }
60
        catch (\Twig_Error $e)
61
        {
62
            throw new TwigError($e);
63
        }
64
65 12
        return new TwigTemplate($template);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 21 View Code Duplication
    public function getAssortmentDependencies($namespace, $bodyContent)
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...
72
    {
73
        // To see what this regex should match and what shouldn't be see:
74
        //     tests/allejo/stakx/Test/FrontMatter/FrontMatterDocumentTest.php
75
76 21
        $regex = "/{[{%].*?(?:$namespace)(?:\.|\[['\"])?([^_][^\W]+)?(?:\.|['\"]\])?[^_=]*?[%}]}/";
77 21
        $results = [];
78
79 21
        preg_match_all($regex, $bodyContent, $results);
80
81 21
        return array_unique($results[1]);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 7 View Code Duplication
    public function getTemplateImportDependencies($bodyContent)
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...
88
    {
89 7
        $regex = "/{%\s?(?:import|from|include)\s?['\"](.+)['\"].+/";
90 7
        $results = [];
91
92 7
        preg_match_all($regex, $bodyContent, $results);
93
94 7
        if (empty($results[1]))
95
        {
96
            return [];
97
        }
98
99 7
        return array_unique($results[1]);
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function hasProfiler()
106
    {
107
        return $this->profiler !== null;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 48
    public function setProfiler($profiler)
114
    {
115 48
        $this->profiler = $profiler;
116 48
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getProfilerOutput(Compiler $compiler)
122
    {
123
        $dumper = new TwigTextProfiler();
124
        $dumper->setTemplateMappings($compiler->getTemplateMappings());
125
126
        return $dumper->dump($this->profiler);
127
    }
128
}
129