Completed
Push — master ( 77c4c6...fbe049 )
by Ori
04:20
created

SchemaValidator::validateKeys()   C

Complexity

Conditions 13
Paths 78

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 78
nop 0
dl 0
loc 41
rs 5.1234
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 frictionlessdata\tableschema;
4
5
/**
6
 * validates a table schema descriptor object
7
 * returns a list of validation errors.
8
 */
9
class SchemaValidator
10
{
11
    /**
12
     * @param object $descriptor
13
     *
14
     * @return SchemaValidationError[]
15
     */
16
    public static function validate($descriptor)
17
    {
18
        $validator = new self($descriptor);
19
20
        return $validator->getValidationErrors();
21
    }
22
23
    /**
24
     * @param object $descriptor
25
     */
26
    public function __construct($descriptor)
27
    {
28
        $this->descriptor = $descriptor;
0 ignored issues
show
Bug introduced by
The property descriptor does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
        $this->errors = [];
0 ignored issues
show
Bug introduced by
The property errors does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30
    }
31
32
    /**
33
     * @return SchemaValidationError[]
34
     */
35
    public function getValidationErrors()
36
    {
37
        $this->errors = [];
38
        $this->validateSchema();
39
        if (count($this->errors) == 0) {
40
            $this->validateKeys();
41
        }
42
43
        return $this->errors;
44
    }
45
46
    /**
47
     * @param int   $code
48
     * @param mixed $extraDetails
49
     */
50
    protected function addError($code, $extraDetails = null)
51
    {
52
        $this->errors[] = new SchemaValidationError($code, $extraDetails);
53
    }
54
55
    protected function validateSchema()
56
    {
57
        // Validate
58
        $validator = new \JsonSchema\Validator();
59
        $descriptor = json_decode(json_encode($this->descriptor));
60
        $this->applyForeignKeysResourceHack($descriptor);
61
        $validator->validate(
62
            $descriptor,
63
            (object) ['$ref' => 'file://'.realpath(dirname(__FILE__)).'/schemas/table-schema.json']
64
        );
65
        if (!$validator->isValid()) {
66
            foreach ($validator->getErrors() as $error) {
67
                $this->addError(
68
                    SchemaValidationError::SCHEMA_VIOLATION,
69
                    sprintf('[%s] %s', $error['property'], $error['message'])
70
                );
71
            }
72
        }
73
    }
74
75
    protected function validateKeys()
76
    {
77
        $fieldNames = array_map(function ($field) {
78
            return $field->name;
79
        }, $this->descriptor->fields);
80
        if (isset($this->descriptor->primaryKey)) {
81
            $primaryKey = is_array($this->descriptor->primaryKey) ? $this->descriptor->primaryKey : [$this->descriptor->primaryKey];
82
            foreach ($primaryKey as $primaryKeyField) {
83
                if (!in_array($primaryKeyField, $fieldNames)) {
84
                    $this->addError(
85
                        SchemaValidationError::SCHEMA_VIOLATION,
86
                        "primary key must refer to a field name ({$primaryKeyField})"
87
                    );
88
                }
89
            }
90
        }
91
        if (isset($this->descriptor->foreignKeys)) {
92
            foreach ($this->descriptor->foreignKeys as $foreignKey) {
93
                $fields = is_array($foreignKey->fields) ? $foreignKey->fields : [$foreignKey->fields];
94
                foreach ($fields as $field) {
95
                    if (!in_array($field, $fieldNames)) {
96
                        $this->addError(
97
                            SchemaValidationError::SCHEMA_VIOLATION,
98
                            "foreign key fields must refer to a field name ({$field})"
99
                        );
100
                    }
101
                }
102
                if ($foreignKey->reference->resource == '') {
103
                    // empty resource = reference to self
104
                    foreach ($foreignKey->reference->fields as $field) {
105
                        if (!in_array($field, $fieldNames)) {
106
                            $this->addError(
107
                                SchemaValidationError::SCHEMA_VIOLATION,
108
                                "foreign key reference to self must refer to a field name ({$field})"
109
                            );
110
                        }
111
                    }
112
                }
113
            }
114
        }
115
    }
116
117
    protected function applyForeignKeysResourceHack($descriptor)
118
    {
119
        if (isset($descriptor->foreignKeys) && is_array($descriptor->foreignKeys)) {
120
            foreach ($descriptor->foreignKeys as $foreignKey) {
121
                // the resource field of foreign keys has problems validating as standard uri string
122
                // we just override the validation entirely by placing a valid uri
123
                if (
124
                    is_object($foreignKey)
125
                    && isset($foreignKey->reference) && is_object($foreignKey->reference)
126
                    && isset($foreignKey->reference->resource) && !empty($foreignKey->reference->resource)
127
                ) {
128
                    $foreignKey->reference->resource = 'void://'.$foreignKey->reference->resource;
129
                }
130
            }
131
        }
132
    }
133
}
134