Completed
Push — master ( 3be2b4...9bcc1d )
by Paweł
02:33
created

StringType   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 74
c 2
b 1
f 0
dl 0
loc 272
ccs 85
cts 85
cp 1
rs 8.72
wmc 46

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setAllowedValues() 0 13 4
A getMaxLength() 0 3 1
A setMinLength() 0 5 1
A getMinLength() 0 3 1
A setValueRegex() 0 6 1
A getAllowedValues() 0 3 1
A setMaxLength() 0 5 1
A setConversion() 0 9 3
A getConversion() 0 3 1
A checkValueRegex() 0 10 5
A checkValue() 0 7 1
A getValueRegex() 0 7 5
A convertValue() 0 10 5
A setValue() 0 18 5
B checkValueLength() 0 9 7
A checkIfValueIsInAllowedValues() 0 12 4

How to fix   Complexity   

Complex Class

Complex classes like StringType often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StringType, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items\DataTypes;
15
16
use InvalidArgumentException;
17
use Wszetko\Sitemap\Interfaces\DataType;
18
19
/**
20
 * Class StringType.
21
 *
22
 * @package Wszetko\Sitemap\Items\DataTypes
23
 */
24
class StringType extends AbstractDataType
25
{
26
    /**
27
     * @var int
28
     */
29
    protected $minLength;
30
31
    /**
32
     * @var int
33
     */
34
    protected $maxLength;
35
36
    /**
37
     * @var null|array
38
     */
39
    protected $allowedValues;
40
41
    /**
42
     * @var string
43
     */
44
    protected $regex;
45
46
    /**
47
     * @var string
48
     */
49
    protected $regexGroup;
50
51
    /**
52
     * @var null|string
53
     */
54
    protected $conversion;
55
56
    /**
57
     * @return null|int
58
     */
59 496
    public function getMinLength(): ?int
60
    {
61 496
        return $this->minLength;
62
    }
63
64
    /**
65
     * @param int $minLength
66
     *
67
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
68
     */
69 302
    public function setMinLength(int $minLength): self
70
    {
71 302
        $this->minLength = $minLength;
72
73 302
        return $this;
74
    }
75
76
    /**
77
     * @return null|int
78
     */
79 496
    public function getMaxLength(): ?int
80
    {
81 496
        return $this->maxLength;
82
    }
83
84
    /**
85
     * @param int $maxLength
86
     *
87
     * @return self
88
     */
89 302
    public function setMaxLength(int $maxLength): self
90
    {
91 302
        $this->maxLength = $maxLength;
92
93 302
        return $this;
94
    }
95
96
    /**
97
     * @return null|array
98
     */
99 496
    public function getAllowedValues(): ?array
100
    {
101 496
        return $this->allowedValues;
102
    }
103
104
    /**
105
     * @param null|array|string $allowedValues
106
     *
107
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
108
     */
109 440
    public function setAllowedValues($allowedValues): self
110
    {
111 440
        if (is_string($allowedValues)) {
112 352
            $allowedValues = explode(',', $allowedValues);
113
114 352
            foreach ($allowedValues as $allowedValue) {
115 352
                $this->allowedValues[] = trim($allowedValue);
116
            }
117 90
        } elseif (is_array($allowedValues)) {
118 90
            $this->allowedValues = $allowedValues;
119
        }
120
121 440
        return $this;
122
    }
123
124
    /**
125
     * @param string $regex
126
     * @param string $regexGroup
127
     *
128
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
129
     */
130 378
    public function setValueRegex(string $regex, string $regexGroup): self
131
    {
132 378
        $this->regex = $regex;
133 378
        $this->regexGroup = $regexGroup;
134
135 378
        return $this;
136
    }
137
138
    /**
139
     * @return null|array
140
     */
141 496
    public function getValueRegex(): ?array
142
    {
143 496
        if (null !== $this->regex && '' !== $this->regex && null !== $this->regexGroup && '' !== $this->regexGroup) {
144 120
            return [$this->regexGroup => $this->regex];
145
        }
146
147 494
        return null;
148
    }
149
150
    /**
151
     * @param string $conversion
152
     *
153
     * @return $this
154
     */
155 360
    public function setConversion(string $conversion): self
156
    {
157 360
        if (in_array($conversion, ['upper', 'UPPER', 'Upper'], true)) {
158 302
            $this->conversion = 'upper';
159 360
        } elseif (in_array($conversion, ['lower', 'LOWER', 'Lower'], true)) {
160 360
            $this->conversion = 'lower';
161
        }
162
163 360
        return $this;
164
    }
165
166
    /**
167
     * @return null|string
168
     */
169 496
    public function getConversion(): ?string
170
    {
171 496
        return $this->conversion;
172
    }
173
174
    /**
175
     * @param mixed $value
176
     * @param array $parameters
177
     *
178
     * @return static
179
     *
180
     * @throws \InvalidArgumentException
181
     */
182 496
    public function setValue($value, $parameters = []): DataType
183
    {
184 496
        if (null !== $value) {
185 496
            $value = (string) $value;
186 496
            $this->checkValue($value);
187
        }
188
189
        if (
190 496
            (null === $value
191 496
            || '' === $value)
192 496
            && $this->isRequired()
193
        ) {
194 14
            throw new InvalidArgumentException($this->getName() . ' need to be set.');
195
        }
196
197 490
        parent::setValue($value, $parameters);
198
199 490
        return $this;
200
    }
201
202
    /**
203
     * @param string $value
204
     * @param-out null|string $value
205
     *
206
     * @return void
207
     */
208 496
    private function checkValue(string &$value):void
209
    {
210 496
        $value = trim($value);
211 496
        $this->checkValueLength($value);
212 496
        $this->convertValue($value);
213 496
        $this->checkIfValueIsInAllowedValues($value);
214 496
        $this->checkValueRegex($value);
215 496
    }
216
217
    /**
218
     * @param null|string $value
219
     * @param-out null|string $value
220
     *
221
     * @return void
222
     */
223 496
    private function checkValueLength(?string &$value):void
224
    {
225 496
        if (null !== $value) {
226 496
            if (null !== $this->getMinLength() && mb_strlen($value) < $this->getMinLength()) {
227 4
                $value = null;
228
            }
229
230 496
            if (null !== $this->getMaxLength() && null !== $value && mb_strlen($value) > $this->getMaxLength()) {
231 6
                $value = mb_substr($value, 0, $this->getMaxLength());
232
            }
233
        }
234 496
    }
235
236
    /**
237
     * @param null|string $value
238
     * @param-out null|string $value
239
     *
240
     * @return void
241
     */
242 496
    private function convertValue(?string &$value): void
243
    {
244 496
        if (null !== $value) {
245 496
            $conversion = $this->getConversion();
246
247 496
            if (is_string($conversion)) {
248 102
                if ('upper' == $conversion) {
249 36
                    $value = mb_strtoupper($value);
250 88
                } elseif ('lower' == $conversion) {
251 88
                    $value = mb_strtolower($value);
252
                }
253
            }
254
        }
255 496
    }
256
257
    /**
258
     * @param null|string $value
259
     * @param-out null|string $value
260
     *
261
     * @return void
262
     */
263 496
    private function checkIfValueIsInAllowedValues(?string &$value): void
264
    {
265 496
        if (null !== $value) {
266 496
            $allowedValues = $this->getAllowedValues();
267
268 496
            if (null !== $allowedValues) {
269 68
                $match = preg_grep("/{$value}/i", $allowedValues);
270
271 68
                if ([] === $match) {
272 12
                    $value = null;
273
                } else {
274 64
                    $value = array_values($match)[0];
275
                }
276
            }
277
        }
278 496
    }
279
280
    /**
281
     * @param null|string $value
282
     * @param-out null|string $value
283
     *
284
     * @return void
285
     */
286 496
    private function checkValueRegex(?string &$value): void
287
    {
288 496
        if (null !== $value) {
289 496
            $regex = $this->getValueRegex();
290
291 496
            if (null !== $regex && [] !== $regex) {
292 120
                $match = preg_match(array_values($regex)[0], $value);
293
294 120
                if (1 !== $match) {
295 6
                    $value = null;
296
                }
297
            }
298
        }
299 496
    }
300
}
301