Failed Conditions
Pull Request — master (#72)
by
unknown
04:08
created

Assert::startsNotWith()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the webmozart/assert package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Assert;
13
14
use ArrayAccess;
15
use BadMethodCallException;
16
use Closure;
17
use Countable;
18
use Exception;
19
use InvalidArgumentException;
20
use Throwable;
21
use Traversable;
22
23
/**
24
 * Efficient assertions to validate the input/output of your methods.
25
 *
26
 * @method static void nullOrString($value, $message = '')
27
 * @method static void nullOrStringNotEmpty($value, $message = '')
28
 * @method static void nullOrInteger($value, $message = '')
29
 * @method static void nullOrIntegerish($value, $message = '')
30
 * @method static void nullOrFloat($value, $message = '')
31
 * @method static void nullOrNumeric($value, $message = '')
32
 * @method static void nullOrBoolean($value, $message = '')
33
 * @method static void nullOrScalar($value, $message = '')
34
 * @method static void nullOrObject($value, $message = '')
35
 * @method static void nullOrResource($value, $type = null, $message = '')
36
 * @method static void nullOrIsCallable($value, $message = '')
37
 * @method static void nullOrIsArray($value, $message = '')
38
 * @method static void nullOrIsTraversable($value, $message = '')
39
 * @method static void nullOrIsArrayAccessible($value, $message = '')
40
 * @method static void nullOrIsCountable($value, $message = '')
41
 * @method static void nullOrIsInstanceOf($value, $class, $message = '')
42
 * @method static void nullOrNotInstanceOf($value, $class, $message = '')
43
 * @method static void nullOrIsInstanceOfAny($value, $classes, $message = '')
44
 * @method static void nullOrIsEmpty($value, $message = '')
45
 * @method static void nullOrNotEmpty($value, $message = '')
46
 * @method static void nullOrTrue($value, $message = '')
47
 * @method static void nullOrFalse($value, $message = '')
48
 * @method static void nullOrEq($value, $value2, $message = '')
49
 * @method static void nullOrNotEq($value,$value2,  $message = '')
50
 * @method static void nullOrSame($value, $value2, $message = '')
51
 * @method static void nullOrNotSame($value, $value2, $message = '')
52
 * @method static void nullOrGreaterThan($value, $value2, $message = '')
53
 * @method static void nullOrGreaterThanEq($value, $value2, $message = '')
54
 * @method static void nullOrLessThan($value, $value2, $message = '')
55
 * @method static void nullOrLessThanEq($value, $value2, $message = '')
56
 * @method static void nullOrRange($value, $min, $max, $message = '')
57
 * @method static void nullOrOneOf($value, $values, $message = '')
58
 * @method static void nullOrContains($value, $subString, $message = '')
59
 * @method static void nullOrNotContains($value, $subString, $message = '')
60
 * @method static void nullOrNotWhitespaceOnly($value, $message = '')
61
 * @method static void nullOrStartsWith($value, $prefix, $message = '')
62
 * @method static void nullOrStartsWithLetter($value, $message = '')
63
 * @method static void nullOrEndsWith($value, $suffix, $message = '')
64
 * @method static void nullOrRegex($value, $pattern, $message = '')
65
 * @method static void nullOrNotRegex($value, $pattern, $message = '')
66
 * @method static void nullOrAlpha($value, $message = '')
67
 * @method static void nullOrDigits($value, $message = '')
68
 * @method static void nullOrAlnum($value, $message = '')
69
 * @method static void nullOrLower($value, $message = '')
70
 * @method static void nullOrUpper($value, $message = '')
71
 * @method static void nullOrLength($value, $length, $message = '')
72
 * @method static void nullOrMinLength($value, $min, $message = '')
73
 * @method static void nullOrMaxLength($value, $max, $message = '')
74
 * @method static void nullOrLengthBetween($value, $min, $max, $message = '')
75
 * @method static void nullOrFileExists($value, $message = '')
76
 * @method static void nullOrFile($value, $message = '')
77
 * @method static void nullOrDirectory($value, $message = '')
78
 * @method static void nullOrReadable($value, $message = '')
79
 * @method static void nullOrWritable($value, $message = '')
80
 * @method static void nullOrClassExists($value, $message = '')
81
 * @method static void nullOrSubclassOf($value, $class, $message = '')
82
 * @method static void nullOrImplementsInterface($value, $interface, $message = '')
83
 * @method static void nullOrPropertyExists($value, $property, $message = '')
84
 * @method static void nullOrPropertyNotExists($value, $property, $message = '')
85
 * @method static void nullOrMethodExists($value, $method, $message = '')
86
 * @method static void nullOrMethodNotExists($value, $method, $message = '')
87
 * @method static void nullOrKeyExists($value, $key, $message = '')
88
 * @method static void nullOrKeyNotExists($value, $key, $message = '')
89
 * @method static void nullOrCount($value, $key, $message = '')
90
 * @method static void nullOrMinCount($value, $min, $message = '')
91
 * @method static void nullOrMaxCount($value, $max, $message = '')
92
 * @method static void nullCountBetween($value, $min, $max, $message = '')
93
 * @method static void nullOrUuid($values, $message = '')
94
 * @method static void allString($values, $message = '')
95
 * @method static void allStringNotEmpty($values, $message = '')
96
 * @method static void allInteger($values, $message = '')
97
 * @method static void allIntegerish($values, $message = '')
98
 * @method static void allFloat($values, $message = '')
99
 * @method static void allNumeric($values, $message = '')
100
 * @method static void allBoolean($values, $message = '')
101
 * @method static void allScalar($values, $message = '')
102
 * @method static void allObject($values, $message = '')
103
 * @method static void allResource($values, $type = null, $message = '')
104
 * @method static void allIsCallable($values, $message = '')
105
 * @method static void allIsArray($values, $message = '')
106
 * @method static void allIsTraversable($values, $message = '')
107
 * @method static void allIsArrayAccessible($values, $message = '')
108
 * @method static void allIsCountable($values, $message = '')
109
 * @method static void allIsInstanceOf($values, $class, $message = '')
110
 * @method static void allNotInstanceOf($values, $class, $message = '')
111
 * @method static void allIsInstanceOfAny($values, $classes, $message = '')
112
 * @method static void allNull($values, $message = '')
113
 * @method static void allNotNull($values, $message = '')
114
 * @method static void allIsEmpty($values, $message = '')
115
 * @method static void allNotEmpty($values, $message = '')
116
 * @method static void allTrue($values, $message = '')
117
 * @method static void allFalse($values, $message = '')
118
 * @method static void allEq($values, $value2, $message = '')
119
 * @method static void allNotEq($values,$value2,  $message = '')
120
 * @method static void allSame($values, $value2, $message = '')
121
 * @method static void allNotSame($values, $value2, $message = '')
122
 * @method static void allGreaterThan($values, $value2, $message = '')
123
 * @method static void allGreaterThanEq($values, $value2, $message = '')
124
 * @method static void allLessThan($values, $value2, $message = '')
125
 * @method static void allLessThanEq($values, $value2, $message = '')
126
 * @method static void allRange($values, $min, $max, $message = '')
127
 * @method static void allOneOf($values, $values, $message = '')
128
 * @method static void allContains($values, $subString, $message = '')
129
 * @method static void allNotContains($values, $subString, $message = '')
130
 * @method static void allNotWhitespaceOnly($values, $message = '')
131
 * @method static void allStartsWith($values, $prefix, $message = '')
132
 * @method static void allStartsWithLetter($values, $message = '')
133
 * @method static void allEndsWith($values, $suffix, $message = '')
134
 * @method static void allRegex($values, $pattern, $message = '')
135
 * @method static void allNotRegex($values, $pattern, $message = '')
136
 * @method static void allAlpha($values, $message = '')
137
 * @method static void allDigits($values, $message = '')
138
 * @method static void allAlnum($values, $message = '')
139
 * @method static void allLower($values, $message = '')
140
 * @method static void allUpper($values, $message = '')
141
 * @method static void allLength($values, $length, $message = '')
142
 * @method static void allMinLength($values, $min, $message = '')
143
 * @method static void allMaxLength($values, $max, $message = '')
144
 * @method static void allLengthBetween($values, $min, $max, $message = '')
145
 * @method static void allFileExists($values, $message = '')
146
 * @method static void allFile($values, $message = '')
147
 * @method static void allDirectory($values, $message = '')
148
 * @method static void allReadable($values, $message = '')
149
 * @method static void allWritable($values, $message = '')
150
 * @method static void allClassExists($values, $message = '')
151
 * @method static void allSubclassOf($values, $class, $message = '')
152
 * @method static void allImplementsInterface($values, $interface, $message = '')
153
 * @method static void allPropertyExists($values, $property, $message = '')
154
 * @method static void allPropertyNotExists($values, $property, $message = '')
155
 * @method static void allMethodExists($values, $method, $message = '')
156
 * @method static void allMethodNotExists($values, $method, $message = '')
157
 * @method static void allKeyExists($values, $key, $message = '')
158
 * @method static void allKeyNotExists($values, $key, $message = '')
159
 * @method static void allCount($values, $key, $message = '')
160
 * @method static void allMinCount($values, $min, $message = '')
161
 * @method static void allMaxCount($values, $max, $message = '')
162
 * @method static void allCountBetween($values, $min, $max, $message = '')
163
 * @method static void allUuid($values, $message = '')
164
 *
165
 * @since  1.0
166
 *
167
 * @author Bernhard Schussek <[email protected]>
168
 */
169
class Assert
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
170
{
171 94
    public static function string($value, $message = '')
172
    {
173 94
        if (!is_string($value)) {
174 14
            static::reportInvalidArgument(sprintf(
175 14
                $message ?: 'Expected a string. Got: %s',
176 14
                static::typeToString($value)
177
            ));
178
        }
179 80
    }
180
181 16
    public static function stringNotEmpty($value, $message = '')
182
    {
183 16
        static::string($value, $message);
184 12
        static::notEq($value, '', $message);
185 8
    }
186
187 17
    public static function integer($value, $message = '')
188
    {
189 17
        if (!is_int($value)) {
190 13
            static::reportInvalidArgument(sprintf(
191 13
                $message ?: 'Expected an integer. Got: %s',
192 13
                static::typeToString($value)
193
            ));
194
        }
195 4
    }
196
197 16
    public static function integerish($value, $message = '')
198
    {
199 16
        if (!is_numeric($value) || $value != (int) $value) {
200 4
            static::reportInvalidArgument(sprintf(
201 4
                $message ?: 'Expected an integerish value. Got: %s',
202 4
                static::typeToString($value)
203
            ));
204
        }
205 12
    }
206
207 16
    public static function float($value, $message = '')
208
    {
209 16
        if (!is_float($value)) {
210 8
            static::reportInvalidArgument(sprintf(
211 8
                $message ?: 'Expected a float. Got: %s',
212 8
                static::typeToString($value)
213
            ));
214
        }
215 8
    }
216
217 20
    public static function numeric($value, $message = '')
218
    {
219 20
        if (!is_numeric($value)) {
220 4
            static::reportInvalidArgument(sprintf(
221 4
                $message ?: 'Expected a numeric. Got: %s',
222 4
                static::typeToString($value)
223
            ));
224
        }
225 16
    }
226
227 24
    public static function natural($value, $message = '')
228
    {
229 24
        if (!is_int($value) || $value < 0) {
230 16
            static::reportInvalidArgument(sprintf(
231 16
                $message ?: 'Expected a non-negative integer. Got %s',
232 16
                static::valueToString($value)
233
            ));
234
        }
235 8
    }
236
237 16
    public static function boolean($value, $message = '')
238
    {
239 16
        if (!is_bool($value)) {
240 8
            static::reportInvalidArgument(sprintf(
241 8
                $message ?: 'Expected a boolean. Got: %s',
242 8
                static::typeToString($value)
243
            ));
244
        }
245 8
    }
246
247 23
    public static function scalar($value, $message = '')
248
    {
249 23
        if (!is_scalar($value)) {
250 11
            static::reportInvalidArgument(sprintf(
251 11
                $message ?: 'Expected a scalar. Got: %s',
252 11
                static::typeToString($value)
253
            ));
254
        }
255 12
    }
256
257 23
    public static function object($value, $message = '')
258
    {
259 23
        if (!is_object($value)) {
260 15
            static::reportInvalidArgument(sprintf(
261 15
                $message ?: 'Expected an object. Got: %s',
262 15
                static::typeToString($value)
263
            ));
264
        }
265 8
    }
266
267 16
    public static function resource($value, $type = null, $message = '')
268
    {
269 16
        if (!is_resource($value)) {
270 4
            static::reportInvalidArgument(sprintf(
271 4
                $message ?: 'Expected a resource. Got: %s',
272 4
                static::typeToString($value)
273
            ));
274
        }
275
276 12
        if ($type && $type !== get_resource_type($value)) {
277 4
            static::reportInvalidArgument(sprintf(
278 4
                $message ?: 'Expected a resource of type %2$s. Got: %s',
279 4
                static::typeToString($value),
280 4
                $type
281
            ));
282
        }
283 8
    }
284
285 20
    public static function isCallable($value, $message = '')
286
    {
287 20
        if (!is_callable($value)) {
288 8
            static::reportInvalidArgument(sprintf(
289 8
                $message ?: 'Expected a callable. Got: %s',
290 8
                static::typeToString($value)
291
            ));
292
        }
293 12
    }
294
295 20
    public static function isArray($value, $message = '')
296
    {
297 20
        if (!is_array($value)) {
298 12
            static::reportInvalidArgument(sprintf(
299 12
                $message ?: 'Expected an array. Got: %s',
300 12
                static::typeToString($value)
301
            ));
302
        }
303 8
    }
304
305 20
    public static function isTraversable($value, $message = '')
306
    {
307 20
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
308 20
            sprintf(
309 20
                'The "%s" assertion is deprecated. You should stop using it, as it will soon be removed in 2.0 version. Use "isIterable" or "isInstanceOf" instead.',
310 20
                __METHOD__
311
            ),
312 20
            E_USER_DEPRECATED
313
        );
314
315 20
        if (!is_array($value) && !($value instanceof Traversable)) {
316 8
            static::reportInvalidArgument(sprintf(
317 8
                $message ?: 'Expected a traversable. Got: %s',
318 8
                static::typeToString($value)
319
            ));
320
        }
321 12
    }
322
323 20 View Code Duplication
    public static function isArrayAccessible($value, $message = '')
324
    {
325 20
        if (!is_array($value) && !($value instanceof ArrayAccess)) {
326 8
            static::reportInvalidArgument(sprintf(
327 8
                $message ?: 'Expected an array accessible. Got: %s',
328 8
                static::typeToString($value)
329
            ));
330
        }
331 12
    }
332
333 24 View Code Duplication
    public static function isCountable($value, $message = '')
334
    {
335 24
        if (!is_array($value) && !($value instanceof Countable)) {
336 12
            static::reportInvalidArgument(sprintf(
337 12
                $message ?: 'Expected a countable. Got: %s',
338 12
                static::typeToString($value)
339
            ));
340
        }
341 12
    }
342
343 628 View Code Duplication
    public static function isIterable($value, $message = '')
344
    {
345 628
        if (!is_array($value) && !($value instanceof Traversable)) {
346 8
            static::reportInvalidArgument(sprintf(
347 8
                $message ?: 'Expected an iterable. Got: %s',
348 8
                static::typeToString($value)
349
            ));
350
        }
351 624
    }
352
353 16
    public static function isInstanceOf($value, $class, $message = '')
354
    {
355 16
        if (!($value instanceof $class)) {
356 12
            static::reportInvalidArgument(sprintf(
357 12
                $message ?: 'Expected an instance of %2$s. Got: %s',
358 12
                static::typeToString($value),
359 12
                $class
360
            ));
361
        }
362 4
    }
363
364 16
    public static function notInstanceOf($value, $class, $message = '')
365
    {
366 16
        if ($value instanceof $class) {
367 4
            static::reportInvalidArgument(sprintf(
368 4
                $message ?: 'Expected an instance other than %2$s. Got: %s',
369 4
                static::typeToString($value),
370 4
                $class
371
            ));
372
        }
373 12
    }
374
375 20
    public static function isInstanceOfAny($value, array $classes, $message = '')
376
    {
377 20
        foreach ($classes as $class) {
378 20
            if ($value instanceof $class) {
379 20
                return;
380
            }
381
        }
382
383 12
        static::reportInvalidArgument(sprintf(
384 12
            $message ?: 'Expected an instance of any of %2$s. Got: %s',
385 12
            static::typeToString($value),
386 12
            implode(', ', array_map(array('static', 'valueToString'), $classes))
387
        ));
388
    }
389
390 23
    public static function isEmpty($value, $message = '')
391
    {
392 23
        if (!empty($value)) {
393 8
            static::reportInvalidArgument(sprintf(
394 8
                $message ?: 'Expected an empty value. Got: %s',
395 8
                static::valueToString($value)
396
            ));
397
        }
398 15
    }
399
400 23
    public static function notEmpty($value, $message = '')
401
    {
402 23
        if (empty($value)) {
403 15
            static::reportInvalidArgument(sprintf(
404 15
                $message ?: 'Expected a non-empty value. Got: %s',
405 15
                static::valueToString($value)
406
            ));
407
        }
408 8
    }
409
410 11
    public static function null($value, $message = '')
411
    {
412 11
        if (null !== $value) {
413 8
            static::reportInvalidArgument(sprintf(
414 8
                $message ?: 'Expected null. Got: %s',
415 8
                static::valueToString($value)
416
            ));
417
        }
418 3
    }
419
420 11
    public static function notNull($value, $message = '')
421
    {
422 11
        if (null === $value) {
423 3
            static::reportInvalidArgument(
424 3
                $message ?: 'Expected a value other than null.'
425
            );
426
        }
427 8
    }
428
429 15
    public static function true($value, $message = '')
430
    {
431 15
        if (true !== $value) {
432 11
            static::reportInvalidArgument(sprintf(
433 11
                $message ?: 'Expected a value to be true. Got: %s',
434 11
                static::valueToString($value)
435
            ));
436
        }
437 4
    }
438
439 19
    public static function false($value, $message = '')
440
    {
441 19
        if (false !== $value) {
442 15
            static::reportInvalidArgument(sprintf(
443 15
                $message ?: 'Expected a value to be false. Got: %s',
444 15
                static::valueToString($value)
445
            ));
446
        }
447 4
    }
448
449 33
    public static function eq($value, $value2, $message = '')
450
    {
451 33
        if ($value2 != $value) {
452 17
            static::reportInvalidArgument(sprintf(
453 17
                $message ?: 'Expected a value equal to %2$s. Got: %s',
454 17
                static::valueToString($value),
455 17
                static::valueToString($value2)
456
            ));
457
        }
458 16
    }
459
460 28
    public static function notEq($value, $value2, $message = '')
461
    {
462 28
        if ($value2 == $value) {
463 16
            static::reportInvalidArgument(sprintf(
464 16
                $message ?: 'Expected a different value than %s.',
465 16
                static::valueToString($value2)
466
            ));
467
        }
468 12
    }
469
470 16
    public static function same($value, $value2, $message = '')
471
    {
472 16
        if ($value2 !== $value) {
473 12
            static::reportInvalidArgument(sprintf(
474 12
                $message ?: 'Expected a value identical to %2$s. Got: %s',
475 12
                static::valueToString($value),
476 12
                static::valueToString($value2)
477
            ));
478
        }
479 4
    }
480
481 16
    public static function notSame($value, $value2, $message = '')
482
    {
483 16
        if ($value2 === $value) {
484 4
            static::reportInvalidArgument(sprintf(
485 4
                $message ?: 'Expected a value not identical to %s.',
486 4
                static::valueToString($value2)
487
            ));
488
        }
489 12
    }
490
491 8
    public static function greaterThan($value, $limit, $message = '')
492
    {
493 8
        if ($value <= $limit) {
494 4
            static::reportInvalidArgument(sprintf(
495 4
                $message ?: 'Expected a value greater than %2$s. Got: %s',
496 4
                static::valueToString($value),
497 4
                static::valueToString($limit)
498
            ));
499
        }
500 4
    }
501
502 12
    public static function greaterThanEq($value, $limit, $message = '')
503
    {
504 12
        if ($value < $limit) {
505 4
            static::reportInvalidArgument(sprintf(
506 4
                $message ?: 'Expected a value greater than or equal to %2$s. Got: %s',
507 4
                static::valueToString($value),
508 4
                static::valueToString($limit)
509
            ));
510
        }
511 8
    }
512
513 8
    public static function lessThan($value, $limit, $message = '')
514
    {
515 8
        if ($value >= $limit) {
516 4
            static::reportInvalidArgument(sprintf(
517 4
                $message ?: 'Expected a value less than %2$s. Got: %s',
518 4
                static::valueToString($value),
519 4
                static::valueToString($limit)
520
            ));
521
        }
522 4
    }
523
524 12
    public static function lessThanEq($value, $limit, $message = '')
525
    {
526 12
        if ($value > $limit) {
527 4
            static::reportInvalidArgument(sprintf(
528 4
                $message ?: 'Expected a value less than or equal to %2$s. Got: %s',
529 4
                static::valueToString($value),
530 4
                static::valueToString($limit)
531
            ));
532
        }
533 8
    }
534
535 16 View Code Duplication
    public static function range($value, $min, $max, $message = '')
536
    {
537 16
        if ($value < $min || $value > $max) {
538 8
            static::reportInvalidArgument(sprintf(
539 8
                $message ?: 'Expected a value between %2$s and %3$s. Got: %s',
540 8
                static::valueToString($value),
541 8
                static::valueToString($min),
542 8
                static::valueToString($max)
543
            ));
544
        }
545 8
    }
546
547 8
    public static function oneOf($value, array $values, $message = '')
548
    {
549 8
        if (!in_array($value, $values, true)) {
550 4
            static::reportInvalidArgument(sprintf(
551 4
                $message ?: 'Expected one of: %2$s. Got: %s',
552 4
                static::valueToString($value),
553 4
                implode(', ', array_map(array('static', 'valueToString'), $values))
554
            ));
555
        }
556 4
    }
557
558 20 View Code Duplication
    public static function contains($value, $subString, $message = '')
559
    {
560 20
        if (false === strpos($value, $subString)) {
561 8
            static::reportInvalidArgument(sprintf(
562 8
                $message ?: 'Expected a value to contain %2$s. Got: %s',
563 8
                static::valueToString($value),
564 8
                static::valueToString($subString)
565
            ));
566
        }
567 12
    }
568
569 20 View Code Duplication
    public static function notContains($value, $subString, $message = '')
570
    {
571 20
        if (false !== strpos($value, $subString)) {
572 12
            static::reportInvalidArgument(sprintf(
573 12
                $message ?: '%2$s was not expected to be contained in a value. Got: %s',
574 12
                static::valueToString($value),
575 12
                static::valueToString($subString)
576
            ));
577
        }
578 8
    }
579
580 40
    public static function notWhitespaceOnly($value, $message = '')
581
    {
582 40
        if (preg_match('/^\s*$/', $value)) {
583 24
            static::reportInvalidArgument(sprintf(
584 24
                $message ?: 'Expected a non-whitespace string. Got: %s',
585 24
                static::valueToString($value)
586
            ));
587
        }
588 16
    }
589
590 12 View Code Duplication
    public static function startsWith($value, $prefix, $message = '')
591
    {
592 12
        if (0 !== strpos($value, $prefix)) {
593 8
            static::reportInvalidArgument(sprintf(
594 8
                $message ?: 'Expected a value to start with %2$s. Got: %s',
595 8
                static::valueToString($value),
596 8
                static::valueToString($prefix)
597
            ));
598
        }
599 4
    }
600
601 12 View Code Duplication
    public static function startsNotWith($value, $prefix, $message = '')
602
    {
603 12
        if (0 === strpos($value, $prefix)) {
604 4
            static::reportInvalidArgument(sprintf(
605 4
                $message ?: 'Expected a value to not start with %2$s.',
606 4
                static::valueToString($value),
607 4
                static::valueToString($prefix)
608
            ));
609
        }
610 8
    }
611
612 12
    public static function startsWithLetter($value, $message = '')
613
    {
614 12
        $valid = isset($value[0]);
615
616 12
        if ($valid) {
617 8
            $locale = setlocale(LC_CTYPE, 0);
618 8
            setlocale(LC_CTYPE, 'C');
619 8
            $valid = ctype_alpha($value[0]);
620 8
            setlocale(LC_CTYPE, $locale);
621
        }
622
623 12
        if (!$valid) {
624 8
            static::reportInvalidArgument(sprintf(
625 8
                $message ?: 'Expected a value to start with a letter. Got: %s',
626 8
                static::valueToString($value)
627
            ));
628
        }
629 4
    }
630
631 12 View Code Duplication
    public static function endsWith($value, $suffix, $message = '')
632
    {
633 12
        if ($suffix !== substr($value, -static::strlen($suffix))) {
634 8
            static::reportInvalidArgument(sprintf(
635 8
                $message ?: 'Expected a value to end with %2$s. Got: %s',
636 8
                static::valueToString($value),
637 8
                static::valueToString($suffix)
638
            ));
639
        }
640 4
    }
641
642 12 View Code Duplication
    public static function endsNotWith($value, $suffix, $message = '')
643
    {
644 12
        if ($suffix === substr($value, -static::strlen($suffix))) {
645 4
            static::reportInvalidArgument(sprintf(
646 4
                $message ?: 'Expected a value to not end with %2$s.',
647 4
                static::valueToString($value),
648 4
                static::valueToString($suffix)
649
            ));
650
        }
651 8
    }
652
653 12
    public static function regex($value, $pattern, $message = '')
654
    {
655 12
        if (!preg_match($pattern, $value)) {
656 8
            static::reportInvalidArgument(sprintf(
657 8
                $message ?: 'The value %s does not match the expected pattern.',
658 8
                static::valueToString($value)
659
            ));
660
        }
661 4
    }
662
663 12
    public static function notRegex($value, $pattern, $message = '')
664
    {
665 12
        if (preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE)) {
666 4
            static::reportInvalidArgument(sprintf(
667 4
                $message ?: 'The value %s matches the pattern %s (at offset %d).',
668 4
                static::valueToString($value),
669 4
                static::valueToString($pattern),
670 4
                $matches[0][1]
671
            ));
672
        }
673 8
    }
674
675 12 View Code Duplication
    public static function alpha($value, $message = '')
676
    {
677 12
        $locale = setlocale(LC_CTYPE, 0);
678 12
        setlocale(LC_CTYPE, 'C');
679 12
        $valid = !ctype_alpha($value);
680 12
        setlocale(LC_CTYPE, $locale);
681
682 12
        if ($valid) {
683 8
            static::reportInvalidArgument(sprintf(
684 8
                $message ?: 'Expected a value to contain only letters. Got: %s',
685 8
                static::valueToString($value)
686
            ));
687
        }
688 4
    }
689
690 12 View Code Duplication
    public static function digits($value, $message = '')
691
    {
692 12
        $locale = setlocale(LC_CTYPE, 0);
693 12
        setlocale(LC_CTYPE, 'C');
694 12
        $valid = !ctype_digit($value);
695 12
        setlocale(LC_CTYPE, $locale);
696
697 12
        if ($valid) {
698 8
            static::reportInvalidArgument(sprintf(
699 8
                $message ?: 'Expected a value to contain digits only. Got: %s',
700 8
                static::valueToString($value)
701
            ));
702
        }
703 4
    }
704
705 12 View Code Duplication
    public static function alnum($value, $message = '')
706
    {
707 12
        $locale = setlocale(LC_CTYPE, 0);
708 12
        setlocale(LC_CTYPE, 'C');
709 12
        $valid = !ctype_alnum($value);
710 12
        setlocale(LC_CTYPE, $locale);
711
712 12
        if ($valid) {
713 8
            static::reportInvalidArgument(sprintf(
714 8
                $message ?: 'Expected a value to contain letters and digits only. Got: %s',
715 8
                static::valueToString($value)
716
            ));
717
        }
718 4
    }
719
720 16 View Code Duplication
    public static function lower($value, $message = '')
721
    {
722 16
        $locale = setlocale(LC_CTYPE, 0);
723 16
        setlocale(LC_CTYPE, 'C');
724 16
        $valid = !ctype_lower($value);
725 16
        setlocale(LC_CTYPE, $locale);
726
727 16
        if ($valid) {
728 12
            static::reportInvalidArgument(sprintf(
729 12
                $message ?: 'Expected a value to contain lowercase characters only. Got: %s',
730 12
                static::valueToString($value)
731
            ));
732
        }
733 4
    }
734
735 16 View Code Duplication
    public static function upper($value, $message = '')
736
    {
737 16
        $locale = setlocale(LC_CTYPE, 0);
738 16
        setlocale(LC_CTYPE, 'C');
739 16
        $valid = !ctype_upper($value);
740 16
        setlocale(LC_CTYPE, $locale);
741
742 16
        if ($valid) {
743 12
            static::reportInvalidArgument(sprintf(
744 12
                $message ?: 'Expected a value to contain uppercase characters only. Got: %s',
745 12
                static::valueToString($value)
746
            ));
747
        }
748 4
    }
749
750 24
    public static function length($value, $length, $message = '')
751
    {
752 24
        if ($length !== static::strlen($value)) {
753 16
            static::reportInvalidArgument(sprintf(
754 16
                $message ?: 'Expected a value to contain %2$s characters. Got: %s',
755 16
                static::valueToString($value),
756 16
                $length
757
            ));
758
        }
759 8
    }
760
761 24
    public static function minLength($value, $min, $message = '')
762
    {
763 24
        if (static::strlen($value) < $min) {
764 8
            static::reportInvalidArgument(sprintf(
765 8
                $message ?: 'Expected a value to contain at least %2$s characters. Got: %s',
766 8
                static::valueToString($value),
767 8
                $min
768
            ));
769
        }
770 16
    }
771
772 24
    public static function maxLength($value, $max, $message = '')
773
    {
774 24
        if (static::strlen($value) > $max) {
775 8
            static::reportInvalidArgument(sprintf(
776 8
                $message ?: 'Expected a value to contain at most %2$s characters. Got: %s',
777 8
                static::valueToString($value),
778 8
                $max
779
            ));
780
        }
781 16
    }
782
783 40 View Code Duplication
    public static function lengthBetween($value, $min, $max, $message = '')
784
    {
785 40
        $length = static::strlen($value);
786
787 40
        if ($length < $min || $length > $max) {
788 16
            static::reportInvalidArgument(sprintf(
789 16
                $message ?: 'Expected a value to contain between %2$s and %3$s characters. Got: %s',
790 16
                static::valueToString($value),
791 16
                $min,
792 16
                $max
793
            ));
794
        }
795 24
    }
796
797 36
    public static function fileExists($value, $message = '')
798
    {
799 36
        static::string($value);
800
801 36
        if (!file_exists($value)) {
802 12
            static::reportInvalidArgument(sprintf(
803 12
                $message ?: 'The file %s does not exist.',
804 12
                static::valueToString($value)
805
            ));
806
        }
807 24
    }
808
809 12 View Code Duplication
    public static function file($value, $message = '')
810
    {
811 12
        static::fileExists($value, $message);
812
813 8
        if (!is_file($value)) {
814 4
            static::reportInvalidArgument(sprintf(
815 4
                $message ?: 'The path %s is not a file.',
816 4
                static::valueToString($value)
817
            ));
818
        }
819 4
    }
820
821 12 View Code Duplication
    public static function directory($value, $message = '')
822
    {
823 12
        static::fileExists($value, $message);
824
825 8
        if (!is_dir($value)) {
826 4
            static::reportInvalidArgument(sprintf(
827 4
                $message ?: 'The path %s is no directory.',
828 4
                static::valueToString($value)
829
            ));
830
        }
831 4
    }
832
833
    public static function readable($value, $message = '')
834
    {
835
        if (!is_readable($value)) {
836
            static::reportInvalidArgument(sprintf(
837
                $message ?: 'The path %s is not readable.',
838
                static::valueToString($value)
839
            ));
840
        }
841
    }
842
843
    public static function writable($value, $message = '')
844
    {
845
        if (!is_writable($value)) {
846
            static::reportInvalidArgument(sprintf(
847
                $message ?: 'The path %s is not writable.',
848
                static::valueToString($value)
849
            ));
850
        }
851
    }
852
853 8
    public static function classExists($value, $message = '')
854
    {
855 8
        if (!class_exists($value)) {
856 4
            static::reportInvalidArgument(sprintf(
857 4
                $message ?: 'Expected an existing class name. Got: %s',
858 4
                static::valueToString($value)
859
            ));
860
        }
861 4
    }
862
863 8
    public static function subclassOf($value, $class, $message = '')
864
    {
865 8
        if (!is_subclass_of($value, $class)) {
866 4
            static::reportInvalidArgument(sprintf(
867 4
                $message ?: 'Expected a sub-class of %2$s. Got: %s',
868 4
                static::valueToString($value),
869 4
                static::valueToString($class)
870
            ));
871
        }
872 4
    }
873
874 8 View Code Duplication
    public static function implementsInterface($value, $interface, $message = '')
875
    {
876 8
        if (!in_array($interface, class_implements($value))) {
877 4
            static::reportInvalidArgument(sprintf(
878 4
                $message ?: 'Expected an implementation of %2$s. Got: %s',
879 4
                static::valueToString($value),
880 4
                static::valueToString($interface)
881
            ));
882
        }
883 4
    }
884
885 12 View Code Duplication
    public static function propertyExists($classOrObject, $property, $message = '')
886
    {
887 12
        if (!property_exists($classOrObject, $property)) {
888 4
            static::reportInvalidArgument(sprintf(
889 4
                $message ?: 'Expected the property %s to exist.',
890 4
                static::valueToString($property)
891
            ));
892
        }
893 8
    }
894
895 12 View Code Duplication
    public static function propertyNotExists($classOrObject, $property, $message = '')
896
    {
897 12
        if (property_exists($classOrObject, $property)) {
898 8
            static::reportInvalidArgument(sprintf(
899 8
                $message ?: 'Expected the property %s to not exist.',
900 8
                static::valueToString($property)
901
            ));
902
        }
903 4
    }
904
905 27 View Code Duplication
    public static function methodExists($classOrObject, $method, $message = '')
906
    {
907 27
        if (!method_exists($classOrObject, $method)) {
908 19
            static::reportInvalidArgument(sprintf(
909 19
                $message ?: 'Expected the method %s to exist.',
910 19
                static::valueToString($method)
911
            ));
912
        }
913 8
    }
914
915 27 View Code Duplication
    public static function methodNotExists($classOrObject, $method, $message = '')
916
    {
917 27
        if (method_exists($classOrObject, $method)) {
918 8
            static::reportInvalidArgument(sprintf(
919 8
                $message ?: 'Expected the method %s to not exist.',
920 8
                static::valueToString($method)
921
            ));
922
        }
923 19
    }
924
925 12 View Code Duplication
    public static function keyExists($array, $key, $message = '')
926
    {
927 12
        if (!(isset($array[$key]) || array_key_exists($key, $array))) {
928 4
            static::reportInvalidArgument(sprintf(
929 4
                $message ?: 'Expected the key %s to exist.',
930 4
                static::valueToString($key)
931
            ));
932
        }
933 8
    }
934
935 12 View Code Duplication
    public static function keyNotExists($array, $key, $message = '')
936
    {
937 12
        if (isset($array[$key]) || array_key_exists($key, $array)) {
938 8
            static::reportInvalidArgument(sprintf(
939 8
                $message ?: 'Expected the key %s to not exist.',
940 8
                static::valueToString($key)
941
            ));
942
        }
943 4
    }
944
945 8
    public static function count($array, $number, $message = '')
946
    {
947 8
        static::eq(
948 8
            count($array),
949 8
            $number,
950 8
            $message ?: sprintf('Expected an array to contain %d elements. Got: %d.', $number, count($array))
951
        );
952 4
    }
953
954 12 View Code Duplication
    public static function minCount($array, $min, $message = '')
955
    {
956 12
        if (count($array) < $min) {
957 4
            static::reportInvalidArgument(sprintf(
958 4
                $message ?: 'Expected an array to contain at least %2$d elements. Got: %d',
959 4
                count($array),
960 4
                $min
961
            ));
962
        }
963 8
    }
964
965 12 View Code Duplication
    public static function maxCount($array, $max, $message = '')
966
    {
967 12
        if (count($array) > $max) {
968 4
            static::reportInvalidArgument(sprintf(
969 4
                $message ?: 'Expected an array to contain at most %2$d elements. Got: %d',
970 4
                count($array),
971 4
                $max
972
            ));
973
        }
974 8
    }
975
976 12
    public static function countBetween($array, $min, $max, $message = '')
977
    {
978 12
        $count = count($array);
979
980 12
        if ($count < $min || $count > $max) {
981 8
            static::reportInvalidArgument(sprintf(
982 8
                $message ?: 'Expected an array to contain between %2$d and %3$d elements. Got: %d',
983 8
                $count,
984 8
                $min,
985 8
                $max
986
            ));
987
        }
988 4
    }
989
990 48
    public static function uuid($value, $message = '')
991
    {
992 48
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
993
994
        // The nil UUID is special form of UUID that is specified to have all
995
        // 128 bits set to zero.
996 48
        if ('00000000-0000-0000-0000-000000000000' === $value) {
997 4
            return;
998
        }
999
1000 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)) {
1001 20
            static::reportInvalidArgument(sprintf(
1002 20
                $message ?: 'Value %s is not a valid UUID.',
1003 20
                static::valueToString($value)
1004
            ));
1005
        }
1006 24
    }
1007
1008 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1009
    {
1010 24
        static::string($class);
1011
1012 24
        $actual = 'none';
1013
1014
        try {
1015 24
            $expression();
1016 24
        } catch (Exception $e) {
1017 20
            $actual = get_class($e);
1018 20
            if ($e instanceof $class) {
1019 20
                return;
1020
            }
1021 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...
1022 4
            $actual = get_class($e);
1023 4
            if ($e instanceof $class) {
1024 4
                return;
1025
            }
1026
        }
1027
1028 8
        static::reportInvalidArgument($message ?: sprintf(
1029 8
            'Expected to throw "%s", got "%s"',
1030 8
            $class,
1031 8
            $actual
1032
        ));
1033
    }
1034
1035 1000
    public static function __callStatic($name, $arguments)
1036
    {
1037 1000
        if ('nullOr' === substr($name, 0, 6)) {
1038 382
            if (null !== $arguments[0]) {
1039 299
                $method = lcfirst(substr($name, 6));
1040 299
                call_user_func_array(array('static', $method), $arguments);
1041
            }
1042
1043 233
            return;
1044
        }
1045
1046 618
        if ('all' === substr($name, 0, 3)) {
1047 618
            static::isIterable($arguments[0]);
1048
1049 618
            $method = lcfirst(substr($name, 3));
1050 618
            $args = $arguments;
1051
1052 618
            foreach ($arguments[0] as $entry) {
1053 618
                $args[0] = $entry;
1054
1055 618
                call_user_func_array(array('static', $method), $args);
1056
            }
1057
1058 306
            return;
1059
        }
1060
1061
        throw new BadMethodCallException('No such method: '.$name);
1062
    }
1063
1064 449
    protected static function valueToString($value)
1065
    {
1066 449
        if (null === $value) {
1067 11
            return 'null';
1068
        }
1069
1070 440
        if (true === $value) {
1071 15
            return 'true';
1072
        }
1073
1074 430
        if (false === $value) {
1075 13
            return 'false';
1076
        }
1077
1078 417
        if (is_array($value)) {
1079 1
            return 'array';
1080
        }
1081
1082 416
        if (is_object($value)) {
1083 2
            if (method_exists($value, '__toString')) {
1084 1
                return get_class($value).': '.self::valueToString($value->__toString());
1085
            }
1086
1087 1
            return get_class($value);
1088
        }
1089
1090 415
        if (is_resource($value)) {
1091 1
            return 'resource';
1092
        }
1093
1094 415
        if (is_string($value)) {
1095 329
            return '"'.$value.'"';
1096
        }
1097
1098 94
        return (string) $value;
1099
    }
1100
1101 169
    protected static function typeToString($value)
1102
    {
1103 169
        return is_object($value) ? get_class($value) : gettype($value);
1104
    }
1105
1106 136
    protected static function strlen($value)
1107
    {
1108 136
        if (!function_exists('mb_detect_encoding')) {
1109
            return strlen($value);
1110
        }
1111
1112 136
        if (false === $encoding = mb_detect_encoding($value)) {
1113
            return strlen($value);
1114
        }
1115
1116 136
        return mb_strwidth($value, $encoding);
1117
    }
1118
1119 633
    protected static function reportInvalidArgument($message)
1120
    {
1121 633
        throw new InvalidArgumentException($message);
1122
    }
1123
1124
    private function __construct()
1125
    {
1126
    }
1127
}
1128