Completed
Pull Request — master (#73)
by Matt
11:50
created

Format::validate()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 53
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 7.0007

Importance

Changes 0
Metric Value
cc 7
eloc 43
nc 7
nop 3
dl 0
loc 53
rs 7.5251
c 0
b 0
f 0
ccs 40
cts 41
cp 0.9756
crap 7.0007

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