Failed Conditions
Pull Request — master (#34)
by Dmitriy
03:37
created

Assert::notInstanceOf()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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