Passed
Push — master ( 4ff148...e24b7d )
by Michael
05:11 queued 02:53
created

PrivateStaticTransformer::getClassConfig()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 13
cp 0
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
crap 20
1
<?php
2
3
namespace micmania1\config\Transformer;
4
5
use ReflectionClass;
6
7
class PrivateStaticTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $classes = [];
13
14
    /**
15
     * @var int
16
     */
17
    protected $sort = 0;
18
19
    /**
20
     * @param array $classes
21
     */
22
    public function __construct(array $classes, $sort = 0)
23
    {
24
        $this->classes = $classes;
25
        $this->sort = $sort;
26
    }
27
28
    /**
29
     * This loops through each class and fetches the private static config for each class.
30
     */
31
    public function transform()
32
    {
33
        $config = [];
34
        foreach($this->classes as $class) {
35
            $config = array_merge($this->getClassConfig($class), $config);
36
        }
37
38
        return [$sort => $config];
0 ignored issues
show
Bug introduced by
The variable $sort does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
    }
40
41
    /**
42
     * This is responsible for introspecting a given class and returning an
43
     * array continaing all of its private statics
44
     *
45
     * @param string $class
46
     *
47
     * @return string[]
48
     */
49
    protected function getClassConfig($class)
50
    {
51
        // Autoload the class if it exists
52
        if(!class_exists($class)) {
53
            return [];
54
        }
55
56
        /** @var \ReflectionProperty[] **/
57
        $props = (new ReflectionClass($class))
58
            ->getStaticProperties();
59
60
        $classConfig = [];
61
62
        // Loop through each static property and add all private statics to the
63
        // class config
64
        foreach($props as $prop) {
65
            // We don't want non-private statics
66
            if(!$prop->isPrivate()) {
0 ignored issues
show
Bug introduced by
The method isPrivate cannot be called on $prop (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
67
                continue;
68
            }
69
70
            $prop->setAccessible(true);
0 ignored issues
show
Bug introduced by
The method setAccessible cannot be called on $prop (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
71
            $classConfig[$prop->getName()] = $prop->getValue();
0 ignored issues
show
Bug introduced by
The method getName cannot be called on $prop (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method getValue cannot be called on $prop (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
72
        }
73
74
        return $classConfig;
75
    }
76
77
}
78