Failed Conditions
Pull Request — master (#94)
by Tobias
02:53
created

Assert::isAOf()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 2
nop 3
crap 4
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 nullOrNatural($value, $message = '')
33
 * @method static void nullOrBoolean($value, $message = '')
34
 * @method static void nullOrScalar($value, $message = '')
35
 * @method static void nullOrObject($value, $message = '')
36
 * @method static void nullOrResource($value, $type = null, $message = '')
37
 * @method static void nullOrIsCallable($value, $message = '')
38
 * @method static void nullOrIsArray($value, $message = '')
39
 * @method static void nullOrIsTraversable($value, $message = '')
40
 * @method static void nullOrIsArrayAccessible($value, $message = '')
41
 * @method static void nullOrIsCountable($value, $message = '')
42
 * @method static void nullOrIsIterable($value, $message = '')
43
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
44
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
45
 * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
46
 * @method static void nullOrIsAOf($value, $classes, $message = '')
47
 * @method static void nullOrIsAOfAny($value, $classes, $message = '')
48
 * @method static void nullOrNotAOf($value, $classes, $message = '')
49
 * @method static void nullOrIsEmpty($value, $message = '')
50
 * @method static void nullOrNotEmpty($value, $message = '')
51
 * @method static void nullOrTrue($value, $message = '')
52
 * @method static void nullOrFalse($value, $message = '')
53
 * @method static void nullOrIp($value, $message = '')
54
 * @method static void nullOrIpv4($value, $message = '')
55
 * @method static void nullOrIpv6($value, $message = '')
56
 * @method static void nullOrUniqueValues($values, $message = '')
57
 * @method static void nullOrEq($value, $expect, $message = '')
58
 * @method static void nullOrNotEq($value, $expect, $message = '')
59
 * @method static void nullOrSame($value, $expect, $message = '')
60
 * @method static void nullOrNotSame($value, $expect, $message = '')
61
 * @method static void nullOrGreaterThan($value, $limit, $message = '')
62
 * @method static void nullOrGreaterThanEq($value, $limit, $message = '')
63
 * @method static void nullOrLessThan($value, $limit, $message = '')
64
 * @method static void nullOrLessThanEq($value, $limit, $message = '')
65
 * @method static void nullOrRange($value, $min, $max, $message = '')
66
 * @method static void nullOrOneOf($value, $values, $message = '')
67
 * @method static void nullOrContains($value, $subString, $message = '')
68
 * @method static void nullOrNotContains($value, $subString, $message = '')
69
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
70
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
71
 * @method static void nullOrStartsWithLetter($value, $message = '')
72
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
73
 * @method static void nullOrRegex($value, $pattern, $message = '')
74
 * @method static void nullOrNotRegex($value, $pattern, $message = '')
75
 * @method static void nullOrAlpha($value, $message = '')
76
 * @method static void nullOrDigits($value, $message = '')
77
 * @method static void nullOrAlnum($value, $message = '')
78
 * @method static void nullOrLower($value, $message = '')
79
 * @method static void nullOrUpper($value, $message = '')
80
 * @method static void nullOrLength($value, $length, $message = '')
81
 * @method static void nullOrMinLength($value, $min, $message = '')
82
 * @method static void nullOrMaxLength($value, $max, $message = '')
83
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
84
 * @method static void nullOrFileExists($value, $message = '')
85
 * @method static void nullOrFile($value, $message = '')
86
 * @method static void nullOrDirectory($value, $message = '')
87
 * @method static void nullOrReadable($value, $message = '')
88
 * @method static void nullOrWritable($value, $message = '')
89
 * @method static void nullOrClassExists($value, $message = '')
90
 * @method static void nullOrSubclassOf($value, $class, $message = '')
91
 * @method static void nullOrInterfaceExists($value, $message = '')
92
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
93
 * @method static void nullOrPropertyExists($value, $property, $message = '')
94
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
95
 * @method static void nullOrMethodExists($value, $method, $message = '')
96
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
97
 * @method static void nullOrKeyExists($value, $key, $message = '')
98
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
99
 * @method static void nullOrCount($value, $key, $message = '')
100
 * @method static void nullOrMinCount($value, $min, $message = '')
101
 * @method static void nullOrMaxCount($value, $max, $message = '')
102
 * @method static void nullOrIsList($value, $message = '')
103
 * @method static void nullOrIsMap($value, $message = '')
104
 * @method static void nullOrCountBetween($value, $min, $max, $message = '')
105
 * @method static void nullOrUuid($values, $message = '')
106
 * @method static void nullOrThrows($expression, $class = 'Exception', $message = '')
107
 * @method static void allString($values, $message = '')
108
 * @method static void allStringNotEmpty($values, $message = '')
109
 * @method static void allInteger($values, $message = '')
110
 * @method static void allIntegerish($values, $message = '')
111
 * @method static void allFloat($values, $message = '')
112
 * @method static void allNumeric($values, $message = '')
113
 * @method static void allNatural($values, $message = '')
114
 * @method static void allBoolean($values, $message = '')
115
 * @method static void allScalar($values, $message = '')
116
 * @method static void allObject($values, $message = '')
117
 * @method static void allResource($values, $type = null, $message = '')
118
 * @method static void allIsCallable($values, $message = '')
119
 * @method static void allIsArray($values, $message = '')
120
 * @method static void allIsTraversable($values, $message = '')
121
 * @method static void allIsArrayAccessible($values, $message = '')
122
 * @method static void allIsCountable($values, $message = '')
123
 * @method static void allIsIterable($values, $message = '')
124
 * @method static void allIsInstanceOf($values, $class, $message = '')
125
 * @method static void allNotInstanceOf($values, $class, $message = '')
126
 * @method static void allIsInstanceOfAny($values, $classes, $message = '')
127
 * @method static void allIsAOf($values, $class, $message = '')
128
 * @method static void allIsAOfAny($values, $class, $message = '')
129
 * @method static void allNotAOf($values, $class, $message = '')
130
 * @method static void allNull($values, $message = '')
131
 * @method static void allNotNull($values, $message = '')
132
 * @method static void allIsEmpty($values, $message = '')
133
 * @method static void allNotEmpty($values, $message = '')
134
 * @method static void allTrue($values, $message = '')
135
 * @method static void allFalse($values, $message = '')
136
 * @method static void allIp($values, $message = '')
137
 * @method static void allIpv4($values, $message = '')
138
 * @method static void allIpv6($values, $message = '')
139
 * @method static void allUniqueValues($values, $message = '')
140
 * @method static void allEq($values, $expect, $message = '')
141
 * @method static void allNotEq($values, $expect, $message = '')
142
 * @method static void allSame($values, $expect, $message = '')
143
 * @method static void allNotSame($values, $expect, $message = '')
144
 * @method static void allGreaterThan($values, $limit, $message = '')
145
 * @method static void allGreaterThanEq($values, $limit, $message = '')
146
 * @method static void allLessThan($values, $limit, $message = '')
147
 * @method static void allLessThanEq($values, $limit, $message = '')
148
 * @method static void allRange($values, $min, $max, $message = '')
149
 * @method static void allOneOf($values, $values, $message = '')
150
 * @method static void allContains($values, $subString, $message = '')
151
 * @method static void allNotContains($values, $subString, $message = '')
152
 * @method static void allNotWhitespaceOnly($values, $message = '')
153
 * @method static void allStartsWith($values, $prefix, $message = '')
154
 * @method static void allStartsWithLetter($values, $message = '')
155
 * @method static void allEndsWith($values, $suffix, $message = '')
156
 * @method static void allRegex($values, $pattern, $message = '')
157
 * @method static void allNotRegex($values, $pattern, $message = '')
158
 * @method static void allAlpha($values, $message = '')
159
 * @method static void allDigits($values, $message = '')
160
 * @method static void allAlnum($values, $message = '')
161
 * @method static void allLower($values, $message = '')
162
 * @method static void allUpper($values, $message = '')
163
 * @method static void allLength($values, $length, $message = '')
164
 * @method static void allMinLength($values, $min, $message = '')
165
 * @method static void allMaxLength($values, $max, $message = '')
166
 * @method static void allLengthBetween($values, $min, $max, $message = '')
167
 * @method static void allFileExists($values, $message = '')
168
 * @method static void allFile($values, $message = '')
169
 * @method static void allDirectory($values, $message = '')
170
 * @method static void allReadable($values, $message = '')
171
 * @method static void allWritable($values, $message = '')
172
 * @method static void allClassExists($values, $message = '')
173
 * @method static void allSubclassOf($values, $class, $message = '')
174
 * @method static void allInterfaceExists($values, $message = '')
175
 * @method static void allImplementsInterface($values, $interface, $message = '')
176
 * @method static void allPropertyExists($values, $property, $message = '')
177
 * @method static void allPropertyNotExists($values, $property, $message = '')
178
 * @method static void allMethodExists($values, $method, $message = '')
179
 * @method static void allMethodNotExists($values, $method, $message = '')
180
 * @method static void allKeyExists($values, $key, $message = '')
181
 * @method static void allKeyNotExists($values, $key, $message = '')
182
 * @method static void allCount($values, $key, $message = '')
183
 * @method static void allMinCount($values, $min, $message = '')
184
 * @method static void allMaxCount($values, $max, $message = '')
185
 * @method static void allCountBetween($values, $min, $max, $message = '')
186
 * @method static void allIsList($values, $message = '')
187
 * @method static void allIsMap($values, $message = '')
188
 * @method static void allUuid($values, $message = '')
189
 * @method static void allThrows($expressions, $class = 'Exception', $message = '')
190
 *
191
 * @since  1.0
192
 *
193
 * @author Bernhard Schussek <[email protected]>
194
 */
195
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...
196
{
197 94
    public static function string($value, $message = '')
198
    {
199 94
        if (!is_string($value)) {
200 14
            static::reportInvalidArgument(sprintf(
201 14
                $message ?: 'Expected a string. Got: %s',
202 14
                static::typeToString($value)
203
            ));
204
        }
205 80
    }
206
207 16
    public static function stringNotEmpty($value, $message = '')
208
    {
209 16
        static::string($value, $message);
210 12
        static::notEq($value, '', $message);
211 8
    }
212
213 17
    public static function integer($value, $message = '')
214
    {
215 17
        if (!is_int($value)) {
216 13
            static::reportInvalidArgument(sprintf(
217 13
                $message ?: 'Expected an integer. Got: %s',
218 13
                static::typeToString($value)
219
            ));
220
        }
221 4
    }
222
223 16
    public static function integerish($value, $message = '')
224
    {
225 16
        if (!is_numeric($value) || $value != (int) $value) {
226 4
            static::reportInvalidArgument(sprintf(
227 4
                $message ?: 'Expected an integerish value. Got: %s',
228 4
                static::typeToString($value)
229
            ));
230
        }
231 12
    }
232
233 16
    public static function float($value, $message = '')
234
    {
235 16
        if (!is_float($value)) {
236 8
            static::reportInvalidArgument(sprintf(
237 8
                $message ?: 'Expected a float. Got: %s',
238 8
                static::typeToString($value)
239
            ));
240
        }
241 8
    }
242
243 20
    public static function numeric($value, $message = '')
244
    {
245 20
        if (!is_numeric($value)) {
246 4
            static::reportInvalidArgument(sprintf(
247 4
                $message ?: 'Expected a numeric. Got: %s',
248 4
                static::typeToString($value)
249
            ));
250
        }
251 16
    }
252
253 24
    public static function natural($value, $message = '')
254
    {
255 24
        if (!is_int($value) || $value < 0) {
256 16
            static::reportInvalidArgument(sprintf(
257 16
                $message ?: 'Expected a non-negative integer. Got %s',
258 16
                static::valueToString($value)
259
            ));
260
        }
261 8
    }
262
263 16
    public static function boolean($value, $message = '')
264
    {
265 16
        if (!is_bool($value)) {
266 8
            static::reportInvalidArgument(sprintf(
267 8
                $message ?: 'Expected a boolean. Got: %s',
268 8
                static::typeToString($value)
269
            ));
270
        }
271 8
    }
272
273 23
    public static function scalar($value, $message = '')
274
    {
275 23
        if (!is_scalar($value)) {
276 11
            static::reportInvalidArgument(sprintf(
277 11
                $message ?: 'Expected a scalar. Got: %s',
278 11
                static::typeToString($value)
279
            ));
280
        }
281 12
    }
282
283 23
    public static function object($value, $message = '')
284
    {
285 23
        if (!is_object($value)) {
286 15
            static::reportInvalidArgument(sprintf(
287 15
                $message ?: 'Expected an object. Got: %s',
288 15
                static::typeToString($value)
289
            ));
290
        }
291 8
    }
292
293 16
    public static function resource($value, $type = null, $message = '')
294
    {
295 16
        if (!is_resource($value)) {
296 4
            static::reportInvalidArgument(sprintf(
297 4
                $message ?: 'Expected a resource. Got: %s',
298 4
                static::typeToString($value)
299
            ));
300
        }
301
302 12
        if ($type && $type !== get_resource_type($value)) {
303 4
            static::reportInvalidArgument(sprintf(
304 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
305 4
                static::typeToString($value),
306 4
                $type
307
            ));
308
        }
309 8
    }
310
311 20
    public static function isCallable($value, $message = '')
312
    {
313 20
        if (!is_callable($value)) {
314 8
            static::reportInvalidArgument(sprintf(
315 8
                $message ?: 'Expected a callable. Got: %s',
316 8
                static::typeToString($value)
317
            ));
318
        }
319 12
    }
320
321 20
    public static function isArray($value, $message = '')
322
    {
323 20
        if (!is_array($value)) {
324 12
            static::reportInvalidArgument(sprintf(
325 12
                $message ?: 'Expected an array. Got: %s',
326 12
                static::typeToString($value)
327
            ));
328
        }
329 8
    }
330
331 20
    public static function isTraversable($value, $message = '')
332
    {
333 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...
334 20
            sprintf(
335 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.',
336 20
                __METHOD__
337
            ),
338 20
            E_USER_DEPRECATED
339
        );
340
341 20
        if (!is_array($value) && !($value instanceof Traversable)) {
342 8
            static::reportInvalidArgument(sprintf(
343 8
                $message ?: 'Expected a traversable. Got: %s',
344 8
                static::typeToString($value)
345
            ));
346
        }
347 12
    }
348
349 20 View Code Duplication
    public static function isArrayAccessible($value, $message = '')
350
    {
351 20
        if (!is_array($value) && !($value instanceof ArrayAccess)) {
352 8
            static::reportInvalidArgument(sprintf(
353 8
                $message ?: 'Expected an array accessible. Got: %s',
354 8
                static::typeToString($value)
355
            ));
356
        }
357 12
    }
358
359 24 View Code Duplication
    public static function isCountable($value, $message = '')
360
    {
361 24
        if (!is_array($value) && !($value instanceof Countable)) {
362 12
            static::reportInvalidArgument(sprintf(
363 12
                $message ?: 'Expected a countable. Got: %s',
364 12
                static::typeToString($value)
365
            ));
366
        }
367 12
    }
368
369 740 View Code Duplication
    public static function isIterable($value, $message = '')
370
    {
371 740
        if (!is_array($value) && !($value instanceof Traversable)) {
372 8
            static::reportInvalidArgument(sprintf(
373 8
                $message ?: 'Expected an iterable. Got: %s',
374 8
                static::typeToString($value)
375
            ));
376
        }
377 736
    }
378
379 16
    public static function isInstanceOf($value, $class, $message = '')
380
    {
381 16
        if (!($value instanceof $class)) {
382 12
            static::reportInvalidArgument(sprintf(
383 12
                $message ?: 'Expected an instance of %2$s. Got: %s',
384 12
                static::typeToString($value),
385 12
                $class
386
            ));
387
        }
388 4
    }
389
390 16
    public static function notInstanceOf($value, $class, $message = '')
391
    {
392 16
        if ($value instanceof $class) {
393 4
            static::reportInvalidArgument(sprintf(
394 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
395 4
                static::typeToString($value),
396 4
                $class
397
            ));
398
        }
399 12
    }
400
401 20
    public static function isInstanceOfAny($value, array $classes, $message = '')
402
    {
403 20
        foreach ($classes as $class) {
404 20
            if ($value instanceof $class) {
405 20
                return;
406
            }
407
        }
408
409 12
        static::reportInvalidArgument(sprintf(
410 12
            $message ?: 'Expected an instance of this class or to this class among his parents of any of %2$s. Got: %s',
411 12
            static::typeToString($value),
412 12
            implode(', ', array_map(array('static', 'valueToString'), $classes))
413
        ));
414
    }
415
416 16 View Code Duplication
    public static function isAOf($value, $class, $message = '')
417
    {
418 16
        if (!is_string($class) || !is_a($value, $class, is_string($value))) {
419 12
            static::reportInvalidArgument(sprintf(
420 12
                $message ?: 'Expected an instance of this class or to this class among his parents %2$s. Got: %s',
421 12
                static::typeToString($value),
422 12
                $class
423
            ));
424
        }
425 4
    }
426
427 16 View Code Duplication
    public static function notAOf($value, $class, $message = '')
428
    {
429 16
        if (!is_string($class) || is_a($value, $class, is_string($value))) {
430 4
            static::reportInvalidArgument(sprintf(
431 4
                $message ?: 'Expected an instance of this class or to this class among his parents other than %2$s. Got: %s',
432 4
                static::typeToString($value),
433 4
                $class
434
            ));
435
        }
436 12
    }
437
438 20
    public static function isAOfAny($value, array $classes, $message = '')
439
    {
440 20
        foreach ($classes as $class) {
441 20
            if (!is_string($class) || is_a($value, $class, is_string($value))) {
442 20
                return;
443
            }
444
        }
445
446 12
        static::reportInvalidArgument(sprintf(
447 12
            $message ?: 'Expected an any of instance of this class or to this class among his parents other than %2$s. Got: %s',
448 12
            static::typeToString($value),
449 12
            implode(', ', array_map(array('static', 'valueToString'), $classes))
450
        ));
451
    }
452
453 23
    public static function isEmpty($value, $message = '')
454
    {
455 23
        if (!empty($value)) {
456 8
            static::reportInvalidArgument(sprintf(
457 8
                $message ?: 'Expected an empty value. Got: %s',
458 8
                static::valueToString($value)
459
            ));
460
        }
461 15
    }
462
463 23
    public static function notEmpty($value, $message = '')
464
    {
465 23
        if (empty($value)) {
466 15
            static::reportInvalidArgument(sprintf(
467 15
                $message ?: 'Expected a non-empty value. Got: %s',
468 15
                static::valueToString($value)
469
            ));
470
        }
471 8
    }
472
473 11
    public static function null($value, $message = '')
474
    {
475 11
        if (null !== $value) {
476 8
            static::reportInvalidArgument(sprintf(
477 8
                $message ?: 'Expected null. Got: %s',
478 8
                static::valueToString($value)
479
            ));
480
        }
481 3
    }
482
483 11
    public static function notNull($value, $message = '')
484
    {
485 11
        if (null === $value) {
486 3
            static::reportInvalidArgument(
487 3
                $message ?: 'Expected a value other than null.'
488
            );
489
        }
490 8
    }
491
492 15
    public static function true($value, $message = '')
493
    {
494 15
        if (true !== $value) {
495 11
            static::reportInvalidArgument(sprintf(
496 11
                $message ?: 'Expected a value to be true. Got: %s',
497 11
                static::valueToString($value)
498
            ));
499
        }
500 4
    }
501
502 19
    public static function false($value, $message = '')
503
    {
504 19
        if (false !== $value) {
505 15
            static::reportInvalidArgument(sprintf(
506 15
                $message ?: 'Expected a value to be false. Got: %s',
507 15
                static::valueToString($value)
508
            ));
509
        }
510 4
    }
511
512 47 View Code Duplication
    public static function ip($value, $message = '')
513
    {
514 47
        if (false === filter_var($value, FILTER_VALIDATE_IP)) {
515 19
            static::reportInvalidArgument(sprintf(
516 19
                $message ?: 'Expected a value to be an IP. Got: %s',
517 19
                static::valueToString($value)
518
            ));
519
        }
520 28
    }
521
522 47 View Code Duplication
    public static function ipv4($value, $message = '')
523
    {
524 47
        if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
525 35
            static::reportInvalidArgument(sprintf(
526 35
                $message ?: 'Expected a value to be an IPv4. Got: %s',
527 35
                static::valueToString($value)
528
            ));
529
        }
530 12
    }
531
532 47 View Code Duplication
    public static function ipv6($value, $message = '')
533
    {
534 47
        if (false === filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
535 31
            static::reportInvalidArgument(sprintf(
536 31
                $message ?: 'Expected a value to be an IPv6. Got %s',
537 31
                static::valueToString($value)
538
            ));
539
        }
540 16
    }
541
542 12
    public static function uniqueValues(array $values, $message = '')
543
    {
544 12
        $allValues = count($values);
545 12
        $uniqueValues = count(array_unique($values));
546
547 12
        if ($allValues !== $uniqueValues) {
548 8
            $difference = $allValues - $uniqueValues;
549
550 8
            static::reportInvalidArgument(sprintf(
551 8
                $message ?: 'Expected an array of unique values, but %s of them %s duplicated',
552 8
                $difference,
553 8
                (1 === $difference ? 'is' : 'are')
554
            ));
555
        }
556 4
    }
557
558 33
    public static function eq($value, $expect, $message = '')
559
    {
560 33
        if ($expect != $value) {
561 17
            static::reportInvalidArgument(sprintf(
562 17
                $message ?: 'Expected a value equal to %2$s. Got: %s',
563 17
                static::valueToString($value),
564 17
                static::valueToString($expect)
565
            ));
566
        }
567 16
    }
568
569 28
    public static function notEq($value, $expect, $message = '')
570
    {
571 28
        if ($expect == $value) {
572 16
            static::reportInvalidArgument(sprintf(
573 16
                $message ?: 'Expected a different value than %s.',
574 16
                static::valueToString($expect)
575
            ));
576
        }
577 12
    }
578
579 16
    public static function same($value, $expect, $message = '')
580
    {
581 16
        if ($expect !== $value) {
582 12
            static::reportInvalidArgument(sprintf(
583 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
584 12
                static::valueToString($value),
585 12
                static::valueToString($expect)
586
            ));
587
        }
588 4
    }
589
590 16
    public static function notSame($value, $expect, $message = '')
591
    {
592 16
        if ($expect === $value) {
593 4
            static::reportInvalidArgument(sprintf(
594 4
                $message ?: 'Expected a value not identical to %s.',
595 4
                static::valueToString($expect)
596
            ));
597
        }
598 12
    }
599
600 8
    public static function greaterThan($value, $limit, $message = '')
601
    {
602 8
        if ($value <= $limit) {
603 4
            static::reportInvalidArgument(sprintf(
604 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
605 4
                static::valueToString($value),
606 4
                static::valueToString($limit)
607
            ));
608
        }
609 4
    }
610
611 12
    public static function greaterThanEq($value, $limit, $message = '')
612
    {
613 12
        if ($value < $limit) {
614 4
            static::reportInvalidArgument(sprintf(
615 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
616 4
                static::valueToString($value),
617 4
                static::valueToString($limit)
618
            ));
619
        }
620 8
    }
621
622 8
    public static function lessThan($value, $limit, $message = '')
623
    {
624 8
        if ($value >= $limit) {
625 4
            static::reportInvalidArgument(sprintf(
626 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
627 4
                static::valueToString($value),
628 4
                static::valueToString($limit)
629
            ));
630
        }
631 4
    }
632
633 12
    public static function lessThanEq($value, $limit, $message = '')
634
    {
635 12
        if ($value > $limit) {
636 4
            static::reportInvalidArgument(sprintf(
637 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
638 4
                static::valueToString($value),
639 4
                static::valueToString($limit)
640
            ));
641
        }
642 8
    }
643
644 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
645
    {
646 16
        if ($value < $min || $value > $max) {
647 8
            static::reportInvalidArgument(sprintf(
648 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
649 8
                static::valueToString($value),
650 8
                static::valueToString($min),
651 8
                static::valueToString($max)
652
            ));
653
        }
654 8
    }
655
656 8
    public static function oneOf($value, array $values, $message = '')
657
    {
658 8
        if (!in_array($value, $values, true)) {
659 4
            static::reportInvalidArgument(sprintf(
660 4
                $message ?: 'Expected one of: %2$s. Got: %s',
661 4
                static::valueToString($value),
662 4
                implode(', ', array_map(array('static', 'valueToString'), $values))
663
            ));
664
        }
665 4
    }
666
667 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
668
    {
669 20
        if (false === strpos($value, $subString)) {
670 8
            static::reportInvalidArgument(sprintf(
671 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
672 8
                static::valueToString($value),
673 8
                static::valueToString($subString)
674
            ));
675
        }
676 12
    }
677
678 20 View Code Duplication
    public static function notContains($value, $subString, $message = '')
679
    {
680 20
        if (false !== strpos($value, $subString)) {
681 12
            static::reportInvalidArgument(sprintf(
682 12
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
683 12
                static::valueToString($value),
684 12
                static::valueToString($subString)
685
            ));
686
        }
687 8
    }
688
689 40
    public static function notWhitespaceOnly($value, $message = '')
690
    {
691 40
        if (preg_match('/^\s*$/', $value)) {
692 24
            static::reportInvalidArgument(sprintf(
693 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
694 24
                static::valueToString($value)
695
            ));
696
        }
697 16
    }
698
699 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
700
    {
701 12
        if (0 !== strpos($value, $prefix)) {
702 8
            static::reportInvalidArgument(sprintf(
703 8
                $message ?: 'Expected a value to start with %2$s. Got: %s',
704 8
                static::valueToString($value),
705 8
                static::valueToString($prefix)
706
            ));
707
        }
708 4
    }
709
710 12
    public static function startsWithLetter($value, $message = '')
711
    {
712 12
        $valid = isset($value[0]);
713
714 12
        if ($valid) {
715 8
            $locale = setlocale(LC_CTYPE, 0);
716 8
            setlocale(LC_CTYPE, 'C');
717 8
            $valid = ctype_alpha($value[0]);
718 8
            setlocale(LC_CTYPE, $locale);
719
        }
720
721 12
        if (!$valid) {
722 8
            static::reportInvalidArgument(sprintf(
723 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
724 8
                static::valueToString($value)
725
            ));
726
        }
727 4
    }
728
729 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
730
    {
731 12
        if ($suffix !== substr($value, -static::strlen($suffix))) {
732 8
            static::reportInvalidArgument(sprintf(
733 8
                $message ?: 'Expected a value to end with %2$s. Got: %s',
734 8
                static::valueToString($value),
735 8
                static::valueToString($suffix)
736
            ));
737
        }
738 4
    }
739
740 12
    public static function regex($value, $pattern, $message = '')
741
    {
742 12
        if (!preg_match($pattern, $value)) {
743 8
            static::reportInvalidArgument(sprintf(
744 8
                $message ?: 'The value %s does not match the expected pattern.',
745 8
                static::valueToString($value)
746
            ));
747
        }
748 4
    }
749
750 12
    public static function notRegex($value, $pattern, $message = '')
751
    {
752 12
        if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
753 4
            static::reportInvalidArgument(sprintf(
754 4
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
755 4
                static::valueToString($value),
756 4
                static::valueToString($pattern),
757 4
                $matches[0][1]
758
            ));
759
        }
760 8
    }
761
762 12 View Code Duplication
    public static function alpha($value, $message = '')
763
    {
764 12
        $locale = setlocale(LC_CTYPE, 0);
765 12
        setlocale(LC_CTYPE, 'C');
766 12
        $valid = !ctype_alpha($value);
767 12
        setlocale(LC_CTYPE, $locale);
768
769 12
        if ($valid) {
770 8
            static::reportInvalidArgument(sprintf(
771 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
772 8
                static::valueToString($value)
773
            ));
774
        }
775 4
    }
776
777 12 View Code Duplication
    public static function digits($value, $message = '')
778
    {
779 12
        $locale = setlocale(LC_CTYPE, 0);
780 12
        setlocale(LC_CTYPE, 'C');
781 12
        $valid = !ctype_digit($value);
782 12
        setlocale(LC_CTYPE, $locale);
783
784 12
        if ($valid) {
785 8
            static::reportInvalidArgument(sprintf(
786 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
787 8
                static::valueToString($value)
788
            ));
789
        }
790 4
    }
791
792 12 View Code Duplication
    public static function alnum($value, $message = '')
793
    {
794 12
        $locale = setlocale(LC_CTYPE, 0);
795 12
        setlocale(LC_CTYPE, 'C');
796 12
        $valid = !ctype_alnum($value);
797 12
        setlocale(LC_CTYPE, $locale);
798
799 12
        if ($valid) {
800 8
            static::reportInvalidArgument(sprintf(
801 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
802 8
                static::valueToString($value)
803
            ));
804
        }
805 4
    }
806
807 16 View Code Duplication
    public static function lower($value, $message = '')
808
    {
809 16
        $locale = setlocale(LC_CTYPE, 0);
810 16
        setlocale(LC_CTYPE, 'C');
811 16
        $valid = !ctype_lower($value);
812 16
        setlocale(LC_CTYPE, $locale);
813
814 16
        if ($valid) {
815 12
            static::reportInvalidArgument(sprintf(
816 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
817 12
                static::valueToString($value)
818
            ));
819
        }
820 4
    }
821
822 16 View Code Duplication
    public static function upper($value, $message = '')
823
    {
824 16
        $locale = setlocale(LC_CTYPE, 0);
825 16
        setlocale(LC_CTYPE, 'C');
826 16
        $valid = !ctype_upper($value);
827 16
        setlocale(LC_CTYPE, $locale);
828
829 16
        if ($valid) {
830 12
            static::reportInvalidArgument(sprintf(
831 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
832 12
                static::valueToString($value)
833
            ));
834
        }
835 4
    }
836
837 24
    public static function length($value, $length, $message = '')
838
    {
839 24
        if ($length !== static::strlen($value)) {
840 16
            static::reportInvalidArgument(sprintf(
841 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
842 16
                static::valueToString($value),
843 16
                $length
844
            ));
845
        }
846 8
    }
847
848 24
    public static function minLength($value, $min, $message = '')
849
    {
850 24
        if (static::strlen($value) < $min) {
851 8
            static::reportInvalidArgument(sprintf(
852 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
853 8
                static::valueToString($value),
854 8
                $min
855
            ));
856
        }
857 16
    }
858
859 24
    public static function maxLength($value, $max, $message = '')
860
    {
861 24
        if (static::strlen($value) > $max) {
862 8
            static::reportInvalidArgument(sprintf(
863 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
864 8
                static::valueToString($value),
865 8
                $max
866
            ));
867
        }
868 16
    }
869
870 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
871
    {
872 40
        $length = static::strlen($value);
873
874 40
        if ($length < $min || $length > $max) {
875 16
            static::reportInvalidArgument(sprintf(
876 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
877 16
                static::valueToString($value),
878 16
                $min,
879 16
                $max
880
            ));
881
        }
882 24
    }
883
884 36
    public static function fileExists($value, $message = '')
885
    {
886 36
        static::string($value);
887
888 36
        if (!file_exists($value)) {
889 12
            static::reportInvalidArgument(sprintf(
890 12
                $message ?: 'The file %s does not exist.',
891 12
                static::valueToString($value)
892
            ));
893
        }
894 24
    }
895
896 12 View Code Duplication
    public static function file($value, $message = '')
897
    {
898 12
        static::fileExists($value, $message);
899
900 8
        if (!is_file($value)) {
901 4
            static::reportInvalidArgument(sprintf(
902 4
                $message ?: 'The path %s is not a file.',
903 4
                static::valueToString($value)
904
            ));
905
        }
906 4
    }
907
908 12 View Code Duplication
    public static function directory($value, $message = '')
909
    {
910 12
        static::fileExists($value, $message);
911
912 8
        if (!is_dir($value)) {
913 4
            static::reportInvalidArgument(sprintf(
914 4
                $message ?: 'The path %s is no directory.',
915 4
                static::valueToString($value)
916
            ));
917
        }
918 4
    }
919
920
    public static function readable($value, $message = '')
921
    {
922
        if (!is_readable($value)) {
923
            static::reportInvalidArgument(sprintf(
924
                $message ?: 'The path %s is not readable.',
925
                static::valueToString($value)
926
            ));
927
        }
928
    }
929
930
    public static function writable($value, $message = '')
931
    {
932
        if (!is_writable($value)) {
933
            static::reportInvalidArgument(sprintf(
934
                $message ?: 'The path %s is not writable.',
935
                static::valueToString($value)
936
            ));
937
        }
938
    }
939
940 8
    public static function classExists($value, $message = '')
941
    {
942 8
        if (!class_exists($value)) {
943 4
            static::reportInvalidArgument(sprintf(
944 4
                $message ?: 'Expected an existing class name. Got: %s',
945 4
                static::valueToString($value)
946
            ));
947
        }
948 4
    }
949
950 8
    public static function subclassOf($value, $class, $message = '')
951
    {
952 8
        if (!is_subclass_of($value, $class)) {
953 4
            static::reportInvalidArgument(sprintf(
954 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
955 4
                static::valueToString($value),
956 4
                static::valueToString($class)
957
            ));
958
        }
959 4
    }
960
961 8
    public static function interfaceExists($value, $message = '')
962
    {
963 8
        if (!interface_exists($value)) {
964 4
            static::reportInvalidArgument(sprintf(
965 4
                $message ?: 'Expected an existing interface name. got %s',
966 4
                static::valueToString($value)
967
            ));
968
        }
969 4
    }
970
971 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
972
    {
973 8
        if (!in_array($interface, class_implements($value))) {
974 4
            static::reportInvalidArgument(sprintf(
975 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
976 4
                static::valueToString($value),
977 4
                static::valueToString($interface)
978
            ));
979
        }
980 4
    }
981
982 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
983
    {
984 12
        if (!property_exists($classOrObject, $property)) {
985 4
            static::reportInvalidArgument(sprintf(
986 4
                $message ?: 'Expected the property %s to exist.',
987 4
                static::valueToString($property)
988
            ));
989
        }
990 8
    }
991
992 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
993
    {
994 12
        if (property_exists($classOrObject, $property)) {
995 8
            static::reportInvalidArgument(sprintf(
996 8
                $message ?: 'Expected the property %s to not exist.',
997 8
                static::valueToString($property)
998
            ));
999
        }
1000 4
    }
1001
1002 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
1003
    {
1004 27
        if (!method_exists($classOrObject, $method)) {
1005 19
            static::reportInvalidArgument(sprintf(
1006 19
                $message ?: 'Expected the method %s to exist.',
1007 19
                static::valueToString($method)
1008
            ));
1009
        }
1010 8
    }
1011
1012 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
1013
    {
1014 27
        if (method_exists($classOrObject, $method)) {
1015 8
            static::reportInvalidArgument(sprintf(
1016 8
                $message ?: 'Expected the method %s to not exist.',
1017 8
                static::valueToString($method)
1018
            ));
1019
        }
1020 19
    }
1021
1022 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
1023
    {
1024 12
        if (!(isset($array[$key]) || array_key_exists($key, $array))) {
1025 4
            static::reportInvalidArgument(sprintf(
1026 4
                $message ?: 'Expected the key %s to exist.',
1027 4
                static::valueToString($key)
1028
            ));
1029
        }
1030 8
    }
1031
1032 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
1033
    {
1034 12
        if (isset($array[$key]) || array_key_exists($key, $array)) {
1035 8
            static::reportInvalidArgument(sprintf(
1036 8
                $message ?: 'Expected the key %s to not exist.',
1037 8
                static::valueToString($key)
1038
            ));
1039
        }
1040 4
    }
1041
1042 8
    public static function count($array, $number, $message = '')
1043
    {
1044 8
        static::eq(
1045 8
            count($array),
1046 8
            $number,
1047 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
1048
        );
1049 4
    }
1050
1051 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
1052
    {
1053 12
        if (count($array) < $min) {
1054 4
            static::reportInvalidArgument(sprintf(
1055 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
1056 4
                count($array),
1057 4
                $min
1058
            ));
1059
        }
1060 8
    }
1061
1062 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
1063
    {
1064 12
        if (count($array) > $max) {
1065 4
            static::reportInvalidArgument(sprintf(
1066 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
1067 4
                count($array),
1068 4
                $max
1069
            ));
1070
        }
1071 8
    }
1072
1073 12
    public static function countBetween($array, $min, $max, $message = '')
1074
    {
1075 12
        $count = count($array);
1076
1077 12
        if ($count < $min || $count > $max) {
1078 8
            static::reportInvalidArgument(sprintf(
1079 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
1080 8
                $count,
1081 8
                $min,
1082 8
                $max
1083
            ));
1084
        }
1085 4
    }
1086
1087 16
    public static function isList($array, $message = '')
1088
    {
1089 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...
1090 12
            static::reportInvalidArgument(
1091 12
                $message ?: 'Expected list - non-associative array.'
1092
            );
1093
        }
1094 4
    }
1095
1096 16
    public static function isMap($array, $message = '')
1097
    {
1098
        if (
1099 16
            !is_array($array) ||
1100 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...
1101 16
            array_keys($array) !== array_filter(array_keys($array), function ($key) {
1102 12
                return is_string($key);
1103 16
            })
1104
        ) {
1105 12
            static::reportInvalidArgument(
1106 12
                $message ?: 'Expected map - associative array with string keys.'
1107
            );
1108
        }
1109 4
    }
1110
1111 48
    public static function uuid($value, $message = '')
1112
    {
1113 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1114
1115
        // The nil UUID is special form of UUID that is specified to have all
1116
        // 128 bits set to zero.
1117 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1118 4
            return;
1119
        }
1120
1121 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)) {
1122 20
            static::reportInvalidArgument(sprintf(
1123 20
                $message ?: 'Value %s is not a valid UUID.',
1124 20
                static::valueToString($value)
1125
            ));
1126
        }
1127 24
    }
1128
1129 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1130
    {
1131 24
        static::string($class);
1132
1133 24
        $actual = 'none';
1134
1135
        try {
1136 24
            $expression();
1137 24
        } catch (Exception $e) {
1138 20
            $actual = get_class($e);
1139 20
            if ($e instanceof $class) {
1140 20
                return;
1141
            }
1142 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...
1143 4
            $actual = get_class($e);
1144 4
            if ($e instanceof $class) {
1145 4
                return;
1146
            }
1147
        }
1148
1149 8
        static::reportInvalidArgument($message ?: sprintf(
1150 8
            'Expected to throw "%s", got "%s"',
1151 8
            $class,
1152 8
            $actual
1153
        ));
1154
    }
1155
1156 1176
    public static function __callStatic($name, $arguments)
1157
    {
1158 1176
        if ('nullOr' === substr($name, 0, 6)) {
1159 446
            if (null !== $arguments[0]) {
1160 352
                $method = lcfirst(substr($name, 6));
1161 352
                call_user_func_array(array('static', $method), $arguments);
1162
            }
1163
1164 264
            return;
1165
        }
1166
1167 730
        if ('all' === substr($name, 0, 3)) {
1168 730
            static::isIterable($arguments[0]);
1169
1170 730
            $method = lcfirst(substr($name, 3));
1171 730
            $args = $arguments;
1172
1173 730
            foreach ($arguments[0] as $entry) {
1174 730
                $args[0] = $entry;
1175
1176 730
                call_user_func_array(array('static', $method), $args);
1177
            }
1178
1179 346
            return;
1180
        }
1181
1182
        throw new BadMethodCallException('No such method: '.$name);
1183
    }
1184
1185 542
    protected static function valueToString($value)
1186
    {
1187 542
        if (null === $value) {
1188 20
            return 'null';
1189
        }
1190
1191 524
        if (true === $value) {
1192 15
            return 'true';
1193
        }
1194
1195 514
        if (false === $value) {
1196 25
            return 'false';
1197
        }
1198
1199 489
        if (is_array($value)) {
1200 13
            return 'array';
1201
        }
1202
1203 476
        if (is_object($value)) {
1204 2
            if (method_exists($value, '__toString')) {
1205 1
                return get_class($value).': '.self::valueToString($value->__toString());
1206
            }
1207
1208 1
            return get_class($value);
1209
        }
1210
1211 475
        if (is_resource($value)) {
1212 1
            return 'resource';
1213
        }
1214
1215 475
        if (is_string($value)) {
1216 377
            return '"'.$value.'"';
1217
        }
1218
1219 106
        return (string) $value;
1220
    }
1221
1222 197
    protected static function typeToString($value)
1223
    {
1224 197
        return is_object($value) ? get_class($value) : gettype($value);
1225
    }
1226
1227 124
    protected static function strlen($value)
1228
    {
1229 124
        if (!function_exists('mb_detect_encoding')) {
1230
            return strlen($value);
1231
        }
1232
1233 124
        if (false === $encoding = mb_detect_encoding($value)) {
1234
            return strlen($value);
1235
        }
1236
1237 124
        return mb_strwidth($value, $encoding);
1238
    }
1239
1240 774
    protected static function reportInvalidArgument($message)
1241
    {
1242 774
        throw new InvalidArgumentException($message);
1243
    }
1244
1245
    private function __construct()
1246
    {
1247
    }
1248
}
1249