Failed Conditions
Pull Request — master (#58)
by Baptiste
02:19
created

Assert::notRegex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

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.4285
c 0
b 0
f 0
cc 3
eloc 6
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 (at offset %d).',
646 4
                static::valueToString($value),
647 4
                $matches[0][1]
648
            ));
649
        }
650 8
    }
651
652 12 View Code Duplication
    public static function alpha($value, $message = '')
653
    {
654 12
        $locale = setlocale(LC_CTYPE, 0);
655 12
        setlocale(LC_CTYPE, 'C');
656 12
        $valid = !ctype_alpha($value);
657 12
        setlocale(LC_CTYPE, $locale);
658
659 12
        if ($valid) {
660 8
            static::reportInvalidArgument(sprintf(
661 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
662 8
                static::valueToString($value)
663
            ));
664
        }
665 4
    }
666
667 12 View Code Duplication
    public static function digits($value, $message = '')
668
    {
669 12
        $locale = setlocale(LC_CTYPE, 0);
670 12
        setlocale(LC_CTYPE, 'C');
671 12
        $valid = !ctype_digit($value);
672 12
        setlocale(LC_CTYPE, $locale);
673
674 12
        if ($valid) {
675 8
            static::reportInvalidArgument(sprintf(
676 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
677 8
                static::valueToString($value)
678
            ));
679
        }
680 4
    }
681
682 12 View Code Duplication
    public static function alnum($value, $message = '')
683
    {
684 12
        $locale = setlocale(LC_CTYPE, 0);
685 12
        setlocale(LC_CTYPE, 'C');
686 12
        $valid = !ctype_alnum($value);
687 12
        setlocale(LC_CTYPE, $locale);
688
689 12
        if ($valid) {
690 8
            static::reportInvalidArgument(sprintf(
691 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
692 8
                static::valueToString($value)
693
            ));
694
        }
695 4
    }
696
697 16 View Code Duplication
    public static function lower($value, $message = '')
698
    {
699 16
        $locale = setlocale(LC_CTYPE, 0);
700 16
        setlocale(LC_CTYPE, 'C');
701 16
        $valid = !ctype_lower($value);
702 16
        setlocale(LC_CTYPE, $locale);
703
704 16
        if ($valid) {
705 12
            static::reportInvalidArgument(sprintf(
706 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
707 12
                static::valueToString($value)
708
            ));
709
        }
710 4
    }
711
712 16 View Code Duplication
    public static function upper($value, $message = '')
713
    {
714 16
        $locale = setlocale(LC_CTYPE, 0);
715 16
        setlocale(LC_CTYPE, 'C');
716 16
        $valid = !ctype_upper($value);
717 16
        setlocale(LC_CTYPE, $locale);
718
719 16
        if ($valid) {
720 12
            static::reportInvalidArgument(sprintf(
721 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
722 12
                static::valueToString($value)
723
            ));
724
        }
725 4
    }
726
727 24
    public static function length($value, $length, $message = '')
728
    {
729 24
        if ($length !== static::strlen($value)) {
730 16
            static::reportInvalidArgument(sprintf(
731 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
732 16
                static::valueToString($value),
733 16
                $length
734
            ));
735
        }
736 8
    }
737
738 24 View Code Duplication
    public static function minLength($value, $min, $message = '')
739
    {
740 24
        if (static::strlen($value) < $min) {
741 8
            static::reportInvalidArgument(sprintf(
742 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
743 8
                static::valueToString($value),
744 8
                $min
745
            ));
746
        }
747 16
    }
748
749 24 View Code Duplication
    public static function maxLength($value, $max, $message = '')
750
    {
751 24
        if (static::strlen($value) > $max) {
752 8
            static::reportInvalidArgument(sprintf(
753 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
754 8
                static::valueToString($value),
755 8
                $max
756
            ));
757
        }
758 16
    }
759
760 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
761
    {
762 40
        $length = static::strlen($value);
763
764 40
        if ($length < $min || $length > $max) {
765 16
            static::reportInvalidArgument(sprintf(
766 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
767 16
                static::valueToString($value),
768 16
                $min,
769 16
                $max
770
            ));
771
        }
772 24
    }
773
774 36
    public static function fileExists($value, $message = '')
775
    {
776 36
        static::string($value);
777
778 36
        if (!file_exists($value)) {
779 12
            static::reportInvalidArgument(sprintf(
780 12
                $message ?: 'The file %s does not exist.',
781 12
                static::valueToString($value)
782
            ));
783
        }
784 24
    }
785
786 12 View Code Duplication
    public static function file($value, $message = '')
787
    {
788 12
        static::fileExists($value, $message);
789
790 8
        if (!is_file($value)) {
791 4
            static::reportInvalidArgument(sprintf(
792 4
                $message ?: 'The path %s is not a file.',
793 4
                static::valueToString($value)
794
            ));
795
        }
796 4
    }
797
798 12 View Code Duplication
    public static function directory($value, $message = '')
799
    {
800 12
        static::fileExists($value, $message);
801
802 8
        if (!is_dir($value)) {
803 4
            static::reportInvalidArgument(sprintf(
804 4
                $message ?: 'The path %s is no directory.',
805 4
                static::valueToString($value)
806
            ));
807
        }
808 4
    }
809
810 View Code Duplication
    public static function readable($value, $message = '')
811
    {
812
        if (!is_readable($value)) {
813
            static::reportInvalidArgument(sprintf(
814
                $message ?: 'The path %s is not readable.',
815
                static::valueToString($value)
816
            ));
817
        }
818
    }
819
820 View Code Duplication
    public static function writable($value, $message = '')
821
    {
822
        if (!is_writable($value)) {
823
            static::reportInvalidArgument(sprintf(
824
                $message ?: 'The path %s is not writable.',
825
                static::valueToString($value)
826
            ));
827
        }
828
    }
829
830 8 View Code Duplication
    public static function classExists($value, $message = '')
831
    {
832 8
        if (!class_exists($value)) {
833 4
            static::reportInvalidArgument(sprintf(
834 4
                $message ?: 'Expected an existing class name. Got: %s',
835 4
                static::valueToString($value)
836
            ));
837
        }
838 4
    }
839
840 8
    public static function subclassOf($value, $class, $message = '')
841
    {
842 8
        if (!is_subclass_of($value, $class)) {
843 4
            static::reportInvalidArgument(sprintf(
844 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
845 4
                static::valueToString($value),
846 4
                static::valueToString($class)
847
            ));
848
        }
849 4
    }
850
851 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
852
    {
853 8
        if (!in_array($interface, class_implements($value))) {
854 4
            static::reportInvalidArgument(sprintf(
855 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
856 4
                static::valueToString($value),
857 4
                static::valueToString($interface)
858
            ));
859
        }
860 4
    }
861
862 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
863
    {
864 12
        if (!property_exists($classOrObject, $property)) {
865 4
            static::reportInvalidArgument(sprintf(
866 4
                $message ?: 'Expected the property %s to exist.',
867 4
                static::valueToString($property)
868
            ));
869
        }
870 8
    }
871
872 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
873
    {
874 12
        if (property_exists($classOrObject, $property)) {
875 8
            static::reportInvalidArgument(sprintf(
876 8
                $message ?: 'Expected the property %s to not exist.',
877 8
                static::valueToString($property)
878
            ));
879
        }
880 4
    }
881
882 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
883
    {
884 27
        if (!method_exists($classOrObject, $method)) {
885 19
            static::reportInvalidArgument(sprintf(
886 19
                $message ?: 'Expected the method %s to exist.',
887 19
                static::valueToString($method)
888
            ));
889
        }
890 8
    }
891
892 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
893
    {
894 27
        if (method_exists($classOrObject, $method)) {
895 8
            static::reportInvalidArgument(sprintf(
896 8
                $message ?: 'Expected the method %s to not exist.',
897 8
                static::valueToString($method)
898
            ));
899
        }
900 19
    }
901
902 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
903
    {
904 12
        if (!array_key_exists($key, $array)) {
905 4
            static::reportInvalidArgument(sprintf(
906 4
                $message ?: 'Expected the key %s to exist.',
907 4
                static::valueToString($key)
908
            ));
909
        }
910 8
    }
911
912 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
913
    {
914 12
        if (array_key_exists($key, $array)) {
915 8
            static::reportInvalidArgument(sprintf(
916 8
                $message ?: 'Expected the key %s to not exist.',
917 8
                static::valueToString($key)
918
            ));
919
        }
920 4
    }
921
922 8
    public static function count($array, $number, $message = '')
923
    {
924 8
        static::eq(
925 8
            count($array),
926 8
            $number,
927 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
928
        );
929 4
    }
930
931 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
932
    {
933 12
        if (count($array) < $min) {
934 4
            static::reportInvalidArgument(sprintf(
935 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
936 4
                count($array),
937 4
                $min
938
            ));
939
        }
940 8
    }
941
942 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
943
    {
944 12
        if (count($array) > $max) {
945 4
            static::reportInvalidArgument(sprintf(
946 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
947 4
                count($array),
948 4
                $max
949
            ));
950
        }
951 8
    }
952
953 12
    public static function countBetween($array, $min, $max, $message = '')
954
    {
955 12
        $count = count($array);
956
957 12
        if ($count < $min || $count > $max) {
958 8
            static::reportInvalidArgument(sprintf(
959 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
960 8
                $count,
961 8
                $min,
962 8
                $max
963
            ));
964
        }
965 4
    }
966
967 48
    public static function uuid($value, $message = '')
968
    {
969 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
970
971
        // The nil UUID is special form of UUID that is specified to have all
972
        // 128 bits set to zero.
973 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
974 4
            return;
975
        }
976
977 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)) {
978 20
            static::reportInvalidArgument(sprintf(
979 20
                $message ?: 'Value %s is not a valid UUID.',
980 20
                static::valueToString($value)
981
            ));
982
        }
983 24
    }
984
985 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
986
    {
987 24
        static::string($class);
988
989 24
        $actual = 'none';
990
991
        try {
992 24
            $expression();
993 24
        } catch (Exception $e) {
994 20
            $actual = get_class($e);
995 20
            if ($e instanceof $class) {
996 20
                return;
997
            }
998 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...
999 4
            $actual = get_class($e);
1000 4
            if ($e instanceof $class) {
1001 4
                return;
1002
            }
1003
        }
1004
1005 8
        static::reportInvalidArgument($message ?: sprintf(
1006 8
            'Expected to throw "%s", got "%s"',
1007 8
            $class,
1008 8
            $actual
1009
        ));
1010
    }
1011
1012 980
    public static function __callStatic($name, $arguments)
1013
    {
1014 980
        if ('nullOr' === substr($name, 0, 6)) {
1015 374
            if (null !== $arguments[0]) {
1016 293
                $method = lcfirst(substr($name, 6));
1017 293
                call_user_func_array(array('static', $method), $arguments);
1018
            }
1019
1020 227
            return;
1021
        }
1022
1023 606
        if ('all' === substr($name, 0, 3)) {
1024 606
            static::isIterable($arguments[0]);
1025
1026 606
            $method = lcfirst(substr($name, 3));
1027 606
            $args = $arguments;
1028
1029 606
            foreach ($arguments[0] as $entry) {
1030 606
                $args[0] = $entry;
1031
1032 606
                call_user_func_array(array('static', $method), $args);
1033
            }
1034
1035 298
            return;
1036
        }
1037
1038
        throw new BadMethodCallException('No such method: '.$name);
1039
    }
1040
1041 440
    protected static function valueToString($value)
1042
    {
1043 440
        if (null === $value) {
1044 11
            return 'null';
1045
        }
1046
1047 431
        if (true === $value) {
1048 15
            return 'true';
1049
        }
1050
1051 421
        if (false === $value) {
1052 13
            return 'false';
1053
        }
1054
1055 408
        if (is_array($value)) {
1056 1
            return 'array';
1057
        }
1058
1059 407
        if (is_object($value)) {
1060 1
            return get_class($value);
1061
        }
1062
1063 406
        if (is_resource($value)) {
1064 1
            return 'resource';
1065
        }
1066
1067 406
        if (is_string($value)) {
1068 320
            return '"'.$value.'"';
1069
        }
1070
1071 94
        return (string) $value;
1072
    }
1073
1074 169
    protected static function typeToString($value)
1075
    {
1076 169
        return is_object($value) ? get_class($value) : gettype($value);
1077
    }
1078
1079 124
    protected static function strlen($value)
1080
    {
1081 124
        if (!function_exists('mb_detect_encoding')) {
1082
            return strlen($value);
1083
        }
1084
1085 124
        if (false === $encoding = mb_detect_encoding($value)) {
1086
            return strlen($value);
1087
        }
1088
1089 124
        return mb_strwidth($value, $encoding);
1090
    }
1091
1092 624
    protected static function reportInvalidArgument($message)
1093
    {
1094 624
        throw new InvalidArgumentException($message);
1095
    }
1096
1097
    private function __construct()
1098
    {
1099
    }
1100
}
1101