Test Failed
Push — develop ( 32ca41...230235 )
by Brent
04:59
created

VariableFactory   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 162
Duplicated Lines 12.35 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 39.47%

Importance

Changes 0
Metric Value
dl 20
loc 162
ccs 30
cts 76
cp 0.3947
rs 10
c 0
b 0
f 0
wmc 28
lcom 1
cbo 8

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A make() 0 4 1
A setVariableParser() 0 6 1
A setYamlParser() 0 6 1
A setMarkdownParser() 0 6 1
A setImageParser() 0 6 1
A create() 0 16 4
A setJsonRule() 10 10 2
A setYamlRule() 0 16 3
A setMarkdownRule() 0 14 3
A setHtmlRule() 10 10 2
A setImageRule() 0 18 4
A setDirectoryRule() 0 14 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
namespace Stitcher\Variable;
4
5
use Pageon\Html\Image\ImageFactory;
6
use Pageon\Lib\Markdown\MarkdownParser;
7
use Stitcher\DynamicFactory;
8
use Symfony\Component\Yaml\Yaml;
9
use TypeError;
10
11
class VariableFactory extends DynamicFactory
12
{
13
    /** @var \Symfony\Component\Yaml\Yaml */
14
    private $yamlParser;
15
16
    /** @var \Pageon\Lib\Markdown\MarkdownParser */
17
    private $markdownParser;
18
19
    /** @var \Pageon\Html\Image\ImageFactory */
20
    private $imageParser;
21
22
    /** @var \Stitcher\Variable\VariableParser */
23
    private $variableParser;
24
25 3
    public function __construct()
26
    {
27 3
        $this->setJsonRule();
28 3
        $this->setYamlRule();
29 3
        $this->setMarkdownRule();
30 3
        $this->setImageRule();
31 3
        $this->setDirectoryRule();
32 3
        $this->setHtmlRule();
33 3
    }
34
35 3
    public static function make(): VariableFactory
36
    {
37 3
        return new self();
38
    }
39
40
    public function setYamlParser(Yaml $yamlParser): VariableFactory
41
    {
42
        $this->yamlParser = $yamlParser;
43
44
        return $this;
45
    }
46
47
    public function setMarkdownParser(MarkdownParser $markdownParser): VariableFactory
48
    {
49
        $this->markdownParser = $markdownParser;
50
51
        return $this;
52
    }
53
54
    public function setImageParser(ImageFactory $imageParser): VariableFactory
55
    {
56
        $this->imageParser = $imageParser;
57
58
        return $this;
59
    }
60
61 3
    public function setVariableParser(VariableParser $variableParser): VariableFactory
62
    {
63 3
        $this->variableParser = $variableParser;
64
65 3
        return $this;
66
    }
67
68
    public function create($value): AbstractVariable
69
    {
70
        foreach ($this->getRules() as $rule) {
71
            try {
72
                $variable = $rule($value);
73
            } catch (TypeError $e) {
0 ignored issues
show
Bug introduced by
The class TypeError does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
74
                continue;
75
            }
76
77
            if ($variable instanceof AbstractVariable) {
78
                return $variable;
79
            }
80
        }
81
82
        return DefaultVariable::make($value);
83
    }
84
85 View Code Duplication
    private function setJsonRule(): DynamicFactory
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...
86
    {
87 3
        return $this->setRule(JsonVariable::class, function (string $value) {
88
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'json') {
89
                return null;
90
            }
91
92
            return JsonVariable::make($value);
93 3
        });
94
    }
95
96
    private function setYamlRule(): void
97
    {
98 3
        $this->setRule(YamlVariable::class, function (string $value) {
99
            if (! $this->yamlParser) {
100
                return null;
101
            }
102
103
            $extension = pathinfo($value, PATHINFO_EXTENSION);
104
105
            if (! \in_array($extension, ['yaml', 'yml'])) {
106
                return null;
107
            }
108
109
            return YamlVariable::make($value, $this->yamlParser, $this->variableParser);
110 3
        });
111 3
    }
112
113
    private function setMarkdownRule(): void
114
    {
115 3
        $this->setRule(MarkdownVariable::class, function (string $value) {
116
            if (! $this->markdownParser) {
117
                return null;
118
            }
119
120
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'md') {
121
                return null;
122
            }
123
124
            return MarkdownVariable::make($value, $this->markdownParser);
125 3
        });
126 3
    }
127
128 View Code Duplication
    private function setHtmlRule(): void
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...
129
    {
130 3
        $this->setRule(HtmlVariable::class, function (string $value) {
131
            if (pathinfo($value, PATHINFO_EXTENSION) !== 'html') {
132
                return null;
133
            }
134
135
            return new HtmlVariable($value);
136 3
        });
137 3
    }
138
139
    private function setImageRule(): void
140
    {
141 3
        $this->setRule(ImageVariable::class, function ($value) {
142
            if (! $this->imageParser) {
143
                return null;
144
            }
145
146
            $srcPath = \is_array($value) ? $value['src'] ?? null : $value;
147
148
            $extension = pathinfo($srcPath, PATHINFO_EXTENSION);
149
150
            if (! \in_array($extension, ['jpeg', 'jpg', 'png', 'gif'])) {
151
                return null;
152
            }
153
154
            return ImageVariable::make($value, $this->imageParser);
155 3
        });
156 3
    }
157
158
    private function setDirectoryRule(): void
159
    {
160 3
        $this->setRule(DirectoryVariable::class, function ($value) {
161
            if (!is_string($value) || substr($value, -1) !== '/') {
162
                return null;
163
            }
164
165
            if (strpos($value, 'http') !== null) {
166
                return null;
167
            }
168
169
            return new DirectoryVariable($value, $this->variableParser);
170 3
        });
171 3
    }
172
}
173