Completed
Branch master (7d7b3f)
by Ori
01:38
created

ResourceValidator::getValidationErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace frictionlessdata\datapackage\Validators;
4
5
use frictionlessdata\datapackage\Registry;
6
use frictionlessdata\datapackage\Factory;
7
8
/**
9
 * validate a resource descriptor
10
 * checks the profile attribute to determine which schema to validate with.
11
 */
12
class ResourceValidator extends BaseValidator
13
{
14
    protected function getSchemaValidationErrorClass()
15
    {
16
        return 'frictionlessdata\\datapackage\\Validators\\ResourceValidationError';
17
    }
18
19
    protected function getValidationProfile()
20
    {
21
        return Registry::getResourceValidationProfile($this->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...
22
    }
23
24
    protected function getDescriptorForValidation()
25
    {
26
        // add base path to uri fields
27
        // need to ensure all attributes exist because we are before schema validation
28
        // TODO: find a more elegant way to do it with support for registring custom url fields
29
        $descriptor = clone $this->descriptor;
30
        if (isset($descriptor->path) && is_array($descriptor->path)) {
31
            foreach ($descriptor->path as &$url) {
32
                $url = 'file://'.$url;
33
            }
34
        }
35
36
        return $descriptor;
37
    }
38
39
    protected function getValidationErrorMessage($error)
40
    {
41
        $property = $error['property'];
42
        // silly hack to only show properties within the resource of the fake datapackage
43
        // $property = str_replace("resources[0].", "", $property);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        return sprintf('[%s] %s', $property, $error['message']);
45
    }
46
47
    protected function getResourceClass()
48
    {
49
        return Factory::getResourceClass($this->descriptor);
50
    }
51
52
    protected function validateKeys()
53
    {
54
        $resourceClass = $this->getResourceClass();
55
        foreach ($this->descriptor->path as $dataSource) {
56
            foreach ($resourceClass::validateDataSource($dataSource, $this->basePath) as $error) {
57
                $this->errors[] = $error;
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...
58
            }
59
        }
60
    }
61
62
    protected function getJsonSchemaFileFromRegistry($profile)
63
    {
64
        if ($filename = Registry::getJsonSchemaFile($profile)) {
65
            return $filename;
66
        } else {
67
            return parent::getJsonSchemaFileFromRegistry($profile);
68
        }
69
    }
70
}
71