Failed Conditions
Pull Request — master (#93)
by David
02:06
created

Assert::uniqueValues()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

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

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1053 12
            static::reportInvalidArgument(
1054 12
                $message ?: 'Expected list - non-associative array.'
1055
            );
1056
        }
1057 4
    }
1058
1059 16
    public static function isMap($array, $message = '')
1060
    {
1061
        if (
1062 16
            !is_array($array) ||
1063 16
            !$array ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1064 16
            array_keys($array) !== array_filter(array_keys($array), function ($key) {
1065 12
                return is_string($key);
1066 16
            })
1067
        ) {
1068 12
            static::reportInvalidArgument(
1069 12
                $message ?: 'Expected map - associative array with string keys.'
1070
            );
1071
        }
1072 4
    }
1073
1074 48
    public static function uuid($value, $message = '')
1075
    {
1076 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1077
1078
        // The nil UUID is special form of UUID that is specified to have all
1079
        // 128 bits set to zero.
1080 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1081 4
            return;
1082
        }
1083
1084 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)) {
1085 20
            static::reportInvalidArgument(sprintf(
1086 20
                $message ?: 'Value %s is not a valid UUID.',
1087 20
                static::valueToString($value)
1088
            ));
1089
        }
1090 24
    }
1091
1092 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1093
    {
1094 24
        static::string($class);
1095
1096 24
        $actual = 'none';
1097
1098
        try {
1099 24
            $expression();
1100 24
        } catch (Exception $e) {
1101 20
            $actual = get_class($e);
1102 20
            if ($e instanceof $class) {
1103 20
                return;
1104
            }
1105 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...
1106 4
            $actual = get_class($e);
1107 4
            if ($e instanceof $class) {
1108 4
                return;
1109
            }
1110
        }
1111
1112 8
        static::reportInvalidArgument($message ?: sprintf(
1113 8
            'Expected to throw "%s", got "%s"',
1114 8
            $class,
1115 8
            $actual
1116
        ));
1117
    }
1118
1119 1131
    public static function __callStatic($name, $arguments)
1120
    {
1121 1131
        if ('nullOr' === substr($name, 0, 6)) {
1122 429
            if (null !== $arguments[0]) {
1123 338
                $method = lcfirst(substr($name, 6));
1124 338
                call_user_func_array(array('static', $method), $arguments);
1125
            }
1126
1127 255
            return;
1128
        }
1129
1130 702
        if ('all' === substr($name, 0, 3)) {
1131 702
            static::isIterable($arguments[0]);
1132
1133 702
            $method = lcfirst(substr($name, 3));
1134 702
            $args = $arguments;
1135
1136 702
            foreach ($arguments[0] as $entry) {
1137 702
                $args[0] = $entry;
1138
1139 702
                call_user_func_array(array('static', $method), $args);
1140
            }
1141
1142 334
            return;
1143
        }
1144
1145
        throw new BadMethodCallException('No such method: '.$name);
1146
    }
1147
1148 530
    protected static function valueToString($value)
1149
    {
1150 530
        if (null === $value) {
1151 20
            return 'null';
1152
        }
1153
1154 512
        if (true === $value) {
1155 15
            return 'true';
1156
        }
1157
1158 502
        if (false === $value) {
1159 25
            return 'false';
1160
        }
1161
1162 477
        if (is_array($value)) {
1163 13
            return 'array';
1164
        }
1165
1166 464
        if (is_object($value)) {
1167 2
            if (method_exists($value, '__toString')) {
1168 1
                return get_class($value).': '.self::valueToString($value->__toString());
1169
            }
1170
1171 1
            return get_class($value);
1172
        }
1173
1174 463
        if (is_resource($value)) {
1175 1
            return 'resource';
1176
        }
1177
1178 463
        if (is_string($value)) {
1179 365
            return '"'.$value.'"';
1180
        }
1181
1182 106
        return (string) $value;
1183
    }
1184
1185 169
    protected static function typeToString($value)
1186
    {
1187 169
        return is_object($value) ? get_class($value) : gettype($value);
1188
    }
1189
1190 124
    protected static function strlen($value)
1191
    {
1192 124
        if (!function_exists('mb_detect_encoding')) {
1193
            return strlen($value);
1194
        }
1195
1196 124
        if (false === $encoding = mb_detect_encoding($value)) {
1197
            return strlen($value);
1198
        }
1199
1200 124
        return mb_strwidth($value, $encoding);
1201
    }
1202
1203 738
    protected static function reportInvalidArgument($message)
1204
    {
1205 738
        throw new InvalidArgumentException($message);
1206
    }
1207
1208
    private function __construct()
1209
    {
1210
    }
1211
}
1212