Failed Conditions
Pull Request — master (#112)
by
unknown
15:38
created

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