Failed Conditions
Pull Request — master (#58)
by Baptiste
01:37
created

Assert::notRegex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

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