Passed
Push — master ( 346e4a...b3bab7 )
by Michael
02:23
created

PrivateStaticTransformer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 69
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A transform() 0 14 3
A getClassConfig() 0 20 3
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 3
    public function __construct(array $classes, $sort = 0)
24
    {
25 3
        $this->classes = $classes;
26 3
        $this->sort = $sort;
27 3
    }
28
29
    /**
30
     * This loops through each class and fetches the private static config for each class.
31
     */
32 3
    public function transform()
33
    {
34 3
        $config = [];
35 3
        foreach($this->classes as $class) {
36
            // Skip if the class doesn't exist
37 3
            if(!class_exists($class)) {
38 1
                continue;
39
            }
40
41 2
            $config[$class] = $this->getClassConfig($class);
42 3
        }
43
44 3
        return [$this->sort => $config];
45
    }
46
47
    /**
48
     * This is responsible for introspecting a given class and returning an
49
     * array continaing all of its private statics
50
     *
51
     * @param string $class
52
     *
53
     * @return string[]
54
     */
55 2
    protected function getClassConfig($class)
56
    {
57
        /** @var \ReflectionProperty[] **/
58 2
        $props = (new ReflectionClass($class))
59 2
            ->getProperties(ReflectionProperty::IS_STATIC);
60
61 2
        $classConfig = [];
62
63 2
        foreach($props as $prop) {
64 2
            if(!$prop->isPrivate()) {
65
                // Ignore anything which isn't private
66 2
                continue;
67
            }
68
69 2
            $prop->setAccessible(true);
70 2
            $classConfig[$prop->getName()] = $prop->getValue();
71 2
        }
72
73 2
        return $classConfig;
74
    }
75
76
}
77