Completed
Pull Request — master (#13)
by Viacheslav
07:58
created

Type::readString()   C

Complexity

Conditions 12
Paths 50

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 5.1612
c 0
b 0
f 0
cc 12
eloc 32
nc 50
nop 2

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 Swaggest\JsonSchema\Constraint;
4
5
class Type implements Constraint
6
{
7
    // TODO deprecate in favour of JsonSchema::<TYPE> ?
8
    const OBJECT = 'object';
9
    const STRING = 'string';
10
    const INTEGER = 'integer';
11
    const NUMBER = 'number';
12
    const ARR = 'array';
13
    const BOOLEAN = 'boolean';
14
    const NULL = 'null';
15
16
    public static function readString($types, &$data)
17
    {
18
        if (!is_array($types)) {
19
            $types = array($types);
20
        }
21
        $ok = false;
22
        foreach ($types as $type) {
23
            switch ($type) {
24
                case self::OBJECT:
25
                    break;
26
                case self::ARR:
27
                    break;
28
                case self::STRING:
29
                    $ok = true;
30
                    break;
31
                case self::NUMBER:
32
                    $newData = filter_var($data, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
33
                    $ok = is_float($newData);
34
                    break;
35
                case self::INTEGER:
36
                    $newData = filter_var($data, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
37
                    $ok = is_int($newData);
38
                    break;
39
                case self::BOOLEAN:
40
                    $newData = filter_var($data, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
41
                    $ok = is_bool($data);
42
                    break;
43
                case self::NULL:
44
                    break;
45
            }
46
            if ($ok) {
47
                if (isset($newData)) {
48
                    $data = $newData;
49
                }
50
                return true;
51
            }
52
        }
53
        return false;
54
    }
55
56
    public static function isValid($types, $data)
57
    {
58
        if (!is_array($types)) {
59
            $types = array($types);
60
        }
61
        $ok = false;
62
        foreach ($types as $type) {
63
            switch ($type) {
64
                case self::OBJECT:
65
                    $ok = $data instanceof \stdClass;
66
                    break;
67
                case self::ARR:
68
                    $ok = is_array($data);
69
                    break;
70
                case self::STRING:
71
                    $ok = is_string($data);
72
                    break;
73
                case self::INTEGER:
74
                    $ok = is_int($data);
75
                    break;
76
                case self::NUMBER:
77
                    $ok = is_int($data) || is_float($data);
78
                    break;
79
                case self::BOOLEAN:
80
                    $ok = is_bool($data);
81
                    break;
82
                case self::NULL:
83
                    $ok = null === $data;
84
                    break;
85
            }
86
            if ($ok) {
87
                return true;
88
            }
89
        }
90
        return false;
91
    }
92
93
94
}