Completed
Push — master ( e24db7...5dd27b )
by Matt
02:58
created

Type::validate()   C

Complexity

Conditions 12
Paths 9

Size

Total Lines 49
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 12.003

Importance

Changes 0
Metric Value
cc 12
eloc 34
nc 9
nop 3
dl 0
loc 49
ccs 35
cts 36
cp 0.9722
crap 12.003
rs 5.1474
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ValidationError;
7
8
class Type implements PropertyConstraint
9
{
10
    const KEYWORD = 'type';
11
12
    /**
13
     * {@inheritdoc}
14
     */
15 80
    public static function validate($value, $type, $pointer = null)
16
    {
17 80
        if (is_array($type)) {
18 8
            return self::anyType($value, $type, $pointer);
19
        }
20
21
        switch ($type) {
22 80
            case 'object':
23 24
                return self::validateType($value, $type, 'is_object', $pointer);
24 76
            case 'array':
25 14
                return self::validateType($value, $type, 'is_array', $pointer);
26 72
            case 'boolean':
27 16
                return self::validateType($value, $type, 'is_bool', $pointer);
28 68
            case 'null':
29 8
                return self::validateType($value, $type, 'is_null', $pointer);
30 68
            case 'number':
31 12
                return self::validateType(
32 12
                    $value,
33 12
                    $type,
34 12
                    'League\JsonGuard\is_json_number',
35
                    $pointer
36 12
                );
37 64
            case 'integer':
38 60
                return self::validateType(
39 60
                    $value,
40 60
                    $type,
41 60
                    'League\JsonGuard\is_json_integer',
42
                    $pointer
43 60
                );
44 44
            case 'string':
45 44
                return self::validateType(
46 44
                    $value,
47 44
                    $type,
48 44
                    function ($value) {
49 44
                        if (is_string($value)) {
50
                            // Make sure the string isn't actually a number that was too large
51
                            // to be cast to an int on this platform.  This will only happen if
52
                            // you decode JSON with the JSON_BIGINT_AS_STRING option.
53 40
                            if (!(ctype_digit($value) && JsonGuard\compare($value, PHP_INT_MAX) === 1)) {
54 38
                                return true;
55
                            }
56 2
                        }
57
58 32
                        return false;
59 44
                    },
60
                    $pointer
61 44
                );
62
        }
63
    }
64
65
    /**
66
     * @param mixed    $value
67
     * @param string   $type
68
     * @param callable $callable
69
     * @param string   $pointer
70
     *
71
     * @return \League\JsonGuard\ValidationError|null
72
     */
73 80
    private static function validateType($value, $type, callable $callable, $pointer)
74
    {
75 80
        if (call_user_func($callable, $value) === true) {
76 76
            return null;
77
        }
78
79 68
        return new ValidationError(
80 68
            'Value {value} is not a(n) {type}',
81 68
            self::KEYWORD,
82 68
            $value,
83 68
            $pointer,
84 68
            ['value' => $value, 'type' => $type]
85 68
        );
86
    }
87
88
    /**
89
     * @param mixed  $value
90
     * @param array  $choices
91
     * @param string $pointer
92
     *
93
     * @return \League\JsonGuard\ValidationError|null
94
     */
95 8
    private static function anyType($value, array $choices, $pointer)
96
    {
97 8
        foreach ($choices as $type) {
98 8
            $error = static::validate($value, $type, $pointer);
99 8
            if (is_null($error)) {
100 8
                return null;
101
            }
102 8
        }
103
104 8
        return new ValidationError(
105 8
            'Value {value} is not one of: {choices}',
106 8
            self::KEYWORD,
107 8
            $value,
108 8
            $pointer,
109
            [
110 8
                'value'   => $value,
111
                'choices' => $choices
112 8
            ]
113 8
        );
114
    }
115
}
116