Passed
Push — master ( e24b7d...346e4a )
by Michael
06:49
created

PrivateStaticTransformer::getClassConfig()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 12
1
<?php
2
3
namespace micmania1\config\Transformer;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
class PrivateStaticTransformer implements TransformerInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $classes = [];
14
15
    /**
16
     * @var int
17
     */
18
    protected $sort = 0;
19
20
    /**
21
     * @param array $classes
22
     */
23
    public function __construct(array $classes, $sort = 0)
24
    {
25
        $this->classes = $classes;
26
        $this->sort = $sort;
27
    }
28
29
    /**
30
     * This loops through each class and fetches the private static config for each class.
31
     */
32
    public function transform()
33
    {
34
        $config = [];
35
        foreach($this->classes as $class) {
36
            $config = array_merge($this->getClassConfig($class), $config);
37
        }
38
39
        return [$this->sort => $config];
40
    }
41
42
    /**
43
     * This is responsible for introspecting a given class and returning an
44
     * array continaing all of its private statics
45
     *
46
     * @param string $class
47
     *
48
     * @return string[]
49
     */
50
    protected function getClassConfig($class)
51
    {
52
        // Autoload the class if it exists
53
        if(!class_exists($class)) {
54
            return [];
55
        }
56
57
        /** @var \ReflectionProperty[] **/
58
        $props = (new ReflectionClass($class))
59
            ->getProperties(ReflectionProperty::IS_STATIC | ReflectionProperty::IS_PRIVATE);
60
61
        $classConfig = [];
62
63
        foreach($props as $prop) {
64
            $prop->setAccessible(true);
65
            $classConfig[$prop->getName()] = $prop->getValue();
66
        }
67
68
        return $classConfig;
69
    }
70
71
}
72