1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace micmania1\config\Transformer; |
4
|
|
|
|
5
|
|
|
use micmania1\config\ConfigCollectionInterface; |
6
|
|
|
use micmania1\config\ConfigItem; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use ReflectionProperty; |
9
|
|
|
|
10
|
|
|
class PrivateStaticTransformer implements TransformerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $classes = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
protected $sort = 0; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ConfigCollectionInterface |
24
|
|
|
*/ |
25
|
|
|
protected $collection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $classes |
29
|
|
|
*/ |
30
|
3 |
|
public function __construct(array $classes, ConfigCollectionInterface $collection) |
31
|
|
|
{ |
32
|
3 |
|
$this->classes = $classes; |
33
|
3 |
|
$this->collection = $collection; |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* This loops through each class and fetches the private static config for each class. |
38
|
|
|
*/ |
39
|
3 |
|
public function transform() |
40
|
|
|
{ |
41
|
3 |
|
$collectionClassName = get_class($this->collection); |
|
|
|
|
42
|
3 |
|
foreach($this->classes as $class) { |
43
|
|
|
// Skip if the class doesn't exist |
44
|
3 |
|
if(!class_exists($class)) { |
45
|
1 |
|
continue; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// returns an array of value and metadata |
49
|
2 |
|
$item = $this->getClassConfig($class); |
50
|
|
|
|
51
|
|
|
// Add the item to the collection |
52
|
2 |
|
$this->collection->set($class, $item); |
53
|
3 |
|
} |
54
|
|
|
|
55
|
3 |
|
return $this->collection; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This is responsible for introspecting a given class and returning an |
60
|
|
|
* array continaing all of its private statics |
61
|
|
|
* |
62
|
|
|
* @param string $class |
63
|
|
|
* |
64
|
|
|
* @return ConfigItem |
65
|
|
|
*/ |
66
|
2 |
|
protected function getClassConfig($class) |
67
|
|
|
{ |
68
|
2 |
|
$reflection = new ReflectionClass($class); |
69
|
|
|
|
70
|
|
|
/** @var \ReflectionProperty[] **/ |
71
|
2 |
|
$props = $reflection->getProperties(ReflectionProperty::IS_STATIC); |
72
|
|
|
|
73
|
2 |
|
$classConfig = []; |
74
|
|
|
|
75
|
2 |
|
foreach($props as $prop) { |
76
|
2 |
|
if(!$prop->isPrivate()) { |
77
|
|
|
// Ignore anything which isn't private |
78
|
2 |
|
continue; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$prop->setAccessible(true); |
82
|
2 |
|
$classConfig[$prop->getName()] = $prop->getValue(); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
// Create the metadata for our new item |
86
|
|
|
$metadata = [ |
87
|
2 |
|
'filename' => $reflection->getFileName(), |
88
|
2 |
|
'class' => $class, |
89
|
2 |
|
'transformer' => static::class |
90
|
2 |
|
]; |
91
|
|
|
|
92
|
2 |
|
return new ConfigItem($classConfig, $metadata); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.