Total Complexity | 42 |
Total Lines | 314 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like MetaData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MetaData, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class MetaData implements ArrayAccess |
||
22 | { |
||
23 | use TranslatorTrait; |
||
24 | |||
25 | public const TYPE_MODULE = 2; |
||
26 | |||
27 | public const TYPE_SYSTEM = 3; |
||
28 | |||
29 | public const TYPE_CORE = 4; |
||
30 | |||
31 | public const DEPENDENCY_REQUIRED = 1; |
||
32 | |||
33 | public const DEPENDENCY_RECOMMENDED = 2; |
||
34 | |||
35 | public const DEPENDENCY_CONFLICTS = 3; |
||
36 | |||
37 | private $name; |
||
38 | |||
39 | private $version; |
||
40 | |||
41 | private $description; |
||
42 | |||
43 | private $type; |
||
44 | |||
45 | private $dependencies; |
||
46 | |||
47 | private $shortName; |
||
48 | |||
49 | private $class; |
||
50 | |||
51 | private $namespace; |
||
52 | |||
53 | private $basePath; |
||
54 | |||
55 | private $rootPath; |
||
56 | |||
57 | private $autoload; |
||
58 | |||
59 | private $displayName; |
||
60 | |||
61 | private $url; |
||
62 | |||
63 | private $oldNames; |
||
64 | |||
65 | private $capabilities; |
||
66 | |||
67 | private $securitySchema; |
||
68 | |||
69 | private $extensionType; |
||
70 | |||
71 | private $coreCompatibility; |
||
72 | |||
73 | public function __construct(array $json = []) |
||
74 | { |
||
75 | $this->name = $json['name']; |
||
76 | $this->version = $json['version'] ?? ''; |
||
77 | $this->description = $json['description'] ?? ''; |
||
78 | $this->type = $json['type']; |
||
79 | $this->dependencies = $this->formatDependencies($json); |
||
80 | $this->shortName = $json['extra']['zikula']['short-name']; |
||
81 | $this->class = $json['extra']['zikula']['class']; |
||
82 | $this->namespace = mb_substr($this->class, 0, mb_strrpos($this->class, '\\') + 1); |
||
83 | $this->basePath = $json['extra']['zikula']['base-path']; |
||
84 | $this->rootPath = $json['extra']['zikula']['root-path']; |
||
85 | $this->autoload = $json['autoload']; |
||
86 | $this->displayName = $json['extra']['zikula']['displayname'] ?? ''; |
||
87 | $this->url = $json['extra']['zikula']['url'] ?? ''; |
||
88 | $this->oldNames = $json['extra']['zikula']['oldnames'] ?? []; |
||
89 | $this->capabilities = $json['extra']['zikula']['capabilities'] ?? []; |
||
90 | $this->securitySchema = $json['extra']['zikula']['securityschema'] ?? []; |
||
91 | $this->extensionType = $json['extensionType'] ?? self::TYPE_MODULE; |
||
92 | $this->coreCompatibility = $json['extra']['zikula']['core-compatibility'] ?? null; |
||
93 | } |
||
94 | |||
95 | public function getName(): string |
||
96 | { |
||
97 | return $this->name; |
||
98 | } |
||
99 | |||
100 | public function getVersion(): string |
||
101 | { |
||
102 | return $this->version; |
||
103 | } |
||
104 | |||
105 | public function getShortName(): string |
||
108 | } |
||
109 | |||
110 | public function getPsr0(): array |
||
111 | { |
||
112 | return $this->autoload['psr-0'] ?? []; |
||
113 | } |
||
114 | |||
115 | public function getPsr4(): array |
||
116 | { |
||
117 | return $this->autoload['psr-4'] ?? []; |
||
118 | } |
||
119 | |||
120 | public function getAutoload(): array |
||
121 | { |
||
122 | return $this->autoload; |
||
123 | } |
||
124 | |||
125 | public function getBasePath(): string |
||
126 | { |
||
127 | return $this->basePath; |
||
128 | } |
||
129 | |||
130 | public function getRootPath(): string |
||
131 | { |
||
132 | return $this->rootPath; |
||
133 | } |
||
134 | |||
135 | public function getClass(): string |
||
136 | { |
||
137 | return $this->class; |
||
138 | } |
||
139 | |||
140 | public function getNamespace(): string |
||
141 | { |
||
142 | return $this->namespace; |
||
143 | } |
||
144 | |||
145 | public function getType(): string |
||
146 | { |
||
147 | return $this->type; |
||
148 | } |
||
149 | |||
150 | public function getDescription(): string |
||
151 | { |
||
152 | $this->confirmTranslator(); |
||
153 | |||
154 | $description = $this->trans(/** @Ignore */$this->description); |
||
155 | |||
156 | return empty($description) ? $this->description : $description; |
||
157 | } |
||
158 | |||
159 | public function setDescription(string $description): void |
||
160 | { |
||
161 | $this->description = $description; |
||
162 | } |
||
163 | |||
164 | public function getDependencies(): array |
||
165 | { |
||
166 | return $this->dependencies; |
||
167 | } |
||
168 | |||
169 | public function getDisplayName(): string |
||
170 | { |
||
171 | $this->confirmTranslator(); |
||
172 | |||
173 | $display_name = $this->trans(/** @Ignore */$this->displayName); |
||
174 | |||
175 | return empty($display_name) ? $this->displayName : $display_name; |
||
176 | } |
||
177 | |||
178 | public function setDisplayName(string $name): void |
||
179 | { |
||
180 | $this->displayName = $name; |
||
181 | } |
||
182 | |||
183 | public function getUrl(bool $translated = true): string |
||
184 | { |
||
185 | if ($translated) { |
||
186 | $this->confirmTranslator(); |
||
187 | $url = $this->trans(/** @Ignore */$this->url); |
||
188 | |||
189 | return empty($url) ? $this->url : $url; |
||
190 | } |
||
191 | |||
192 | return $this->url; |
||
193 | } |
||
194 | |||
195 | public function setUrl(string $url): void |
||
196 | { |
||
197 | $this->url = $url; |
||
198 | } |
||
199 | |||
200 | public function getOldNames(): array |
||
201 | { |
||
202 | return $this->oldNames; |
||
203 | } |
||
204 | |||
205 | public function getCapabilities(): array |
||
206 | { |
||
207 | return $this->capabilities; |
||
208 | } |
||
209 | |||
210 | public function getSecuritySchema(): array |
||
211 | { |
||
212 | return $this->securitySchema; |
||
213 | } |
||
214 | |||
215 | public function getExtensionType(): int |
||
216 | { |
||
217 | return $this->extensionType; |
||
218 | } |
||
219 | |||
220 | public function getCoreCompatibility(): ?string |
||
221 | { |
||
222 | return $this->coreCompatibility; |
||
223 | } |
||
224 | |||
225 | private function formatDependencies(array $json = []): array |
||
226 | { |
||
227 | $dependencies = []; |
||
228 | if (!empty($json['require'])) { |
||
229 | foreach ($json['require'] as $package => $version) { |
||
230 | $dependencies[] = [ |
||
231 | 'modname' => $package, |
||
232 | 'minversion' => $version, |
||
233 | 'maxversion' => $version, |
||
234 | 'status' => self::DEPENDENCY_REQUIRED |
||
235 | ]; |
||
236 | } |
||
237 | } else { |
||
238 | $dependencies[] = [ |
||
239 | 'modname' => 'zikula/core', |
||
240 | 'minversion' => '>=1.4.1 <3.0.0', |
||
241 | 'maxversion' => '>=1.4.1 <3.0.0', |
||
242 | 'status' => self::DEPENDENCY_REQUIRED |
||
243 | ]; |
||
244 | } |
||
245 | if (!empty($json['suggest'])) { |
||
246 | foreach ($json['suggest'] as $package => $reason) { |
||
247 | if (mb_strpos($package, ':')) { |
||
248 | list($name, $version) = explode(':', $package, 2); |
||
249 | } else { |
||
250 | $name = $package; |
||
251 | $version = '-1'; |
||
252 | } |
||
253 | $dependencies[] = [ |
||
254 | 'modname' => $name, |
||
255 | 'minversion' => $version, |
||
256 | 'maxversion' => $version, |
||
257 | 'reason' => $reason, |
||
258 | 'status' => self::DEPENDENCY_RECOMMENDED |
||
259 | ]; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | return $dependencies; |
||
264 | } |
||
265 | |||
266 | private function confirmTranslator(): void |
||
270 | } |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Theme MetaData as array |
||
275 | */ |
||
276 | public function getThemeFilteredVersionInfoArray(): array |
||
277 | { |
||
278 | $capabilities = $this->getCapabilities(); |
||
279 | |||
280 | return [ |
||
281 | 'name' => $this->getShortName(), |
||
282 | 'type' => $this->getExtensionType(), |
||
283 | 'displayname' => $this->getDisplayName(), |
||
284 | 'description' => $this->getDescription(), |
||
285 | 'version' => $this->getVersion(), |
||
286 | // 'capabilities' => $this->getCapabilities(), |
||
287 | // It would be better to add capabilities to DB and move to inverse in legacy code and refactor later checks. refs #3644 |
||
288 | 'user' => $capabilities['user'] ?? true, |
||
289 | 'admin' => $capabilities['admin'] ?? true, |
||
290 | 'system' => $capabilities['system'] ?? false, |
||
291 | 'xhtml' => $capabilities['xhtml'] ?? true, // this is not truly valid in 2.0 |
||
292 | ]; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Module MetaData as array |
||
297 | */ |
||
298 | public function getFilteredVersionInfoArray(): array |
||
299 | { |
||
300 | return [ |
||
301 | 'name' => $this->getShortName(), |
||
302 | 'type' => $this->getExtensionType(), |
||
303 | 'displayname' => $this->getDisplayName(), |
||
304 | 'oldnames' => $this->getOldNames(), |
||
305 | 'description' => $this->getDescription(), |
||
306 | 'version' => $this->getVersion(), |
||
307 | 'url' => $this->getUrl(), |
||
308 | 'capabilities' => $this->getCapabilities(), |
||
309 | 'securityschema' => $this->getSecuritySchema(), |
||
310 | 'dependencies' => $this->getDependencies(), |
||
311 | 'coreCompatibility' => $this->getCoreCompatibility() |
||
312 | ]; |
||
313 | } |
||
314 | |||
315 | public function offsetExists($offset) |
||
316 | { |
||
317 | return isset($this->{$offset}); |
||
318 | } |
||
319 | |||
320 | public function offsetGet($offset) |
||
321 | { |
||
322 | $method = 'get' . ucwords($offset); |
||
323 | |||
324 | return $this->{$method}(); |
||
325 | } |
||
326 | |||
327 | public function offsetSet($offset, $value): void |
||
328 | { |
||
329 | throw new Exception('Setting values by array access is not allowed.'); |
||
330 | } |
||
331 | |||
332 | public function offsetUnset($offset): void |
||
335 | } |
||
336 | } |
||
337 |