Completed
Push — master ( 342d67...6b3932 )
by Paweł
02:12
created

StringType::checkValue()   B

Complexity

Conditions 10
Paths 36

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 10

Importance

Changes 0
Metric Value
cc 10
nc 36
nop 1
dl 0
loc 32
ccs 17
cts 17
cp 1
crap 10
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Wszetko\Sitemap\Items\DataTypes;
5
6
use InvalidArgumentException;
7
use Wszetko\Sitemap\Interfaces\DataType;
8
9
/**
10
 * Class StringType
11
 *
12
 * @package Wszetko\Sitemap\Items\DataTypes
13
 */
14
class StringType extends AbstractDataType
15
{
16
    /**
17
     * @var int
18
     */
19
    protected $minLength;
20
21
    /**
22
     * @var int
23
     */
24
    protected $maxLength;
25
26
    /**
27
     * @var array|null
28
     */
29
    protected $allowedValues = null;
30
31
    /**
32
     * @var string
33
     */
34
    protected $regex;
35
36
    /**
37
     * @var string
38
     */
39
    protected $regexGroup;
40
41
    /**
42
     * @return int|null
43
     */
44 60
    public function getMinLength(): ?int
45
    {
46 60
        return $this->minLength;
47
    }
48
49
    /**
50
     * @param int $minLength
51
     *
52
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
53
     */
54 24
    public function setMinLength(int $minLength): self
55
    {
56 24
        $this->minLength = $minLength;
57
58 24
        return $this;
59
    }
60
61
    /**
62
     * @return int|null
63
     */
64 60
    public function getMaxLength(): ?int
65
    {
66 60
        return $this->maxLength;
67
    }
68
69
    /**
70
     * @param int $maxLength
71
     *
72
     * @return self
73
     */
74 24
    public function setMaxLength(int $maxLength): self
75
    {
76 24
        $this->maxLength = $maxLength;
77
78 24
        return $this;
79
    }
80
81
    /**
82
     * @return array|null
83
     */
84 60
    public function getAllowedValues(): ?array
85
    {
86 60
        return $this->allowedValues;
87
    }
88
89
    /**
90
     * @param string|array|null $allowedValues
91
     *
92
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
93
     */
94 46
    public function setAllowedValues($allowedValues): self
95
    {
96 46
        if (is_string($allowedValues)) {
97 34
            $allowedValues = explode(',', $allowedValues);
98
99 34
            foreach ($allowedValues as $allowedValue) {
100 34
                $this->allowedValues[] = trim($allowedValue);
101
            }
102 12
        } elseif (is_array($allowedValues)) {
103 12
            $this->allowedValues = $allowedValues;
104
        }
105
106 46
        return $this;
107
    }
108
109
    /**
110
     * @param string $regex
111
     * @param string $regexGroup
112
     *
113
     * @return \Wszetko\Sitemap\Items\DataTypes\StringType
114
     */
115 40
    public function setValueRegex(string $regex, string $regexGroup): self
116
    {
117 40
        $this->regex = $regex;
118 40
        $this->regexGroup = $regexGroup;
119
120 40
        return $this;
121
    }
122
123
    /**
124
     * @return array|null
125
     */
126 60
    public function getValueRegex(): ?array
127
    {
128 60
        if (!empty($this->regex) && !empty($this->regexGroup)) {
129 20
            return [$this->regexGroup => $this->regex];
130
        }
131
132 60
        return null;
133
    }
134
135
    /**
136
     * @param string|int|float|object|null $value
137
     * @param array                        $parameters
138
     *
139
     * @return self
140
     */
141 60
    public function setValue($value, ...$parameters): DataType
142
    {
143 60
        if ($value !== null) {
144 60
            $value = strval($value);
145 60
            $this->checkValue($value);
146
        }
147
148 60
        if (empty($value) && $this->isRequired()) {
149 4
            throw new InvalidArgumentException($this->getName() . ' need to be set.');
150
        }
151
152 59
        parent::setValue($value, $parameters[0] ?? []);
153
154 59
        return $this;
155
    }
156
157
    /**
158
     * @param $value
159
     */
160 60
    private function checkValue(&$value)
161
    {
162 60
        $value = trim($value);
163
164 60
        if ($this->getMinLength() !== null && mb_strlen($value) < $this->getMinLength()) {
165 1
            $value = null;
166
        }
167
168 60
        if ($this->getMaxLength() !== null && $value !== null && mb_strlen($value) > $this->getMaxLength()) {
169 2
            $value = mb_substr($value, 0, $this->getMaxLength());
170
        }
171
172 60
        if (!empty($this->getAllowedValues())) {
173 9
            $match = preg_grep("/$value/i", $this->getAllowedValues());
174
175 9
            if (empty($match)) {
176 3
                $value = null;
177
            } else {
178 9
                $value = array_values($match)[0];
179
            }
180
        }
181
182 60
        $regex = $this->getValueRegex();
183
184 60
        if (!empty($regex)) {
185 20
            preg_match_all(array_values($regex)[0], $value, $matches);
186
187 20
            if (empty($matches[array_key_first($regex)])) {
188 2
                $value = null;
189
            }
190
        }
191 60
    }
192
}
193