Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ElggPluginManifest 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ElggPluginManifest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class ElggPluginManifest { |
||
19 | |||
20 | /** |
||
21 | * The parser object |
||
22 | * |
||
23 | * @var \ElggPluginManifestParser18 |
||
24 | */ |
||
25 | protected $parser; |
||
26 | |||
27 | /** |
||
28 | * The root for plugin manifest namespaces. |
||
29 | * This is in the format http://www.elgg.org/plugin_manifest/<version> |
||
30 | */ |
||
31 | protected $namespace_root = 'http://www.elgg.org/plugin_manifest/'; |
||
32 | |||
33 | /** |
||
34 | * The expected structure of a plugins requires element |
||
35 | */ |
||
36 | private $depsStructPlugin = array( |
||
37 | 'type' => '', |
||
38 | 'name' => '', |
||
39 | 'version' => '', |
||
40 | 'comparison' => 'ge' |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * The expected structure of a priority element |
||
45 | */ |
||
46 | private $depsStructPriority = array( |
||
47 | 'type' => '', |
||
48 | 'priority' => '', |
||
49 | 'plugin' => '' |
||
50 | ); |
||
51 | |||
52 | /* |
||
53 | * The expected structure of elgg_version and elgg_release requires element |
||
54 | */ |
||
55 | private $depsStructElgg = array( |
||
56 | 'type' => '', |
||
57 | 'version' => '', |
||
58 | 'comparison' => 'ge' |
||
59 | ); |
||
60 | |||
61 | /** |
||
62 | * The expected structure of a requires php_version dependency element |
||
63 | */ |
||
64 | private $depsStructPhpVersion = array( |
||
65 | 'type' => '', |
||
66 | 'version' => '', |
||
67 | 'comparison' => 'ge' |
||
68 | ); |
||
69 | |||
70 | /** |
||
71 | * The expected structure of a requires php_ini dependency element |
||
72 | */ |
||
73 | private $depsStructPhpIni = array( |
||
74 | 'type' => '', |
||
75 | 'name' => '', |
||
76 | 'value' => '', |
||
77 | 'comparison' => '=' |
||
78 | ); |
||
79 | |||
80 | /** |
||
81 | * The expected structure of a requires php_extension dependency element |
||
82 | */ |
||
83 | private $depsStructPhpExtension = array( |
||
84 | 'type' => '', |
||
85 | 'name' => '', |
||
86 | 'version' => '', |
||
87 | 'comparison' => '=' |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * The expected structure of a conflicts depedency element |
||
92 | */ |
||
93 | private $depsConflictsStruct = array( |
||
94 | 'type' => '', |
||
95 | 'name' => '', |
||
96 | 'version' => '', |
||
97 | 'comparison' => '=' |
||
98 | ); |
||
99 | |||
100 | /** |
||
101 | * The expected structure of a provides dependency element. |
||
102 | */ |
||
103 | private $depsProvidesStruct = array( |
||
104 | 'type' => '', |
||
105 | 'name' => '', |
||
106 | 'version' => '' |
||
107 | ); |
||
108 | |||
109 | /** |
||
110 | * The expected structure of a screenshot element |
||
111 | */ |
||
112 | private $screenshotStruct = array( |
||
113 | 'description' => '', |
||
114 | 'path' => '' |
||
115 | ); |
||
116 | |||
117 | /** |
||
118 | * The expected structure of a contributor element |
||
119 | */ |
||
120 | private $contributorStruct = array( |
||
121 | 'name' => '', |
||
122 | 'email' => '', |
||
123 | 'website' => '', |
||
124 | 'username' => '', |
||
125 | 'description' => '', |
||
126 | ); |
||
127 | |||
128 | /** |
||
129 | * The API version of the manifest. |
||
130 | * |
||
131 | * @var int |
||
132 | */ |
||
133 | protected $apiVersion; |
||
134 | |||
135 | /** |
||
136 | * The optional plugin id this manifest belongs to. |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | protected $pluginID; |
||
141 | |||
142 | /** |
||
143 | * Load a manifest file, XmlElement or path to manifest.xml file |
||
144 | * |
||
145 | * @param mixed $manifest A string, XmlElement, or path of a manifest file. |
||
146 | * @param string $plugin_id Optional ID of the owning plugin. Used to |
||
147 | * fill in some values automatically. |
||
148 | * |
||
149 | * @throws PluginException |
||
150 | */ |
||
151 | public function __construct($manifest, $plugin_id = null) { |
||
211 | |||
212 | /** |
||
213 | * Returns the API version in use. |
||
214 | * |
||
215 | * @return int |
||
216 | */ |
||
217 | public function getApiVersion() { |
||
220 | |||
221 | /** |
||
222 | * Returns the plugin ID. |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getPluginID() { |
||
233 | |||
234 | /** |
||
235 | * Returns the manifest array. |
||
236 | * |
||
237 | * Used for backward compatibility. Specific |
||
238 | * methods should be called instead. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getManifest() { |
||
245 | |||
246 | /*************************************** |
||
247 | * Parsed and Normalized Manifest Data * |
||
248 | ***************************************/ |
||
249 | |||
250 | /** |
||
251 | * Returns the plugin name |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getName() { |
||
264 | |||
265 | /** |
||
266 | * Return the plugin ID required by the author. If getPluginID() does |
||
267 | * not match this, the plugin should not be started. |
||
268 | * |
||
269 | * @return string empty string if not empty/not defined |
||
270 | */ |
||
271 | public function getID() { |
||
274 | |||
275 | |||
276 | /** |
||
277 | * Return the description |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getDescription() { |
||
284 | |||
285 | /** |
||
286 | * Return the short description |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | public function getBlurb() { |
||
299 | |||
300 | /** |
||
301 | * Returns the license |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | public function getLicense() { |
||
314 | |||
315 | /** |
||
316 | * Returns the repository url |
||
317 | * |
||
318 | * @return string |
||
319 | */ |
||
320 | public function getRepositoryURL() { |
||
323 | |||
324 | /** |
||
325 | * Returns the bug tracker page |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | public function getBugTrackerURL() { |
||
332 | |||
333 | /** |
||
334 | * Returns the donations page |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getDonationsPageURL() { |
||
341 | |||
342 | /** |
||
343 | * Returns the version of the plugin. |
||
344 | * |
||
345 | * @return float |
||
346 | */ |
||
347 | public function getVersion() { |
||
350 | |||
351 | /** |
||
352 | * Returns the plugin author. |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getAuthor() { |
||
359 | |||
360 | /** |
||
361 | * Return the copyright |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | public function getCopyright() { |
||
368 | |||
369 | /** |
||
370 | * Return the website |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | public function getWebsite() { |
||
377 | |||
378 | /** |
||
379 | * Return the categories listed for this plugin |
||
380 | * |
||
381 | * @return array |
||
382 | */ |
||
383 | public function getCategories() { |
||
435 | |||
436 | /** |
||
437 | * Return the screenshots listed. |
||
438 | * |
||
439 | * @return array |
||
440 | */ |
||
441 | View Code Duplication | public function getScreenshots() { |
|
455 | |||
456 | /** |
||
457 | * Return the contributors listed. |
||
458 | * |
||
459 | * @return array |
||
460 | */ |
||
461 | View Code Duplication | public function getContributors() { |
|
475 | |||
476 | /** |
||
477 | * Return the list of provides by this plugin. |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | public function getProvides() { |
||
509 | |||
510 | /** |
||
511 | * Returns the dependencies listed. |
||
512 | * |
||
513 | * @return array |
||
514 | */ |
||
515 | public function getRequires() { |
||
545 | |||
546 | /** |
||
547 | * Returns the suggests elements. |
||
548 | * |
||
549 | * @return array |
||
550 | */ |
||
551 | public function getSuggests() { |
||
565 | |||
566 | /** |
||
567 | * Normalizes a dependency array using the defined structs. |
||
568 | * Can be used with either requires or suggests. |
||
569 | * |
||
570 | * @param array $dep A dependency array. |
||
571 | * @return array The normalized deps array. |
||
572 | */ |
||
573 | private function normalizeDep($dep) { |
||
660 | |||
661 | /** |
||
662 | * Returns the conflicts listed |
||
663 | * |
||
664 | * @return array |
||
665 | */ |
||
666 | public function getConflicts() { |
||
686 | |||
687 | /** |
||
688 | * Should this plugin be activated when Elgg is installed |
||
689 | * |
||
690 | * @return bool |
||
691 | */ |
||
692 | public function getActivateOnInstall() { |
||
709 | |||
710 | /** |
||
711 | * Normalizes an array into the structure specified |
||
712 | * |
||
713 | * @param array $struct The struct to normalize $element to. |
||
714 | * @param array $array The array |
||
715 | * |
||
716 | * @return array |
||
717 | */ |
||
718 | protected function buildStruct(array $struct, array $array) { |
||
727 | |||
728 | /** |
||
729 | * Returns a category's friendly name. This can be localized by |
||
730 | * defining the string 'admin:plugins:category:<category>'. If no |
||
731 | * localization is found, returns the category with _ and - converted to ' ' |
||
732 | * and then ucwords()'d. |
||
733 | * |
||
734 | * @param string $category The category as defined in the manifest. |
||
735 | * @return string A human-readable category |
||
736 | */ |
||
737 | static public function getFriendlyCategory($category) { |
||
746 | } |
||
747 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: