Failed Conditions
Pull Request — master (#54)
by
unknown
01:44
created

Assert::isList()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.6

Importance

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