1 | <?php namespace Limoncello\Application\Settings; |
||
28 | class InstanceSettingsProvider implements SettingsProviderInterface |
||
29 | { |
||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $applicationData; |
||
34 | |||
35 | /** |
||
36 | * @var SettingsInterface[] |
||
37 | */ |
||
38 | private $instances = []; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $isProcessed = true; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | private $settingsMap = []; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | private $settingsData = []; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | private $ambiguousMap = []; |
||
59 | |||
60 | /** |
||
61 | * @param array $applicationData |
||
62 | */ |
||
63 | 13 | public function __construct(array $applicationData) |
|
64 | { |
||
65 | 13 | $this->applicationData = $applicationData; |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | 3 | public function has(string $className): bool |
|
72 | { |
||
73 | 3 | $this->checkInstancesAreProcessed(); |
|
74 | |||
75 | 3 | $result = array_key_exists($className, $this->getSettingsMap()); |
|
76 | |||
77 | 3 | return $result; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $className |
||
82 | * |
||
83 | * @return array |
||
84 | */ |
||
85 | 3 | public function get(string $className): array |
|
86 | { |
||
87 | 3 | if ($this->has($className) === false) { |
|
88 | 2 | if (array_key_exists($className, $this->ambiguousMap) === true) { |
|
89 | 1 | throw new AmbiguousSettingsException($className); |
|
90 | } |
||
91 | 1 | throw new NotRegisteredSettingsException($className); |
|
92 | } |
||
93 | |||
94 | 1 | $index = $this->settingsMap[$className]; |
|
95 | 1 | $data = $this->settingsData[$index]; |
|
96 | |||
97 | 1 | return $data; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param SettingsInterface $settings |
||
102 | * |
||
103 | * @return InstanceSettingsProvider |
||
104 | */ |
||
105 | 9 | public function register(SettingsInterface $settings): InstanceSettingsProvider |
|
106 | { |
||
107 | 9 | $className = get_class($settings); |
|
108 | 9 | if (array_key_exists($className, $this->instances) === true) { |
|
109 | 1 | throw new AlreadyRegisteredSettingsException($className); |
|
110 | } |
||
111 | |||
112 | 9 | $this->instances[$className] = $settings; |
|
113 | 9 | $this->isProcessed = false; |
|
114 | |||
115 | 9 | return $this; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return array |
||
120 | */ |
||
121 | 9 | public function getSettingsMap(): array |
|
122 | { |
||
123 | 9 | $this->checkInstancesAreProcessed(); |
|
124 | |||
125 | 9 | return $this->settingsMap; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | 7 | public function getSettingsData(): array |
|
132 | { |
||
133 | 7 | $this->checkInstancesAreProcessed(); |
|
134 | |||
135 | 7 | return $this->settingsData; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return array |
||
140 | */ |
||
141 | 7 | public function getAmbiguousMap(): array |
|
142 | { |
||
143 | 7 | $this->checkInstancesAreProcessed(); |
|
144 | |||
145 | 7 | return $this->ambiguousMap; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 1 | public function isAmbiguous(string $className): bool |
|
152 | { |
||
153 | 1 | $result = array_key_exists($className, $this->getAmbiguousMap()); |
|
154 | |||
155 | 1 | return $result; |
|
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return array |
||
160 | */ |
||
161 | 8 | protected function getApplicationData(): array |
|
162 | { |
||
163 | 8 | return $this->applicationData; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return void |
||
168 | */ |
||
169 | 9 | private function checkInstancesAreProcessed(): void |
|
173 | |||
174 | /** |
||
175 | * @return void |
||
176 | * |
||
177 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
178 | */ |
||
179 | 8 | private function processInstances(): void |
|
180 | { |
||
181 | 8 | $preliminaryMap = []; |
|
182 | 8 | foreach ($this->instances as $instance) { |
|
228 | |||
229 | /** |
||
230 | * @param SettingsInterface[] $instanceList |
||
231 | * |
||
232 | * @return SettingsInterface|null |
||
233 | */ |
||
234 | 7 | private function selectChildSettings(array $instanceList): ?SettingsInterface |
|
250 | |||
251 | /** |
||
252 | * @param SettingsInterface $instance1 |
||
253 | * @param SettingsInterface $instance2 |
||
254 | * |
||
255 | * @return SettingsInterface|null |
||
256 | */ |
||
257 | 7 | private function selectChildSettingsAmongTwo( |
|
264 | } |
||
265 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.