Failed Conditions
Pull Request — master (#94)
by
unknown
05:03 queued 55s
created

Assert::isAOfAny()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

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