Completed
Push — master ( 481507...e81850 )
by Denis
01:52
created

Configurable::loadConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Woo\GridView\Traits;
4
5
use Woo\GridView\Exceptions\GridViewConfigException;
6
7
trait Configurable
8
{
9
    /**
10
     * Allows to load config into object properties
11
     * @param object $object
0 ignored issues
show
Bug introduced by
There is no parameter named $object. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
12
     * @param array $config
13
     * @throws GridViewConfigException
14
     */
15
    public function loadConfig(array $config)
16
    {
17
        foreach ($config as $key => $value) {
18
19
            if (property_exists($this, $key)) {
20
                $this->$key = $value;
21
            }
22
        }
23
24
        $this->testConfig();
25
    }
26
27
    /**
28
     * Override this method in classes
29
     * @return array
30
     */
31
    protected function configTests() : array
32
    {
33
        return [];
34
    }
35
36
    /**
37
     * Allows to test attributes and types in config
38
     * @param $object
39
     * @param array $tests
0 ignored issues
show
Bug introduced by
There is no parameter named $tests. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
     * @throws GridViewConfigException
41
     */
42
    protected function testConfig()
43
    {
44
        foreach ($this->configTests() as $property => $test) {
45
46
            if (!property_exists($this, $property)) {
47
                throw new GridViewConfigException(
48
                    'Unable to test ' . get_class($this) . ': property ' . $property . ' does not exist'
49
                );
50
            }
51
52
            if (is_scalar($test)) {
53
54
                $testPassed = true;
55
56
                switch ($test) {
57
                    case 'int':
58
                        $testPassed = is_numeric($this->$property);
59
                        break;
60
61
                    case 'string':
62
                        $testPassed = is_string($this->$property);
63
                        break;
64
65
                    case 'array':
66
                        $testPassed = is_array($this->$property);
67
                        break;
68
69
                    case 'closure':
70
                        $testPassed = $this->$property instanceof \Closure;
71
                        break;
72
73
                    case 'any':
74
                        break;
75
76
                    default:
77
                        $testPassed = is_subclass_of($this->$property, $test);
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $test can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
78
                }
79
80
                if (!$testPassed) {
81
                    throw new GridViewConfigException('
82
                        Test ' . $test . ' has failed on ' . get_class($this) . '::' . $property
83
                    );
84
                }
85
            }
86
        }
87
    }
88
}