Completed
Pull Request — master (#88)
by Dmitriy
20:12
created

Assert::ip()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 3

Importance

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