PrivateStaticTransformer   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 84
ccs 27
cts 27
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 17 3
B getClassConfig() 0 27 3
1
<?php
2
3
namespace micmania1\config\Transformer;
4
5
use micmania1\config\ConfigCollectionInterface;
6
use ReflectionClass;
7
use ReflectionProperty;
8
9
class PrivateStaticTransformer implements TransformerInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $classes = [];
15
16
    /**
17
     * @var int
18
     */
19
    protected $sort = 0;
20
21
    /**
22
     * @var ConfigCollectionInterface
23
     */
24
    protected $collection;
25
26
    /**
27
     * @param array $classes
28
     */
29 3
    public function __construct(array $classes, ConfigCollectionInterface $collection)
30
    {
31 3
        $this->classes = $classes;
32 3
        $this->collection = $collection;
33 3
    }
34
35
    /**
36
     * This loops through each class and fetches the private static config for each class.
37
     */
38 3
    public function transform()
39
    {
40 3
        foreach($this->classes as $class) {
41
            // Skip if the class doesn't exist
42 3
            if(!class_exists($class)) {
43 1
                continue;
44
            }
45
46
            // returns an array of value and metadata
47 2
            $item = $this->getClassConfig($class);
48
49
            // Add the item to the collection
50 2
            $this->collection->set($class, $item['value'], $item['metadata']);
51 3
        }
52
53 3
        return $this->collection;
54
    }
55
56
    /**
57
     * This is responsible for introspecting a given class and returning an
58
     * array continaing all of its private statics
59
     *
60
     * @param string $class
61
     *
62
     * @return mixed
63
     */
64 2
    protected function getClassConfig($class)
65
    {
66 2
        $reflection = new ReflectionClass($class);
67
68
        /** @var ReflectionProperty[] **/
69 2
        $props = $reflection->getProperties(ReflectionProperty::IS_STATIC);
70
71 2
        $classConfig = [];
72 2
        foreach($props as $prop) {
73 2
            if(!$prop->isPrivate()) {
74
                // Ignore anything which isn't private
75 2
                continue;
76
            }
77
78 2
            $prop->setAccessible(true);
79 2
            $classConfig[$prop->getName()] = $prop->getValue();
80 2
        }
81
82
        // Create the metadata for our new item
83
        $metadata = [
84 2
            'filename' => $reflection->getFileName(),
85 2
            'class' => $class,
86 2
            'transformer' => static::class
87 2
        ];
88
89 2
        return ['value' => $classConfig, 'metadata' => $metadata];
90
    }
91
92
}
93