Failed Conditions
Pull Request — master (#84)
by Tobias
35:30 queued 34:28
created

Assert::valueToString()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

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