Total Complexity | 54 |
Total Lines | 300 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like General 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 General, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | trait General |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * Adds validation Error |
||
35 | * @param string $field |
||
36 | * @param string $rule |
||
37 | * @param mixed|null $param |
||
38 | */ |
||
39 | abstract protected function addError(string $field, string $rule, $param = null); |
||
40 | |||
41 | /** |
||
42 | * Checks Field Required |
||
43 | * @param string $field |
||
44 | * @param string $value |
||
45 | * @param null|mixed $param |
||
46 | */ |
||
47 | protected function required(string $field, string $value, $param = null) |
||
51 | } |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Checks Email |
||
56 | * @param string $field |
||
57 | * @param string $value |
||
58 | * @param null|mixed $param |
||
59 | */ |
||
60 | protected function email(string $field, string $value, $param = null) |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Check Captcha |
||
71 | * @param string $field |
||
72 | * @param string $value |
||
73 | * @param $param |
||
74 | * @return void |
||
75 | * @throws ConfigException |
||
76 | * @throws DiException |
||
77 | * @throws ReflectionException |
||
78 | * @throws BaseException |
||
79 | */ |
||
80 | protected function captcha(string $field, string $value, $param = null) |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Checks for a valid credit card number |
||
94 | * @param string $field |
||
95 | * @param string $value |
||
96 | * @param null|mixed $param |
||
97 | */ |
||
98 | protected function creditCard(string $field, string $value, $param = null) |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Checks for a valid IBAN |
||
139 | * @param string $field |
||
140 | * @param string $value |
||
141 | * @param null|mixed $param |
||
142 | */ |
||
143 | protected function iban(string $field, string $value, $param = null) |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Checks for a valid format human name |
||
170 | * @param string $field |
||
171 | * @param string $value |
||
172 | * @param null|mixed $param |
||
173 | */ |
||
174 | protected function name(string $field, string $value, $param = null) |
||
175 | { |
||
176 | if (!empty($value)) { |
||
177 | if (preg_match("/^([a-z \p{L} '-])+$/i", $value) === 0) { |
||
178 | $this->addError($field, 'name', $param); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Checks that the provided string is a likely street address. |
||
185 | * @param string $field |
||
186 | * @param string $value |
||
187 | * @param null|mixed $param |
||
188 | */ |
||
189 | protected function streetAddress(string $field, string $value, $param = null) |
||
190 | { |
||
191 | if (!empty($value)) { |
||
192 | $hasLetter = preg_match('/[a-zA-Z]/', $value); |
||
193 | $hasDigit = preg_match('/\d/', $value); |
||
194 | $hasSpace = preg_match('/\s/', $value); |
||
195 | |||
196 | $passes = $hasLetter && $hasDigit && $hasSpace; |
||
197 | |||
198 | if (!$passes) { |
||
199 | $this->addError($field, 'streetAddress', $param); |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Validates the phone number // 555-555-5555 , 5555425555, 555 555 5555, 1(519) 555-4444, 1 (519) 555-4422, +1-555-555-5555 |
||
206 | * @param string $field |
||
207 | * @param string $value |
||
208 | * @param null|mixed $param |
||
209 | */ |
||
210 | protected function phoneNumber(string $field, string $value, $param = null) |
||
211 | { |
||
212 | if (!empty($value)) { |
||
213 | $regex = '/^(\+*\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i'; |
||
214 | |||
215 | if (!preg_match($regex, $value)) { |
||
216 | $this->addError($field, 'phoneNumber', $param); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Determines if the provided input is a valid date |
||
223 | * @param string $field |
||
224 | * @param string $value |
||
225 | * @param string|null $param format |
||
226 | */ |
||
227 | protected function date(string $field, string $value, string $param = null) |
||
228 | { |
||
229 | if (!empty($value)) { |
||
230 | if (!$param) { |
||
231 | $cdate1 = date('Y-m-d', strtotime($value)); |
||
232 | $cdate2 = date('Y-m-d H:i:s', strtotime($value)); |
||
233 | |||
234 | if ($cdate1 != $value && $cdate2 != $value) { |
||
235 | $this->addError($field, 'date', $param); |
||
236 | } |
||
237 | } else { |
||
238 | $date = \DateTime::createFromFormat($param, $value); |
||
239 | |||
240 | if ($date === false || $value != date($param, $date->getTimestamp())) { |
||
241 | $this->addError($field, 'date', $param); |
||
242 | } |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Ensures the value starts with a certain character / set of character |
||
249 | * @param string $field |
||
250 | * @param string $value |
||
251 | * @param string $param |
||
252 | */ |
||
253 | protected function starts(string $field, string $value, string $param) |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Custom regex validator |
||
264 | * @param string $field |
||
265 | * @param string $value |
||
266 | * @param string $param |
||
267 | */ |
||
268 | protected function regex(string $field, string $value, string $param) |
||
269 | { |
||
270 | if (!empty($value)) { |
||
271 | if (!preg_match($param, $value)) { |
||
272 | $this->addError($field, 'regex', $param); |
||
273 | } |
||
274 | } |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Validates JSON string |
||
279 | * @param string $field |
||
280 | * @param string $value |
||
281 | * @param null|mixed $param |
||
282 | */ |
||
283 | protected function jsonString(string $field, string $value, $param = null) |
||
284 | { |
||
285 | if (!empty($value)) { |
||
286 | $value = htmlspecialchars_decode($value, ENT_QUOTES); |
||
287 | |||
288 | if (!is_object(json_decode($value))) { |
||
289 | $this->addError($field, 'jsonString', $param); |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * Validates same value for both fields |
||
296 | * @param string $field |
||
297 | * @param string $value |
||
298 | * @param null|mixed $param |
||
299 | */ |
||
300 | protected function same(string $field, string $value, $param = null) |
||
301 | { |
||
302 | if (!empty($value)) { |
||
303 | if ($value != $this->data[$param]) { |
||
304 | $this->addError($field, 'same', $param); |
||
305 | } |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Validates uniqueness |
||
311 | * @param string $field |
||
312 | * @param $value |
||
313 | * @param string $param |
||
314 | * @return void |
||
315 | * @throws ConfigException |
||
316 | * @throws DiException |
||
317 | * @throws ReflectionException |
||
318 | * @throws DatabaseException |
||
319 | * @throws ModelException |
||
320 | */ |
||
321 | protected function unique(string $field, $value, string $param) |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |