Completed
Pull Request — master (#88)
by Matt
56:34 queued 23:44
created

Format::validate()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 53
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 48
CRAP Score 7.0004

Importance

Changes 0
Metric Value
cc 7
eloc 43
nc 7
nop 3
dl 0
loc 53
ccs 48
cts 49
cp 0.9796
crap 7.0004
rs 7.5251
c 0
b 0
f 0

How to fix   Long Method   

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\Assert;
6
use League\JsonGuard\ValidationError;
7
use League\JsonGuard\Validator;
8
9
class Format implements Constraint
10
{
11
    const KEYWORD = 'format';
12
13
    // @codingStandardsIgnoreStart
14
    // @see https://www.w3.org/TR/2012/REC-xmlschema11-2-20120405/datatypes.html#dateTime-lexical-mapping
15
    const DATE_TIME_PATTERN = '/^-?([1-9][0-9]{3,}|0[0-9]{3})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])T(([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]+)?|(24:00:00(\.0+)?))(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00))?$/';
16
    // @codingStandardsIgnoreEnd
17
18
    const HOST_NAME_PATTERN = '/^[_a-z]+\.([_a-z]+\.?)+$/i';
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 20
    public function validate($value, $parameter, Validator $validator)
24
    {
25 20
        Assert::type($parameter, 'string', self::KEYWORD, $validator->getPointer());
26
27
        switch ($parameter) {
28 18
            case 'date-time':
29 14
                return self::validateRegex(
30 14
                    $parameter,
31 14
                    $value,
32 14
                    self::DATE_TIME_PATTERN,
33 14
                    $validator->getPointer()
34 14
                );
35 6
            case 'uri':
36 4
                return self::validateFilter(
37 4
                    $parameter,
38 4
                    $value,
39 4
                    FILTER_VALIDATE_URL,
40 4
                    null,
41 4
                    $validator->getPointer()
42 4
                );
43 4
            case 'email':
44 4
                return self::validateFilter(
45 4
                    $parameter,
46 4
                    $value,
47 4
                    FILTER_VALIDATE_EMAIL,
48 4
                    null,
49 4
                    $validator->getPointer()
50 4
                );
51 2
            case 'ipv4':
52 2
                return self::validateFilter(
53 2
                    $parameter,
54 2
                    $value,
55 2
                    FILTER_VALIDATE_IP,
56 2
                    FILTER_FLAG_IPV4,
57 2
                    $validator->getPointer()
58 2
                );
59 2
            case 'ipv6':
60 2
                return self::validateFilter(
61 2
                    $parameter,
62 2
                    $value,
63 2
                    FILTER_VALIDATE_IP,
64 2
                    FILTER_FLAG_IPV6,
65 2
                    $validator->getPointer()
66 2
                );
67 2
            case 'hostname':
68 2
                return self::validateRegex(
69 2
                    $parameter,
70 2
                    $value,
71 2
                    self::HOST_NAME_PATTERN,
72 2
                    $validator->getPointer()
73 2
                );
74
        }
75
    }
76
77
    /**
78
     * @param string $format
79
     * @param mixed $value
80
     * @param string $pattern
81
     * @param string $pointer
82
     *
83
     * @return \League\JsonGuard\ValidationError|null
84
     */
85 14
    private static function validateRegex($format, $value, $pattern, $pointer)
86
    {
87 14
        if (!is_string($value) || preg_match($pattern, $value) === 1) {
88 4
            return null;
89
        }
90
91 12
        return new ValidationError(
92 12
            'Value {value} does not match the format {format}',
93 12
            self::KEYWORD,
94 12
            $value,
95 12
            $pointer,
96 12
            ['value' => $value, 'format' => $format]
97 12
        );
98
    }
99
100
    /**
101
     * @param string $format
102
     * @param mixed  $value
103
     * @param int    $filter
104
     * @param mixed  $options
105
     * @param string $pointer
106
     *
107
     * @return \League\JsonGuard\ValidationError|null
108
     */
109 6
    private static function validateFilter($format, $value, $filter, $options, $pointer)
110
    {
111 6
        if (!is_string($value) || filter_var($value, $filter, $options) !== false) {
112 6
            return null;
113
        }
114
115
        // This workaround allows otherwise valid protocol relative urls to pass.
116
        // @see https://bugs.php.net/bug.php?id=72301
117 2
        if ($filter === FILTER_VALIDATE_URL && is_string($value) && strpos($value, '//') === 0) {
118 2
            if (filter_var('http:' . $value, $filter, $options) !== false) {
119 2
                return null;
120
            }
121
        }
122
123 2
        return new ValidationError(
124 2
            'Value {value} does not match the format {format}',
125 2
            self::KEYWORD,
126 2
            $value,
127 2
            $pointer,
128 2
            ['value' => $value, 'format' => $format]
129 2
        );
130
    }
131
}
132