Passed
Push — develop ( 783769...399dc3 )
by Daniel
25:34 queued 10:42
created

Validator   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Test Coverage

Coverage 69.51%

Importance

Changes 14
Bugs 1 Features 1
Metric Value
eloc 72
c 14
b 1
f 1
dl 0
loc 366
ccs 57
cts 82
cp 0.6951
rs 9.0399
wmc 42

22 Methods

Rating   Name   Duplication   Size   Complexity  
A validInclude() 0 3 1
A validLocation() 0 3 1
A validClassPassId() 0 13 3
A validDate() 0 6 2
A validFileType() 0 3 1
A validFileName() 0 3 1
A validFrom() 0 24 5
A validAddressText() 0 3 1
A validDetails() 0 3 1
A validAdditionalInfo() 0 3 1
A validAttachmentId() 0 3 1
A validEventId() 0 18 4
A validTo() 0 23 5
A validLocationId() 0 3 1
A validTitle() 0 3 1
A validUsageAllowance() 0 3 1
A validId() 0 13 3
A validTicketId() 0 23 5
A validToken() 0 3 1
A validuseRestrictedForDays() 0 3 1
A validUsageType() 0 3 1
A validTag() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Validator 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 Validator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace InShore\Bookwhen;
6
7
use InShore\Bookwhen\Interfaces\ValidatorInterface;
8
use Respect\Validation\Validator as v;
9
10
class Validator implements ValidatorInterface
11
{
12
    /**
13
     *
14
     * @author Daniel Mullin [email protected]
15
     *
16 74
     * @access public
17
     *
18
     * @param string $additionalInfo
19 74
     * @return bool
20
     */
21
    public function validAdditionalInfo(string $additionalInfo): bool
22
    {
23
        return v::stringType()->notEmpty()->alnum()->validate($additionalInfo);
24
    }
25
26
    /**
27
     *
28
     * @author Daniel Mullin [email protected]
29
     *
30
     * @access public
31 1
     *
32
     * @param string $addressText
33 1
     * @return bool
34
     */
35 1
    public function validAddressText(string $addressText): bool
36
    {
37
        return v::stringType()->notEmpty()->alnum()->validate($addressText);
38
    }
39 1
40
    /**
41
     *
42
     * @author Daniel Mullin [email protected]
43 1
     *
44
     * @access public
45
     *
46
     * @param string $attachmentId
47
     * @return bool
48
     */
49
    public function validAttachmentId(string $attachmentId): bool
50
    {
51
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($attachmentId);
52
    }
53
54
    /**
55
     *
56 1
     * @author Daniel Mullin [email protected]
57
     * @author Brandon Lubbehusen [email protected]
58 1
     *
59
     * @access public
60 1
     *
61
     * @param string $classPassId
62
     * @return bool
63
     */
64 1
    public function validClassPassId(string $classPassId): bool
65
    {
66
        $exploded = explode('-', $classPassId);
67
68
        if (count($exploded) !== 2) {
69 1
            return false;
70
        }
71
72
        if ($exploded[0] !== 'cp') {
73 1
            return false;
74
        }
75
76
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($exploded[1]);
77
    }
78
79
    /**
80
     *
81
     * {@inheritDoc}
82
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDate()
83
     */
84
    public function validDate(string $date): bool
85 1
    {
86
        if (v::stringType()->notEmpty()->numericVal()->length(8, 8)->date('Ymd')->validate($date)) {
87
            return true;
88 1
        } else {
89
            return v::stringType()->notEmpty()->numericVal()->length(14, 14)->dateTime('YmdHis')->validate($date);
90 1
        }
91
    }
92
93
    /**
94 1
     *
95
     * {@inheritDoc}
96
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validDetails()
97
     */
98
    public function validDetails($details): bool
99 1
    {
100
        return v::stringType()->notEmpty()->validate($details);
101
    }
102
103 1
    /**
104
     * @author Daniel Mullin [email protected]
105
     * @author Brandon Lubbehusen [email protected]
106
     *
107 1
     * @access public
108
     *
109
     * @param string $eventId
110
     * @return bool
111
     */
112
    public function validEventId(string $eventId): bool
113
    {
114
        $exploded = explode('-', $eventId);
115 14
116
        if (count($exploded) !== 3) {
117 14
            return false;
118 8
        }
119
120 6
        if ($exploded[0] !== 'ev') {
121
            return false;
122
        }
123
124
        // Syntax.
125
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
126
            return false;
127
        }
128
129 4
        return $this->validDate($exploded[2]);
130
    }
131 4
132
    /**
133
     *
134
     * {@inheritDoc}
135
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileName()
136
     */
137
    public function validFileName($fileName): bool
138
    {
139 2
        return v::stringType()->notEmpty()->validate($fileName);
140
    }
141 2
142
    /**
143
     *
144
     * {@inheritDoc}
145
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFileType()
146
     */
147
    public function validFileType(string $fileType): bool
148
    {
149 3
        return v::stringType()->notEmpty()->in(['jpg', 'jpeg', 'gif', 'png'])->validate(strtolower($fileType));
150
    }
151 3
152 3
    /**
153
     *
154
     * {@inheritDoc}
155
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validFrom()
156
     */
157
    public function validFrom(string $from, string $to = null): bool
158
    {
159
        if (!$this->validDate($from)) {
160
            return false;
161
        }
162
163
        $fromDate = new \DateTime($from);
164
165
        if (empty($to)) {
166
            return true;
167
        }
168
169
        if (!$this->validDate($to)) {
170
            return false;
171
        }
172
173
        $toDate = new \DateTime($to);
174
175
        // Compare if actual to date is greater than from.
176
        if ($fromDate > $toDate) {
177
            return false;
178
        }
179
180 28
        return true;
181
    }
182 28
183 13
    /**
184
     *
185
     * {@inheritDoc}
186 15
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validId()
187 15
     * @todo
188 1
     */
189
    public function validId(string $id, string $type): bool
190 14
    {
191 1
        if (!v::stringType()->notEmpty()->validate($id)) {
192
            return false;
193 13
        }
194 1
195
        if (!v::stringType()->regex('/\b(attachment|classPass|event|location|\ticket|)\b/')->validate($type)) {
196 12
            return false;
197 8
        }
198
199 12
        $validationMethod = 'valid' . ucFirst($type) . 'Id';
200
201
        return $this->$validationMethod($id);
202
    }
203
204
    /**
205
     *
206
     * @author Brandon Lubbehusen [email protected]
207
     *
208 4
     * {@inheritDoc}
209
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validInclude()
210 4
     */
211
    public function validInclude(bool $include): bool
212
    {
213
        return v::boolType()->validate($include);
214
    }
215
216
    /**
217
     *
218 11
     * @author Brandon Lubbehusen [email protected]
219
     *
220 11
     * {@inheritDoc}
221
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validLocation()
222
     */
223
    public function validLocation(string $location): bool
224
    {
225
        return v::stringType()->notEmpty()->validate($location);
226
    }
227
228 4
    /**
229
     *
230 4
     * @author Daniel Mullin [email protected]
231
     *
232
     * @access public
233
     *
234
     * @param string $locationId
235
     * @return bool
236
     */
237
    public function validLocationId(string $locationId): bool
238 5
    {
239
        return v::stringType()->notEmpty()->alnum()->length(12, 12)->validate($locationId);
240 5
    }
241
242
    /**
243
     *
244 5
     * {@inheritDoc}
245
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTag()
246 5
     */
247 5
    public function validTag(string $tag): bool
248
    {
249
        return v::stringType()->notEmpty()->alnum()->validate($tag);
250
    }
251
252
    /**
253
     * @author Daniel Mullin [email protected]
254
     * @author Brandon Lubbehusen [email protected]
255
     *
256
     * @access public
257
     *
258
     * @param string $ticketId
259
     * @return bool
260
     */
261
    public function validTicketId(string $ticketId): bool
262
    {
263
264
        $exploded = explode('-', $ticketId);
265
266 1
        if (count($exploded) !== 4) {
267
            return false;
268 1
        }
269
270
        if ($exploded[0] !== 'ti') {
271
            return false;
272
        }
273
274
        // Syntax.
275
        if (!v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[1])) {
276
            return false;
277
        }
278
279
        if (!$this->validDate($exploded[2])) {
280
            return false;
281
        }
282
283
        return v::stringType()->notEmpty()->alnum()->length(4, 4)->validate($exploded[3]);
284
    }
285
286
    /**
287
     *
288
     * {@inheritDoc}
289
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTitle()
290
     */
291
    public function validTitle(string $title): bool
292
    {
293
        return v::stringType()->notEmpty()->validate($title);
294
    }
295
296
    /**
297
     *
298
     * {@inheritDoc}
299
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validTo()
300
     */
301
    public function validTo(string $to, null | string $from = null): bool
302
    {
303
        if (!$this->validDate($to)) {
304
            return false;
305
        }
306
307
        $toDate = new \DateTime($to);
308
309
310
        if (empty($from)) {
311
            return true;
312
        }
313
314
        $fromDate = new \DateTime($from);
315
316
        if (!$this->validFrom($from)) {
317
            return false;
318
        }
319
        if ($toDate < $fromDate) {
320
            return false;
321
        }
322
323
        return true;
324
    }
325
326
    /**
327
     *
328
     * {@inheritDoc}
329
     * @see \InShore\Bookwhen\Interfaces\ValidatorInterface::validToken()
330
     */
331
    public function validToken(string $token): bool
332
    {
333
        return v::alnum()->validate($token);
334
    }
335
336
    /**
337
     *
338
     * @author Brandon Lubbehusen [email protected]
339
     *
340
     * @access public
341
     *
342
     * @param int $usageAllowance
343
     * @return bool
344
     */
345
    public function validUsageAllowance(int $usageAllowance): bool
346
    {
347
        return v::intType()->notEmpty()->alnum()->validate($usageAllowance);
348
    }
349
350
    /**
351
     *
352
     * @author Brandon Lubbehusen [email protected]
353
     *
354
     * @access public
355
     *
356
     * @param string $usageType
357
     * @return bool
358
     */
359
    public function validUsageType(string $usageType): bool
360
    {
361
        return v::regex('/\b(personal|any)\b/')->validate($usageType);
362
    }
363
364
    /**
365
     *
366
     * @author Brandon Lubbehusen [email protected]
367
     *
368
     * @access public
369
     *
370
     * @param int $useRestrictedForDays
371
     * @return bool
372
     */
373
    public function validuseRestrictedForDays(int $useRestrictedForDays): bool
374
    {
375
        return v::intType()->notEmpty()->alnum()->validate($useRestrictedForDays);
376
    }
377
}
378
379
// EOF!
380