Failed Conditions
Pull Request — master (#34)
by Tobias
03:32 queued 01:09
created

Assert::notWhitespaceOnly()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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