Failed Conditions
Push — master ( 2a54fd...14b391 )
by Gert de
02:54
created

Assert   F

Complexity

Total Complexity 286

Size/Duplication

Total Lines 1032
Duplicated Lines 30.62 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.01%

Importance

Changes 0
Metric Value
wmc 286
lcom 1
cbo 0
dl 316
loc 1032
ccs 585
cts 603
cp 0.9701
rs 1.472
c 0
b 0
f 0

87 Methods

Rating   Name   Duplication   Size   Complexity  
A string() 0 9 3
A stringNotEmpty() 0 5 1
A integer() 0 9 3
A integerish() 0 9 4
A float() 0 9 3
A numeric() 0 9 3
A boolean() 0 9 3
A scalar() 0 9 3
A object() 0 9 3
A resource() 0 17 6
A isCallable() 0 9 3
A isArray() 0 9 3
A isArrayAccessible() 9 9 4
A isCountable() 9 9 4
A isIterable() 9 9 4
A isInstanceOf() 0 10 3
A notInstanceOf() 0 10 3
A isInstanceOfAny() 0 14 4
A isEmpty() 0 9 3
A notEmpty() 0 9 3
A notNull() 0 8 3
A ip() 9 9 3
A ipv4() 9 9 3
A ipv6() 9 9 3
A uniqueValues() 0 15 4
A notEq() 0 9 3
A notSame() 0 9 3
A range() 11 11 4
A oneOf() 0 10 3
A contains() 10 10 3
A notContains() 10 10 3
A startsWith() 10 10 3
A startsWithLetter() 0 18 4
A endsWith() 10 10 3
A notRegex() 0 11 3
A natural() 0 9 4
A null() 0 9 3
A true() 0 9 3
A false() 0 9 3
A eq() 0 10 3
A same() 0 10 3
A greaterThan() 0 10 3
A greaterThanEq() 0 10 3
A lessThan() 0 10 3
A lessThanEq() 0 10 3
A notWhitespaceOnly() 0 9 3
A regex() 0 9 3
A fileExists() 11 11 3
A isList() 0 8 5
A uuid() 0 17 4
A alpha() 14 14 3
A digits() 14 14 3
A alnum() 14 14 3
A lower() 14 14 3
A upper() 14 14 3
A length() 0 10 3
A minLength() 0 10 3
A maxLength() 0 10 3
A lengthBetween() 13 13 4
A file() 11 11 3
A directory() 11 11 3
A readable() 0 9 3
A writable() 0 9 3
A classExists() 0 9 3
A subclassOf() 0 10 3
A interfaceExists() 0 9 3
A implementsInterface() 10 10 3
A propertyExists() 9 9 3
A propertyNotExists() 9 9 3
A methodExists() 9 9 3
A methodNotExists() 9 9 3
A keyExists() 9 9 4
A keyNotExists() 9 9 4
A count() 0 8 2
A minCount() 10 10 3
A maxCount() 10 10 3
A countBetween() 0 13 4
B throws() 0 26 6
A __callStatic() 0 28 5
B valueToString() 0 36 9
A typeToString() 0 4 2
A strlen() 0 12 3
A reportInvalidArgument() 0 4 1
A __construct() 0 3 1
A isTraversable() 0 17 4
A unicodeLetters() 11 11 3
A isMap() 0 14 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Assert often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Assert, and based on these observations, apply Extract Interface, too.

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

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1064 20
            static::reportInvalidArgument(
1065 20
                $message ?: 'Expected list - non-associative array.'
1066
            );
1067
        }
1068 4
    }
1069
1070 16
    public static function isMap($array, $message = '')
1071
    {
1072
        if (
1073 16
            !is_array($array) ||
1074 16
            !$array ||
0 ignored issues
show
Bug Best Practice introduced by
The expression $array of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1075
            array_keys($array) !== array_filter(array_keys($array), function ($key) {
1076 12
                return is_string($key);
1077 16
            })
1078
        ) {
1079 12
            static::reportInvalidArgument(
1080 12
                $message ?: 'Expected map - associative array with string keys.'
1081
            );
1082
        }
1083 4
    }
1084
1085 56
    public static function uuid($value, $message = '')
1086
    {
1087 56
        $value = str_replace(array('urn:', 'uuid:', '{', '}'), '', $value);
1088
1089
        // The nil UUID is special form of UUID that is specified to have all
1090
        // 128 bits set to zero.
1091 56
        if ('00000000-0000-0000-0000-000000000000' === $value) {
1092 4
            return;
1093
        }
1094
1095 52
        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)) {
1096 20
            static::reportInvalidArgument(sprintf(
1097 20
                $message ?: 'Value %s is not a valid UUID.',
1098 20
                static::valueToString($value)
1099
            ));
1100
        }
1101 32
    }
1102
1103 24
    public static function throws(Closure $expression, $class = 'Exception', $message = '')
1104
    {
1105 24
        static::string($class);
1106
1107 24
        $actual = 'none';
1108
1109
        try {
1110 24
            $expression();
1111 24
        } catch (Exception $e) {
1112 20
            $actual = get_class($e);
1113 20
            if ($e instanceof $class) {
1114 20
                return;
1115
            }
1116 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...
1117 4
            $actual = get_class($e);
1118 4
            if ($e instanceof $class) {
1119 4
                return;
1120
            }
1121
        }
1122
1123 8
        static::reportInvalidArgument($message ?: sprintf(
1124 8
            'Expected to throw "%s", got "%s"',
1125
            $class,
1126
            $actual
1127
        ));
1128
    }
1129
1130 1370
    public static function __callStatic($name, $arguments)
1131
    {
1132 1370
        if ('nullOr' === substr($name, 0, 6)) {
1133 509
            if (null !== $arguments[0]) {
1134 417
                $method = lcfirst(substr($name, 6));
1135 417
                call_user_func_array(array('static', $method), $arguments);
1136
            }
1137
1138 294
            return;
1139
        }
1140
1141 861
        if ('all' === substr($name, 0, 3)) {
1142 860
            static::isIterable($arguments[0]);
1143
1144 860
            $method = lcfirst(substr($name, 3));
1145 860
            $args = $arguments;
1146
1147 860
            foreach ($arguments[0] as $entry) {
1148 860
                $args[0] = $entry;
1149
1150 860
                call_user_func_array(array('static', $method), $args);
1151
            }
1152
1153 410
            return;
1154
        }
1155
1156 1
        throw new BadMethodCallException('No such method: '.$name);
1157
    }
1158
1159 682
    protected static function valueToString($value)
1160
    {
1161 682
        if (null === $value) {
1162 20
            return 'null';
1163
        }
1164
1165 664
        if (true === $value) {
1166 15
            return 'true';
1167
        }
1168
1169 654
        if (false === $value) {
1170 25
            return 'false';
1171
        }
1172
1173 629
        if (is_array($value)) {
1174 13
            return 'array';
1175
        }
1176
1177 616
        if (is_object($value)) {
1178 2
            if (method_exists($value, '__toString')) {
1179 1
                return get_class($value).': '.self::valueToString($value->__toString());
1180
            }
1181
1182 1
            return get_class($value);
1183
        }
1184
1185 615
        if (is_resource($value)) {
1186 1
            return 'resource';
1187
        }
1188
1189 615
        if (is_string($value)) {
1190 517
            return '"'.$value.'"';
1191
        }
1192
1193 106
        return (string) $value;
1194
    }
1195
1196 169
    protected static function typeToString($value)
1197
    {
1198 169
        return is_object($value) ? get_class($value) : gettype($value);
1199
    }
1200
1201 168
    protected static function strlen($value)
1202
    {
1203 168
        if (!function_exists('mb_detect_encoding')) {
1204
            return strlen($value);
1205
        }
1206
1207 168
        if (false === $encoding = mb_detect_encoding($value)) {
1208
            return strlen($value);
1209
        }
1210
1211 168
        return mb_strlen($value, $encoding);
1212
    }
1213
1214 906
    protected static function reportInvalidArgument($message)
1215
    {
1216 906
        throw new InvalidArgumentException($message);
1217
    }
1218
1219
    private function __construct()
1220
    {
1221
    }
1222
}
1223