Completed
Pull Request — master (#86)
by Vladimir
02:09
created

TwigStakxBridge   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 107
Duplicated Lines 24.3 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 65.71%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 2
cbo 5
dl 26
loc 107
ccs 23
cts 35
cp 0.6571
rs 10

9 Methods

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