1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Solidifier; |
4
|
|
|
|
5
|
|
|
use Solidifier\Visitors\Encapsulation\PublicAttributes; |
6
|
|
|
use Solidifier\Visitors\GetterSetter\FluidSetters; |
7
|
|
|
use Solidifier\Visitors\Encapsulation\EncapsulationViolation; |
8
|
|
|
use Solidifier\Visitors\DependencyInjection\MagicalInstantiation; |
9
|
|
|
use Solidifier\Visitors\DependencyInjection\StrongCoupling; |
10
|
|
|
use Solidifier\Visitors\PreAnalyze\ObjectTypes; |
11
|
|
|
use Solidifier\Visitors\DependencyInjection\InterfaceSegregation; |
12
|
|
|
use Solidifier\Visitors\Encapsulation\PublicClass; |
13
|
|
|
|
14
|
|
|
class ConfigurationHandler |
15
|
|
|
{ |
16
|
|
|
private |
17
|
|
|
$configuration, |
|
|
|
|
18
|
|
|
$objectTypesList, |
19
|
|
|
$visitors; |
20
|
|
|
|
21
|
|
|
public function __construct(array $configuration, ObjectTypes $objectTypesList) |
22
|
|
|
{ |
23
|
|
|
$this->configuration = $configuration; |
|
|
|
|
24
|
|
|
$this->objectTypesList = $objectTypesList; |
25
|
|
|
|
26
|
|
|
$this->initializeVisitors(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function configure(VisitableAnalyzer $analyzer) |
30
|
|
|
{ |
31
|
|
|
$this->addPreAnalyzeVisitors($analyzer); |
32
|
|
|
$this->addAnalyzeVisitors($analyzer); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function addPreAnalyzeVisitors(VisitableAnalyzer $analyzer) |
36
|
|
|
{ |
37
|
|
|
$traverseName = 'preAnalyze'; |
38
|
|
|
|
39
|
|
|
// this visitor must not be disabled |
40
|
|
|
$analyzer->addVisitor($traverseName, $this->objectTypesList); |
41
|
|
|
|
42
|
|
|
$visitors = array(); |
43
|
|
|
|
44
|
|
|
return $this->addVisitors($analyzer, $visitors, $traverseName); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function initializeVisitors() |
48
|
|
|
{ |
49
|
|
|
$this->visitors = array( |
50
|
|
|
|
51
|
|
|
'property.public' => function(array $config) { |
|
|
|
|
52
|
|
|
return new PublicAttributes(); |
53
|
|
|
}, |
54
|
|
|
|
55
|
|
|
'class.public' => function(array $config) { |
56
|
|
|
$visitor = new PublicClass(); |
57
|
|
|
|
58
|
|
|
if(isset($config['threshold']) && is_numeric($config['threshold'])) |
59
|
|
|
{ |
60
|
|
|
$visitor->setThreshold($config['threshold']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if(isset($config['minMethodCount']) && is_numeric($config['minMethodCount'])) |
64
|
|
|
{ |
65
|
|
|
$visitor->setMinMethodCount($config['minMethodCount']); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $visitor; |
69
|
|
|
}, |
70
|
|
|
|
71
|
|
|
'getterSetter.fluid' => function(array $config) { |
|
|
|
|
72
|
|
|
return new FluidSetters(); |
73
|
|
|
}, |
74
|
|
|
|
75
|
|
|
'getterSetter.encapsulationViolation' => function(array $config) { |
|
|
|
|
76
|
|
|
return new EncapsulationViolation(); |
77
|
|
|
}, |
78
|
|
|
|
79
|
|
|
'dependency.magical' => function(array $config) { |
|
|
|
|
80
|
|
|
return new MagicalInstantiation(); |
81
|
|
|
}, |
82
|
|
|
|
83
|
|
|
'dependency.strongCoupling' => function(array $config) { |
84
|
|
|
$visitor = new StrongCoupling(); |
85
|
|
|
|
86
|
|
|
$visitor->addExcludePattern('~Iterator$~') |
87
|
|
|
->addExcludePattern('~^Null~') |
88
|
|
|
->addExcludePattern('~Exception$~'); |
89
|
|
|
|
90
|
|
|
if(isset($config['excludePatterns'])) |
91
|
|
|
{ |
92
|
|
|
foreach($config['excludePatterns'] as $pattern) |
93
|
|
|
{ |
94
|
|
|
$visitor->addExcludePattern("~$pattern~"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if(isset($config['allowedClasses'])) |
99
|
|
|
{ |
100
|
|
|
foreach($config['allowedClasses'] as $fullyQualifiedClassName) |
101
|
|
|
{ |
102
|
|
|
$visitor->addAllowedClass($fullyQualifiedClassName); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $visitor; |
107
|
|
|
}, |
108
|
|
|
|
109
|
|
|
'dependency.interfaceSegregation' => function(array $config) { |
|
|
|
|
110
|
|
|
return new InterfaceSegregation($this->objectTypesList); |
111
|
|
|
}, |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function addAnalyzeVisitors(VisitableAnalyzer $analyzer) |
116
|
|
|
{ |
117
|
|
|
return $this->addVisitors($analyzer, $this->visitors, 'analyze'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function addVisitors(VisitableAnalyzer $analyzer, array $visitors, $traverse) |
121
|
|
|
{ |
122
|
|
|
foreach($visitors as $key => $closure) |
123
|
|
|
{ |
124
|
|
|
$config = array(); |
125
|
|
|
if(isset($this->configuration[$key])) |
126
|
|
|
{ |
127
|
|
|
$config = $this->configuration[$key]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if(! isset($config['enabled']) || $config['enabled'] === true) |
131
|
|
|
{ |
132
|
|
|
$analyzer->addVisitor($traverse, $closure($config)); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.