Completed
Pull Request — master (#88)
by Dmitriy
20:52 queued 19:17
created

Assert::eq()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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