Completed
Push — master ( ae2bb1...bae126 )
by Denis
14s queued 11s
created

Configurable::testConfig()   C

Complexity

Conditions 13
Paths 22

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
nc 22
nop 0
dl 0
loc 57
rs 6.6166
c 0
b 0
f 0

How to fix   Long Method    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 Woo\GridView\Traits;
4
5
use Closure;
6
use Woo\GridView\Exceptions\GridViewConfigException;
7
8
trait Configurable
9
{
10
    /**
11
     * Allows to load config into object properties
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
     * Should specify tests
29
     * @return array
30
     */
31
    abstract protected function configTests() : array;
32
33
    /**
34
     * Allows to test attributes and types in config
35
     * @throws GridViewConfigException
36
     */
37
    protected function testConfig()
38
    {
39
        foreach ($this->configTests() as $property => $tests) {
40
41
            if (!property_exists($this, $property)) {
42
                throw new GridViewConfigException(
43
                    'Unable to test ' . get_class($this) . ': property ' . $property . ' does not exist'
44
                );
45
            }
46
47
            $testPassed = true;
48
            $testMessage = 'Validation failed';
49
50
            foreach (explode('|', $tests) as $test) {
51
52
                switch ($test) {
53
                    case 'int':
54
                        $testPassed = is_numeric($this->$property);
55
                        $testMessage = 'Property should be numeric';
56
                        break;
57
58
                    case 'string':
59
                        $testPassed = is_string($this->$property);
60
                        $testMessage = 'Property should be a string';
61
                        break;
62
63
                    case 'array':
64
                        $testPassed = is_array($this->$property);
65
                        $testMessage = 'Property should be an array';
66
                        break;
67
68
                    case 'closure':
69
                        $testPassed = $this->$property instanceof Closure;
70
                        $testMessage = 'Property should be a valid callback (Closure instance)';
71
                        break;
72
73
                    case 'boolean':
74
                        $testPassed = is_bool($this->$property);
75
                        $testMessage = 'Property should be boolean';
76
                        break;
77
78
                    case 'any':
79
                        break;
80
81
                    default:
82
                        $testPassed = $testPassed || is_a($this->$property, $test) || 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...
83
                        $testMessage = 'Property should be ' . $test . ' instance/class reference, got ' . print_r($this->$property, 1);
84
                }
85
            }
86
87
            if (!$testPassed) {
88
                throw new GridViewConfigException(
89
                    'Tests ' . $tests . ' has failed on ' . get_class($this) . '::' . $property . ': ' . $testMessage
90
                );
91
            }
92
        }
93
    }
94
}