Failed Conditions
Pull Request — master (#54)
by Tobias
01:31
created

Assert   D

Complexity

Total Complexity 267

Size/Duplication

Total Lines 961
Duplicated Lines 27.78 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.72%

Importance

Changes 0
Metric Value
wmc 267
lcom 1
cbo 0
dl 267
loc 961
ccs 561
cts 580
cp 0.9672
rs 4.4444
c 0
b 0
f 0

81 Methods

Rating   Name   Duplication   Size   Complexity  
A string() 0 9 3
A stringNotEmpty() 0 5 1
A integer() 0 9 3
A integerish() 0 9 4
A float() 0 9 3
A numeric() 0 9 3
A natural() 0 9 4
A boolean() 0 9 3
A scalar() 0 9 3
A object() 0 9 3
B resource() 0 17 6
A isCallable() 0 9 3
A isArray() 0 9 3
A isTraversable() 0 17 4
A isArrayAccessible() 9 9 4
A isCountable() 9 9 4
A isIterable() 9 9 4
A isInstanceOf() 0 10 3
A notInstanceOf() 0 10 3
A isInstanceOfAny() 0 14 4
A isEmpty() 0 9 3
A notEmpty() 0 9 3
A null() 0 9 3
A notNull() 0 8 3
A true() 0 9 3
A false() 0 9 3
A eq() 0 10 3
A notEq() 0 9 3
A same() 0 10 3
A notSame() 0 9 3
A greaterThan() 0 10 3
A greaterThanEq() 0 10 3
A lessThan() 0 10 3
A lessThanEq() 0 10 3
A range() 11 11 4
A oneOf() 0 10 3
A contains() 10 10 3
A notContains() 10 10 3
A notWhitespaceOnly() 0 9 3
A startsWith() 10 10 3
A startsWithLetter() 0 18 4
A endsWith() 10 10 3
A regex() 0 9 3
A notRegex() 0 11 3
A alpha() 14 14 3
A digits() 14 14 3
A alnum() 14 14 3
A lower() 14 14 3
A upper() 14 14 3
A length() 0 10 3
A minLength() 0 10 3
A maxLength() 0 10 3
A lengthBetween() 13 13 4
A fileExists() 0 11 3
A file() 11 11 3
A directory() 11 11 3
A readable() 0 9 3
A writable() 0 9 3
A classExists() 0 9 3
A subclassOf() 0 10 3
A implementsInterface() 10 10 3
A propertyExists() 9 9 3
A propertyNotExists() 9 9 3
A methodExists() 9 9 3
A methodNotExists() 9 9 3
A keyExists() 9 9 4
A keyNotExists() 9 9 4
A count() 0 8 2
A minCount() 10 10 3
A maxCount() 10 10 3
A countBetween() 0 13 4
B isList() 0 8 5
B isMap() 0 14 5
A uuid() 0 17 4
B throws() 0 26 6
B __callStatic() 0 28 5
D valueToString() 0 36 9
A typeToString() 0 4 2
A strlen() 0 12 3
A reportInvalidArgument() 0 4 1
A __construct() 0 3 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Assert 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 Assert, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the webmozart/assert package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Assert;
13
14
use ArrayAccess;
15
use BadMethodCallException;
16
use Closure;
17
use Countable;
18
use Exception;
19
use InvalidArgumentException;
20
use Throwable;
21
use Traversable;
22
23
/**
24
 * Efficient assertions to validate the input/output of your methods.
25
 *
26
 * @method static void nullOrString($value, $message = '')
27
 * @method static void nullOrStringNotEmpty($value, $message = '')
28
 * @method static void nullOrInteger($value, $message = '')
29
 * @method static void nullOrIntegerish($value, $message = '')
30
 * @method static void nullOrFloat($value, $message = '')
31
 * @method static void nullOrNumeric($value, $message = '')
32
 * @method static void nullOrBoolean($value, $message = '')
33
 * @method static void nullOrScalar($value, $message = '')
34
 * @method static void nullOrObject($value, $message = '')
35
 * @method static void nullOrResource($value, $type = null, $message = '')
36
 * @method static void nullOrIsCallable($value, $message = '')
37
 * @method static void nullOrIsArray($value, $message = '')
38
 * @method static void nullOrIsTraversable($value, $message = '')
39
 * @method static void nullOrIsArrayAccessible($value, $message = '')
40
 * @method static void nullOrIsCountable($value, $message = '')
41
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
42
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
43
 * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
44
 * @method static void nullOrIsEmpty($value, $message = '')
45
 * @method static void nullOrNotEmpty($value, $message = '')
46
 * @method static void nullOrTrue($value, $message = '')
47
 * @method static void nullOrFalse($value, $message = '')
48
 * @method static void nullOrEq($value, $value2, $message = '')
49
 * @method static void nullOrNotEq($value,$value2,  $message = '')
50
 * @method static void nullOrSame($value, $value2, $message = '')
51
 * @method static void nullOrNotSame($value, $value2, $message = '')
52
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
53
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
54
 * @method static void nullOrLessThan($value, $value2, $message = '')
55
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
56
 * @method static void nullOrRange($value, $min, $max, $message = '')
57
 * @method static void nullOrOneOf($value, $values, $message = '')
58
 * @method static void nullOrContains($value, $subString, $message = '')
59
 * @method static void nullOrNotContains($value, $subString, $message = '')
60
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
61
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
62
 * @method static void nullOrStartsWithLetter($value, $message = '')
63
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
64
 * @method static void nullOrRegex($value, $pattern, $message = '')
65
 * @method static void nullOrNotRegex($value, $pattern, $message = '')
66
 * @method static void nullOrAlpha($value, $message = '')
67
 * @method static void nullOrDigits($value, $message = '')
68
 * @method static void nullOrAlnum($value, $message = '')
69
 * @method static void nullOrLower($value, $message = '')
70
 * @method static void nullOrUpper($value, $message = '')
71
 * @method static void nullOrLength($value, $length, $message = '')
72
 * @method static void nullOrMinLength($value, $min, $message = '')
73
 * @method static void nullOrMaxLength($value, $max, $message = '')
74
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
75
 * @method static void nullOrFileExists($value, $message = '')
76
 * @method static void nullOrFile($value, $message = '')
77
 * @method static void nullOrDirectory($value, $message = '')
78
 * @method static void nullOrReadable($value, $message = '')
79
 * @method static void nullOrWritable($value, $message = '')
80
 * @method static void nullOrClassExists($value, $message = '')
81
 * @method static void nullOrSubclassOf($value, $class, $message = '')
82
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
83
 * @method static void nullOrPropertyExists($value, $property, $message = '')
84
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
85
 * @method static void nullOrMethodExists($value, $method, $message = '')
86
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
87
 * @method static void nullOrKeyExists($value, $key, $message = '')
88
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
89
 * @method static void nullOrCount($value, $key, $message = '')
90
 * @method static void nullOrMinCount($value, $min, $message = '')
91
 * @method static void nullOrMaxCount($value, $max, $message = '')
92
 * @method static void nullCountBetween($value, $min, $max, $message = '')
93
 * @method static void nullOrIsList($value, $message = '')
94
 * @method static void nullOrIsMap($value, $message = '')
95
 * @method static void nullOrUuid($values, $message = '')
96
 * @method static void allString($values, $message = '')
97
 * @method static void allStringNotEmpty($values, $message = '')
98
 * @method static void allInteger($values, $message = '')
99
 * @method static void allIntegerish($values, $message = '')
100
 * @method static void allFloat($values, $message = '')
101
 * @method static void allNumeric($values, $message = '')
102
 * @method static void allBoolean($values, $message = '')
103
 * @method static void allScalar($values, $message = '')
104
 * @method static void allObject($values, $message = '')
105
 * @method static void allResource($values, $type = null, $message = '')
106
 * @method static void allIsCallable($values, $message = '')
107
 * @method static void allIsArray($values, $message = '')
108
 * @method static void allIsTraversable($values, $message = '')
109
 * @method static void allIsArrayAccessible($values, $message = '')
110
 * @method static void allIsCountable($values, $message = '')
111
 * @method static void allIsInstanceOf($values, $class, $message = '')
112
 * @method static void allNotInstanceOf($values, $class, $message = '')
113
 * @method static void allIsInstanceOfAny($values, $classes, $message = '')
114
 * @method static void allNull($values, $message = '')
115
 * @method static void allNotNull($values, $message = '')
116
 * @method static void allIsEmpty($values, $message = '')
117
 * @method static void allNotEmpty($values, $message = '')
118
 * @method static void allTrue($values, $message = '')
119
 * @method static void allFalse($values, $message = '')
120
 * @method static void allEq($values, $value2, $message = '')
121
 * @method static void allNotEq($values,$value2,  $message = '')
122
 * @method static void allSame($values, $value2, $message = '')
123
 * @method static void allNotSame($values, $value2, $message = '')
124
 * @method static void allGreaterThan($values, $value2, $message = '')
125
 * @method static void allGreaterThanEq($values, $value2, $message = '')
126
 * @method static void allLessThan($values, $value2, $message = '')
127
 * @method static void allLessThanEq($values, $value2, $message = '')
128
 * @method static void allRange($values, $min, $max, $message = '')
129
 * @method static void allOneOf($values, $values, $message = '')
130
 * @method static void allContains($values, $subString, $message = '')
131
 * @method static void allNotContains($values, $subString, $message = '')
132
 * @method static void allNotWhitespaceOnly($values, $message = '')
133
 * @method static void allStartsWith($values, $prefix, $message = '')
134
 * @method static void allStartsWithLetter($values, $message = '')
135
 * @method static void allEndsWith($values, $suffix, $message = '')
136
 * @method static void allRegex($values, $pattern, $message = '')
137
 * @method static void allNotRegex($values, $pattern, $message = '')
138
 * @method static void allAlpha($values, $message = '')
139
 * @method static void allDigits($values, $message = '')
140
 * @method static void allAlnum($values, $message = '')
141
 * @method static void allLower($values, $message = '')
142
 * @method static void allUpper($values, $message = '')
143
 * @method static void allLength($values, $length, $message = '')
144
 * @method static void allMinLength($values, $min, $message = '')
145
 * @method static void allMaxLength($values, $max, $message = '')
146
 * @method static void allLengthBetween($values, $min, $max, $message = '')
147
 * @method static void allFileExists($values, $message = '')
148
 * @method static void allFile($values, $message = '')
149
 * @method static void allDirectory($values, $message = '')
150
 * @method static void allReadable($values, $message = '')
151
 * @method static void allWritable($values, $message = '')
152
 * @method static void allClassExists($values, $message = '')
153
 * @method static void allSubclassOf($values, $class, $message = '')
154
 * @method static void allImplementsInterface($values, $interface, $message = '')
155
 * @method static void allPropertyExists($values, $property, $message = '')
156
 * @method static void allPropertyNotExists($values, $property, $message = '')
157
 * @method static void allMethodExists($values, $method, $message = '')
158
 * @method static void allMethodNotExists($values, $method, $message = '')
159
 * @method static void allKeyExists($values, $key, $message = '')
160
 * @method static void allKeyNotExists($values, $key, $message = '')
161
 * @method static void allCount($values, $key, $message = '')
162
 * @method static void allMinCount($values, $min, $message = '')
163
 * @method static void allMaxCount($values, $max, $message = '')
164
 * @method static void allCountBetween($values, $min, $max, $message = '')
165
 * @method static void allIsList($values, $message = '')
166
 * @method static void allIsMap($values, $message = '')
167
 * @method static void allUuid($values, $message = '')
168
 *
169
 * @since  1.0
170
 *
171
 * @author Bernhard Schussek <[email protected]>
172
 */
173
class Assert
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
174
{
175 94
    public static function string($value, $message = '')
176
    {
177 94
        if (!is_string($value)) {
178 14
            static::reportInvalidArgument(sprintf(
179 14
                $message ?: 'Expected a string. Got: %s',
180 14
                static::typeToString($value)
181
            ));
182
        }
183 80
    }
184
185 16
    public static function stringNotEmpty($value, $message = '')
186
    {
187 16
        static::string($value, $message);
188 12
        static::notEq($value, '', $message);
189 8
    }
190
191 17
    public static function integer($value, $message = '')
192
    {
193 17
        if (!is_int($value)) {
194 13
            static::reportInvalidArgument(sprintf(
195 13
                $message ?: 'Expected an integer. Got: %s',
196 13
                static::typeToString($value)
197
            ));
198
        }
199 4
    }
200
201 16
    public static function integerish($value, $message = '')
202
    {
203 16
        if (!is_numeric($value) || $value != (int) $value) {
204 4
            static::reportInvalidArgument(sprintf(
205 4
                $message ?: 'Expected an integerish value. Got: %s',
206 4
                static::typeToString($value)
207
            ));
208
        }
209 12
    }
210
211 16
    public static function float($value, $message = '')
212
    {
213 16
        if (!is_float($value)) {
214 8
            static::reportInvalidArgument(sprintf(
215 8
                $message ?: 'Expected a float. Got: %s',
216 8
                static::typeToString($value)
217
            ));
218
        }
219 8
    }
220
221 20
    public static function numeric($value, $message = '')
222
    {
223 20
        if (!is_numeric($value)) {
224 4
            static::reportInvalidArgument(sprintf(
225 4
                $message ?: 'Expected a numeric. Got: %s',
226 4
                static::typeToString($value)
227
            ));
228
        }
229 16
    }
230
231 24
    public static function natural($value, $message = '')
232
    {
233 24
        if (!is_int($value) || $value < 0) {
234 16
            static::reportInvalidArgument(sprintf(
235 16
                $message ?: 'Expected a non-negative integer. Got %s',
236 16
                static::valueToString($value)
237
            ));
238
        }
239 8
    }
240
241 16
    public static function boolean($value, $message = '')
242
    {
243 16
        if (!is_bool($value)) {
244 8
            static::reportInvalidArgument(sprintf(
245 8
                $message ?: 'Expected a boolean. Got: %s',
246 8
                static::typeToString($value)
247
            ));
248
        }
249 8
    }
250
251 23
    public static function scalar($value, $message = '')
252
    {
253 23
        if (!is_scalar($value)) {
254 11
            static::reportInvalidArgument(sprintf(
255 11
                $message ?: 'Expected a scalar. Got: %s',
256 11
                static::typeToString($value)
257
            ));
258
        }
259 12
    }
260
261 23
    public static function object($value, $message = '')
262
    {
263 23
        if (!is_object($value)) {
264 15
            static::reportInvalidArgument(sprintf(
265 15
                $message ?: 'Expected an object. Got: %s',
266 15
                static::typeToString($value)
267
            ));
268
        }
269 8
    }
270
271 16
    public static function resource($value, $type = null, $message = '')
272
    {
273 16
        if (!is_resource($value)) {
274 4
            static::reportInvalidArgument(sprintf(
275 4
                $message ?: 'Expected a resource. Got: %s',
276 4
                static::typeToString($value)
277
            ));
278
        }
279
280 12
        if ($type && $type !== get_resource_type($value)) {
281 4
            static::reportInvalidArgument(sprintf(
282 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
283 4
                static::typeToString($value),
284 4
                $type
285
            ));
286
        }
287 8
    }
288
289 20
    public static function isCallable($value, $message = '')
290
    {
291 20
        if (!is_callable($value)) {
292 8
            static::reportInvalidArgument(sprintf(
293 8
                $message ?: 'Expected a callable. Got: %s',
294 8
                static::typeToString($value)
295
            ));
296
        }
297 12
    }
298
299 20
    public static function isArray($value, $message = '')
300
    {
301 20
        if (!is_array($value)) {
302 12
            static::reportInvalidArgument(sprintf(
303 12
                $message ?: 'Expected an array. Got: %s',
304 12
                static::typeToString($value)
305
            ));
306
        }
307 8
    }
308
309 20
    public static function isTraversable($value, $message = '')
310
    {
311 20
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
312 20
            sprintf(
313 20
                'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.',
314 20
                __METHOD__
315
            ),
316 20
            E_USER_DEPRECATED
317
        );
318
319 20
        if (!is_array($value) && !($value instanceof Traversable)) {
320 8
            static::reportInvalidArgument(sprintf(
321 8
                $message ?: 'Expected a traversable. Got: %s',
322 8
                static::typeToString($value)
323
            ));
324
        }
325 12
    }
326
327 20 View Code Duplication
    public static function isArrayAccessible($value, $message = '')
328
    {
329 20
        if (!is_array($value) && !($value instanceof ArrayAccess)) {
330 8
            static::reportInvalidArgument(sprintf(
331 8
                $message ?: 'Expected an array accessible. Got: %s',
332 8
                static::typeToString($value)
333
            ));
334
        }
335 12
    }
336
337 24 View Code Duplication
    public static function isCountable($value, $message = '')
338
    {
339 24
        if (!is_array($value) && !($value instanceof Countable)) {
340 12
            static::reportInvalidArgument(sprintf(
341 12
                $message ?: 'Expected a countable. Got: %s',
342 12
                static::typeToString($value)
343
            ));
344
        }
345 12
    }
346
347 632 View Code Duplication
    public static function isIterable($value, $message = '')
348
    {
349 632
        if (!is_array($value) && !($value instanceof Traversable)) {
350 8
            static::reportInvalidArgument(sprintf(
351 8
                $message ?: 'Expected an iterable. Got: %s',
352 8
                static::typeToString($value)
353
            ));
354
        }
355 628
    }
356
357 16
    public static function isInstanceOf($value, $class, $message = '')
358
    {
359 16
        if (!($value instanceof $class)) {
360 12
            static::reportInvalidArgument(sprintf(
361 12
                $message ?: 'Expected an instance of %2$s. Got: %s',
362 12
                static::typeToString($value),
363 12
                $class
364
            ));
365
        }
366 4
    }
367
368 16
    public static function notInstanceOf($value, $class, $message = '')
369
    {
370 16
        if ($value instanceof $class) {
371 4
            static::reportInvalidArgument(sprintf(
372 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
373 4
                static::typeToString($value),
374 4
                $class
375
            ));
376
        }
377 12
    }
378
379 20
    public static function isInstanceOfAny($value, array $classes, $message = '')
380
    {
381 20
        foreach ($classes as $class) {
382 20
            if ($value instanceof $class) {
383 20
                return;
384
            }
385
        }
386
387 12
        static::reportInvalidArgument(sprintf(
388 12
            $message ?: 'Expected an instance of any of %2$s. Got: %s',
389 12
            static::typeToString($value),
390 12
            implode(', ', array_map(array('static', 'valueToString'), $classes))
391
        ));
392
    }
393
394 23
    public static function isEmpty($value, $message = '')
395
    {
396 23
        if (!empty($value)) {
397 8
            static::reportInvalidArgument(sprintf(
398 8
                $message ?: 'Expected an empty value. Got: %s',
399 8
                static::valueToString($value)
400
            ));
401
        }
402 15
    }
403
404 23
    public static function notEmpty($value, $message = '')
405
    {
406 23
        if (empty($value)) {
407 15
            static::reportInvalidArgument(sprintf(
408 15
                $message ?: 'Expected a non-empty value. Got: %s',
409 15
                static::valueToString($value)
410
            ));
411
        }
412 8
    }
413
414 11
    public static function null($value, $message = '')
415
    {
416 11
        if (null !== $value) {
417 8
            static::reportInvalidArgument(sprintf(
418 8
                $message ?: 'Expected null. Got: %s',
419 8
                static::valueToString($value)
420
            ));
421
        }
422 3
    }
423
424 11
    public static function notNull($value, $message = '')
425
    {
426 11
        if (null === $value) {
427 3
            static::reportInvalidArgument(
428 3
                $message ?: 'Expected a value other than null.'
429
            );
430
        }
431 8
    }
432
433 15
    public static function true($value, $message = '')
434
    {
435 15
        if (true !== $value) {
436 11
            static::reportInvalidArgument(sprintf(
437 11
                $message ?: 'Expected a value to be true. Got: %s',
438 11
                static::valueToString($value)
439
            ));
440
        }
441 4
    }
442
443 19
    public static function false($value, $message = '')
444
    {
445 19
        if (false !== $value) {
446 15
            static::reportInvalidArgument(sprintf(
447 15
                $message ?: 'Expected a value to be false. Got: %s',
448 15
                static::valueToString($value)
449
            ));
450
        }
451 4
    }
452
453 33
    public static function eq($value, $value2, $message = '')
454
    {
455 33
        if ($value2 != $value) {
456 17
            static::reportInvalidArgument(sprintf(
457 17
                $message ?: 'Expected a value equal to %2$s. Got: %s',
458 17
                static::valueToString($value),
459 17
                static::valueToString($value2)
460
            ));
461
        }
462 16
    }
463
464 28
    public static function notEq($value, $value2, $message = '')
465
    {
466 28
        if ($value2 == $value) {
467 16
            static::reportInvalidArgument(sprintf(
468 16
                $message ?: 'Expected a different value than %s.',
469 16
                static::valueToString($value2)
470
            ));
471
        }
472 12
    }
473
474 16
    public static function same($value, $value2, $message = '')
475
    {
476 16
        if ($value2 !== $value) {
477 12
            static::reportInvalidArgument(sprintf(
478 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
479 12
                static::valueToString($value),
480 12
                static::valueToString($value2)
481
            ));
482
        }
483 4
    }
484
485 16
    public static function notSame($value, $value2, $message = '')
486
    {
487 16
        if ($value2 === $value) {
488 4
            static::reportInvalidArgument(sprintf(
489 4
                $message ?: 'Expected a value not identical to %s.',
490 4
                static::valueToString($value2)
491
            ));
492
        }
493 12
    }
494
495 8
    public static function greaterThan($value, $limit, $message = '')
496
    {
497 8
        if ($value <= $limit) {
498 4
            static::reportInvalidArgument(sprintf(
499 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
500 4
                static::valueToString($value),
501 4
                static::valueToString($limit)
502
            ));
503
        }
504 4
    }
505
506 12
    public static function greaterThanEq($value, $limit, $message = '')
507
    {
508 12
        if ($value < $limit) {
509 4
            static::reportInvalidArgument(sprintf(
510 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
511 4
                static::valueToString($value),
512 4
                static::valueToString($limit)
513
            ));
514
        }
515 8
    }
516
517 8
    public static function lessThan($value, $limit, $message = '')
518
    {
519 8
        if ($value >= $limit) {
520 4
            static::reportInvalidArgument(sprintf(
521 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
522 4
                static::valueToString($value),
523 4
                static::valueToString($limit)
524
            ));
525
        }
526 4
    }
527
528 12
    public static function lessThanEq($value, $limit, $message = '')
529
    {
530 12
        if ($value > $limit) {
531 4
            static::reportInvalidArgument(sprintf(
532 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
533 4
                static::valueToString($value),
534 4
                static::valueToString($limit)
535
            ));
536
        }
537 8
    }
538
539 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
540
    {
541 16
        if ($value < $min || $value > $max) {
542 8
            static::reportInvalidArgument(sprintf(
543 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
544 8
                static::valueToString($value),
545 8
                static::valueToString($min),
546 8
                static::valueToString($max)
547
            ));
548
        }
549 8
    }
550
551 8
    public static function oneOf($value, array $values, $message = '')
552
    {
553 8
        if (!in_array($value, $values, true)) {
554 4
            static::reportInvalidArgument(sprintf(
555 4
                $message ?: 'Expected one of: %2$s. Got: %s',
556 4
                static::valueToString($value),
557 4
                implode(', ', array_map(array('static', 'valueToString'), $values))
558
            ));
559
        }
560 4
    }
561
562 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
563
    {
564 20
        if (false === strpos($value, $subString)) {
565 8
            static::reportInvalidArgument(sprintf(
566 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
567 8
                static::valueToString($value),
568 8
                static::valueToString($subString)
569
            ));
570
        }
571 12
    }
572
573 20 View Code Duplication
    public static function notContains($value, $subString, $message = '')
574
    {
575 20
        if (false !== strpos($value, $subString)) {
576 12
            static::reportInvalidArgument(sprintf(
577 12
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
578 12
                static::valueToString($value),
579 12
                static::valueToString($subString)
580
            ));
581
        }
582 8
    }
583
584 40
    public static function notWhitespaceOnly($value, $message = '')
585
    {
586 40
        if (preg_match('/^\s*$/', $value)) {
587 24
            static::reportInvalidArgument(sprintf(
588 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
589 24
                static::valueToString($value)
590
            ));
591
        }
592 16
    }
593
594 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
595
    {
596 12
        if (0 !== strpos($value, $prefix)) {
597 8
            static::reportInvalidArgument(sprintf(
598 8
                $message ?: 'Expected a value to start with %2$s. Got: %s',
599 8
                static::valueToString($value),
600 8
                static::valueToString($prefix)
601
            ));
602
        }
603 4
    }
604
605 12
    public static function startsWithLetter($value, $message = '')
606
    {
607 12
        $valid = isset($value[0]);
608
609 12
        if ($valid) {
610 8
            $locale = setlocale(LC_CTYPE, 0);
611 8
            setlocale(LC_CTYPE, 'C');
612 8
            $valid = ctype_alpha($value[0]);
613 8
            setlocale(LC_CTYPE, $locale);
614
        }
615
616 12
        if (!$valid) {
617 8
            static::reportInvalidArgument(sprintf(
618 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
619 8
                static::valueToString($value)
620
            ));
621
        }
622 4
    }
623
624 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
625
    {
626 12
        if ($suffix !== substr($value, -static::strlen($suffix))) {
627 8
            static::reportInvalidArgument(sprintf(
628 8
                $message ?: 'Expected a value to end with %2$s. Got: %s',
629 8
                static::valueToString($value),
630 8
                static::valueToString($suffix)
631
            ));
632
        }
633 4
    }
634
635 12
    public static function regex($value, $pattern, $message = '')
636
    {
637 12
        if (!preg_match($pattern, $value)) {
638 8
            static::reportInvalidArgument(sprintf(
639 8
                $message ?: 'The value %s does not match the expected pattern.',
640 8
                static::valueToString($value)
641
            ));
642
        }
643 4
    }
644
645 12
    public static function notRegex($value, $pattern, $message = '')
646
    {
647 12
        if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
648 4
            static::reportInvalidArgument(sprintf(
649 4
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
650 4
                static::valueToString($value),
651 4
                static::valueToString($pattern),
652 4
                $matches[0][1]
653
            ));
654
        }
655 8
    }
656
657 12 View Code Duplication
    public static function alpha($value, $message = '')
658
    {
659 12
        $locale = setlocale(LC_CTYPE, 0);
660 12
        setlocale(LC_CTYPE, 'C');
661 12
        $valid = !ctype_alpha($value);
662 12
        setlocale(LC_CTYPE, $locale);
663
664 12
        if ($valid) {
665 8
            static::reportInvalidArgument(sprintf(
666 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
667 8
                static::valueToString($value)
668
            ));
669
        }
670 4
    }
671
672 12 View Code Duplication
    public static function digits($value, $message = '')
673
    {
674 12
        $locale = setlocale(LC_CTYPE, 0);
675 12
        setlocale(LC_CTYPE, 'C');
676 12
        $valid = !ctype_digit($value);
677 12
        setlocale(LC_CTYPE, $locale);
678
679 12
        if ($valid) {
680 8
            static::reportInvalidArgument(sprintf(
681 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
682 8
                static::valueToString($value)
683
            ));
684
        }
685 4
    }
686
687 12 View Code Duplication
    public static function alnum($value, $message = '')
688
    {
689 12
        $locale = setlocale(LC_CTYPE, 0);
690 12
        setlocale(LC_CTYPE, 'C');
691 12
        $valid = !ctype_alnum($value);
692 12
        setlocale(LC_CTYPE, $locale);
693
694 12
        if ($valid) {
695 8
            static::reportInvalidArgument(sprintf(
696 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
697 8
                static::valueToString($value)
698
            ));
699
        }
700 4
    }
701
702 16 View Code Duplication
    public static function lower($value, $message = '')
703
    {
704 16
        $locale = setlocale(LC_CTYPE, 0);
705 16
        setlocale(LC_CTYPE, 'C');
706 16
        $valid = !ctype_lower($value);
707 16
        setlocale(LC_CTYPE, $locale);
708
709 16
        if ($valid) {
710 12
            static::reportInvalidArgument(sprintf(
711 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
712 12
                static::valueToString($value)
713
            ));
714
        }
715 4
    }
716
717 16 View Code Duplication
    public static function upper($value, $message = '')
718
    {
719 16
        $locale = setlocale(LC_CTYPE, 0);
720 16
        setlocale(LC_CTYPE, 'C');
721 16
        $valid = !ctype_upper($value);
722 16
        setlocale(LC_CTYPE, $locale);
723
724 16
        if ($valid) {
725 12
            static::reportInvalidArgument(sprintf(
726 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
727 12
                static::valueToString($value)
728
            ));
729
        }
730 4
    }
731
732 24
    public static function length($value, $length, $message = '')
733
    {
734 24
        if ($length !== static::strlen($value)) {
735 16
            static::reportInvalidArgument(sprintf(
736 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
737 16
                static::valueToString($value),
738 16
                $length
739
            ));
740
        }
741 8
    }
742
743 24
    public static function minLength($value, $min, $message = '')
744
    {
745 24
        if (static::strlen($value) < $min) {
746 8
            static::reportInvalidArgument(sprintf(
747 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
748 8
                static::valueToString($value),
749 8
                $min
750
            ));
751
        }
752 16
    }
753
754 24
    public static function maxLength($value, $max, $message = '')
755
    {
756 24
        if (static::strlen($value) > $max) {
757 8
            static::reportInvalidArgument(sprintf(
758 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
759 8
                static::valueToString($value),
760 8
                $max
761
            ));
762
        }
763 16
    }
764
765 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
766
    {
767 40
        $length = static::strlen($value);
768
769 40
        if ($length < $min || $length > $max) {
770 16
            static::reportInvalidArgument(sprintf(
771 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
772 16
                static::valueToString($value),
773 16
                $min,
774 16
                $max
775
            ));
776
        }
777 24
    }
778
779 36
    public static function fileExists($value, $message = '')
780
    {
781 36
        static::string($value);
782
783 36
        if (!file_exists($value)) {
784 12
            static::reportInvalidArgument(sprintf(
785 12
                $message ?: 'The file %s does not exist.',
786 12
                static::valueToString($value)
787
            ));
788
        }
789 24
    }
790
791 12 View Code Duplication
    public static function file($value, $message = '')
792
    {
793 12
        static::fileExists($value, $message);
794
795 8
        if (!is_file($value)) {
796 4
            static::reportInvalidArgument(sprintf(
797 4
                $message ?: 'The path %s is not a file.',
798 4
                static::valueToString($value)
799
            ));
800
        }
801 4
    }
802
803 12 View Code Duplication
    public static function directory($value, $message = '')
804
    {
805 12
        static::fileExists($value, $message);
806
807 8
        if (!is_dir($value)) {
808 4
            static::reportInvalidArgument(sprintf(
809 4
                $message ?: 'The path %s is no directory.',
810 4
                static::valueToString($value)
811
            ));
812
        }
813 4
    }
814
815
    public static function readable($value, $message = '')
816
    {
817
        if (!is_readable($value)) {
818
            static::reportInvalidArgument(sprintf(
819
                $message ?: 'The path %s is not readable.',
820
                static::valueToString($value)
821
            ));
822
        }
823
    }
824
825
    public static function writable($value, $message = '')
826
    {
827
        if (!is_writable($value)) {
828
            static::reportInvalidArgument(sprintf(
829
                $message ?: 'The path %s is not writable.',
830
                static::valueToString($value)
831
            ));
832
        }
833
    }
834
835 8
    public static function classExists($value, $message = '')
836
    {
837 8
        if (!class_exists($value)) {
838 4
            static::reportInvalidArgument(sprintf(
839 4
                $message ?: 'Expected an existing class name. Got: %s',
840 4
                static::valueToString($value)
841
            ));
842
        }
843 4
    }
844
845 8
    public static function subclassOf($value, $class, $message = '')
846
    {
847 8
        if (!is_subclass_of($value, $class)) {
848 4
            static::reportInvalidArgument(sprintf(
849 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
850 4
                static::valueToString($value),
851 4
                static::valueToString($class)
852
            ));
853
        }
854 4
    }
855
856 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
857
    {
858 8
        if (!in_array($interface, class_implements($value))) {
859 4
            static::reportInvalidArgument(sprintf(
860 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
861 4
                static::valueToString($value),
862 4
                static::valueToString($interface)
863
            ));
864
        }
865 4
    }
866
867 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
868
    {
869 12
        if (!property_exists($classOrObject, $property)) {
870 4
            static::reportInvalidArgument(sprintf(
871 4
                $message ?: 'Expected the property %s to exist.',
872 4
                static::valueToString($property)
873
            ));
874
        }
875 8
    }
876
877 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
878
    {
879 12
        if (property_exists($classOrObject, $property)) {
880 8
            static::reportInvalidArgument(sprintf(
881 8
                $message ?: 'Expected the property %s to not exist.',
882 8
                static::valueToString($property)
883
            ));
884
        }
885 4
    }
886
887 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
888
    {
889 27
        if (!method_exists($classOrObject, $method)) {
890 19
            static::reportInvalidArgument(sprintf(
891 19
                $message ?: 'Expected the method %s to exist.',
892 19
                static::valueToString($method)
893
            ));
894
        }
895 8
    }
896
897 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
898
    {
899 27
        if (method_exists($classOrObject, $method)) {
900 8
            static::reportInvalidArgument(sprintf(
901 8
                $message ?: 'Expected the method %s to not exist.',
902 8
                static::valueToString($method)
903
            ));
904
        }
905 19
    }
906
907 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
908
    {
909 12
        if (!(isset($array[$key]) || array_key_exists($key, $array))) {
910 4
            static::reportInvalidArgument(sprintf(
911 4
                $message ?: 'Expected the key %s to exist.',
912 4
                static::valueToString($key)
913
            ));
914
        }
915 8
    }
916
917 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
918
    {
919 12
        if (isset($array[$key]) || array_key_exists($key, $array)) {
920 8
            static::reportInvalidArgument(sprintf(
921 8
                $message ?: 'Expected the key %s to not exist.',
922 8
                static::valueToString($key)
923
            ));
924
        }
925 4
    }
926
927 8
    public static function count($array, $number, $message = '')
928
    {
929 8
        static::eq(
930 8
            count($array),
931 8
            $number,
932 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
933
        );
934 4
    }
935
936 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
937
    {
938 12
        if (count($array) < $min) {
939 4
            static::reportInvalidArgument(sprintf(
940 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
941 4
                count($array),
942 4
                $min
943
            ));
944
        }
945 8
    }
946
947 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
948
    {
949 12
        if (count($array) > $max) {
950 4
            static::reportInvalidArgument(sprintf(
951 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
952 4
                count($array),
953 4
                $max
954
            ));
955
        }
956 8
    }
957
958 12
    public static function countBetween($array, $min, $max, $message = '')
959
    {
960 12
        $count = count($array);
961
962 12
        if ($count < $min || $count > $max) {
963 8
            static::reportInvalidArgument(sprintf(
964 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
965 8
                $count,
966 8
                $min,
967 8
                $max
968
            ));
969
        }
970 4
    }
971
972 16
    public static function isList($array, $message = '')
973
    {
974 16
        if (!is_array($array) || !$array || array_keys($array) !== range(0, count($array) - 1)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
975 12
            static::reportInvalidArgument(
976 12
                $message ?: 'Expected list - non-associative array.'
977
            );
978
        }
979 4
    }
980
981 16
    public static function isMap($array, $message = '')
982
    {
983
        if (
984 16
            !is_array($array) ||
985 16
            !$array ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
986 16
            array_keys($array) !== array_filter(array_keys($array), function ($key) {
987 12
                return is_string($key);
988 16
            })
989
        ) {
990 12
            static::reportInvalidArgument(
991 12
                $message ?: 'Expected map - associative array with string keys.'
992
            );
993
        }
994 4
    }
995
996 48
    public static function uuid($value, $message = '')
997
    {
998 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
999
1000
        // The nil UUID is special form of UUID that is specified to have all
1001
        // 128 bits set to zero.
1002 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1003 4
            return;
1004
        }
1005
1006 44
        if (!preg_match('/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$/', $value)) {
1007 20
            static::reportInvalidArgument(sprintf(
1008 20
                $message ?: 'Value %s is not a valid UUID.',
1009 20
                static::valueToString($value)
1010
            ));
1011
        }
1012 24
    }
1013
1014 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1015
    {
1016 24
        static::string($class);
1017
1018 24
        $actual = 'none';
1019
1020
        try {
1021 24
            $expression();
1022 24
        } catch (Exception $e) {
1023 20
            $actual = get_class($e);
1024 20
            if ($e instanceof $class) {
1025 20
                return;
1026
            }
1027 4
        } catch (Throwable $e) {
0 ignored issues
show
Bug introduced by
The class Throwable 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...
1028 4
            $actual = get_class($e);
1029 4
            if ($e instanceof $class) {
1030 4
                return;
1031
            }
1032
        }
1033
1034 8
        static::reportInvalidArgument($message ?: sprintf(
1035 8
            'Expected to throw "%s", got "%s"',
1036 8
            $class,
1037 8
            $actual
1038
        ));
1039
    }
1040
1041 1006
    public static function __callStatic($name, $arguments)
1042
    {
1043 1006
        if ('nullOr' === substr($name, 0, 6)) {
1044 384
            if (null !== $arguments[0]) {
1045 301
                $method = lcfirst(substr($name, 6));
1046 301
                call_user_func_array(array('static', $method), $arguments);
1047
            }
1048
1049 231
            return;
1050
        }
1051
1052 622
        if ('all' === substr($name, 0, 3)) {
1053 622
            static::isIterable($arguments[0]);
1054
1055 622
            $method = lcfirst(substr($name, 3));
1056 622
            $args = $arguments;
1057
1058 622
            foreach ($arguments[0] as $entry) {
1059 622
                $args[0] = $entry;
1060
1061 622
                call_user_func_array(array('static', $method), $args);
1062
            }
1063
1064 302
            return;
1065
        }
1066
1067
        throw new BadMethodCallException('No such method: '.$name);
1068
    }
1069
1070 441
    protected static function valueToString($value)
1071
    {
1072 441
        if (null === $value) {
1073 11
            return 'null';
1074
        }
1075
1076 432
        if (true === $value) {
1077 15
            return 'true';
1078
        }
1079
1080 422
        if (false === $value) {
1081 13
            return 'false';
1082
        }
1083
1084 409
        if (is_array($value)) {
1085 1
            return 'array';
1086
        }
1087
1088 408
        if (is_object($value)) {
1089 2
            if (method_exists($value, '__toString')) {
1090 1
                return get_class($value).': '.self::valueToString($value->__toString());
1091
            }
1092
1093 1
            return get_class($value);
1094
        }
1095
1096 407
        if (is_resource($value)) {
1097 1
            return 'resource';
1098
        }
1099
1100 407
        if (is_string($value)) {
1101 321
            return '"'.$value.'"';
1102
        }
1103
1104 94
        return (string) $value;
1105
    }
1106
1107 169
    protected static function typeToString($value)
1108
    {
1109 169
        return is_object($value) ? get_class($value) : gettype($value);
1110
    }
1111
1112 124
    protected static function strlen($value)
1113
    {
1114 124
        if (!function_exists('mb_detect_encoding')) {
1115
            return strlen($value);
1116
        }
1117
1118 124
        if (false === $encoding = mb_detect_encoding($value)) {
1119
            return strlen($value);
1120
        }
1121
1122 124
        return mb_strwidth($value, $encoding);
1123
    }
1124
1125 649
    protected static function reportInvalidArgument($message)
1126
    {
1127 649
        throw new InvalidArgumentException($message);
1128
    }
1129
1130
    private function __construct()
1131
    {
1132
    }
1133
}
1134