Completed
Push — master ( 9d2442...b1f272 )
by Denis
01:49
created

GridViewHelper::testConfig()   B

Complexity

Conditions 10
Paths 15

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 15
nop 2
dl 0
loc 46
rs 7.3115
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 Woo\GridView;
4
5
use Woo\GridView\Exceptions\GridViewConfigException;
6
7
class GridViewHelper
8
{
9
    private function __construct() {}
10
11
    /**
12
     * Allows to load config into object properties
13
     * @param object $object
14
     * @param array $config
15
     * @throws GridViewConfigException
16
     */
17
    public static function loadConfig($object, array $config)
18
    {
19
        foreach ($config as $key => $value) {
20
21
            if (property_exists($object, $key)) {
22
                $object->$key = $value;
23
            }
24
        }
25
    }
26
27
    /**
28
     * Allows to test attributes and types in config
29
     * @param $object
30
     * @param array $tests
31
     * @throws GridViewConfigException
32
     */
33
    public static function testConfig($object, array $tests)
34
    {
35
        foreach ($tests as $property => $test) {
36
37
            if (!property_exists($object, $property)) {
38
                throw new GridViewConfigException(
39
                    'Unable to test ' . get_class($object) . ': property ' . $property . ' does not exist'
40
                );
41
            }
42
43
            if (is_scalar($test)) {
44
45
                $testPassed = true;
46
47
                switch ($test) {
48
                    case 'int':
49
                        $testPassed = is_numeric($object->$property);
50
                        break;
51
52
                    case 'string':
53
                        $testPassed = is_string($object->$property);
54
                        break;
55
56
                    case 'array':
57
                        $testPassed = is_array($object->$property);
58
                        break;
59
60
                    case 'closure':
61
                        $testPassed = $object->$property instanceof \Closure;
62
                        break;
63
64
                    case 'any':
65
                        break;
66
67
                    default:
68
                        $testPassed = is_subclass_of($object->$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...
69
                }
70
71
                if (!$testPassed) {
72
                    throw new GridViewConfigException('
73
                        Test ' . $test . ' has failed on ' . get_class($object) . '::' . $property
74
                    );
75
                }
76
            }
77
        }
78
    }
79
80
    /**
81
     * Allows to convert options array to html string
82
     * @param array $htmlOptions
83
     * @return string
84
     */
85
    public static function htmlOptionsToString(array $htmlOptions) : string
86
    {
87
        if (empty($htmlOptions)) {
88
            return '';
89
        }
90
91
        $out = [];
92
93
        foreach ($htmlOptions as $k => $v) {
94
            $out[] = htmlentities($k) . '="' . htmlentities($v, ENT_COMPAT) . '"';
95
        }
96
97
        return implode(' ', $out);
98
    }
99
}