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 VocabularyConfig 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 VocabularyConfig, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class VocabularyConfig extends DataObject |
||
|
|||
7 | { |
||
8 | private $plugins; |
||
9 | |||
10 | public function __construct($resource, $globalPlugins=array()) |
||
11 | { |
||
12 | $this->resource = $resource; |
||
13 | $plugins = $this->resource->allLiterals('skosmos:usePlugin'); |
||
14 | $pluginArray = array(); |
||
15 | if ($plugins) { |
||
16 | foreach ($plugins as $pluginlit) { |
||
17 | $pluginArray[] = $pluginlit->getValue(); |
||
18 | } |
||
19 | } |
||
20 | $this->plugins = new PluginRegister(array_merge($globalPlugins, $pluginArray)); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
25 | * @param string $property the property to query |
||
26 | * @param boolean $default the default value if the value is not set in configuration |
||
27 | */ |
||
28 | private function getBoolean($property, $default = false) |
||
37 | |||
38 | /** |
||
39 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
40 | * @param string $property the property to query |
||
41 | * @param string $lang preferred language for the literal, |
||
42 | */ |
||
43 | private function getLiteral($property, $lang=null) |
||
59 | |||
60 | /** |
||
61 | * Get the default language of this vocabulary |
||
62 | * @return string default language, e.g. 'en' |
||
63 | */ |
||
64 | |||
65 | public function getDefaultLanguage() |
||
80 | |||
81 | /** |
||
82 | * Wether the alphabetical index is small enough to be shown all at once. |
||
83 | * @return boolean true if all concepts can be shown at once. |
||
84 | */ |
||
85 | public function getAlphabeticalFull() |
||
89 | |||
90 | /** |
||
91 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
92 | * using vocabId as a fallback. |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getShortName() |
||
104 | |||
105 | /** |
||
106 | * Get the vocabulary feedback e-mail address and return it. |
||
107 | * |
||
108 | * @return string e-mail address or null if not defined. |
||
109 | */ |
||
110 | public function getFeedbackRecipient() |
||
115 | |||
116 | /** |
||
117 | * Returns the human readable vocabulary title. |
||
118 | * @return string the title of the vocabulary |
||
119 | */ |
||
120 | public function getTitle($lang = null) |
||
124 | |||
125 | /** |
||
126 | * Returns a boolean value set in the vocabularies.ttl config. |
||
127 | * @return boolean |
||
128 | */ |
||
129 | public function sortByNotation() |
||
133 | |||
134 | /** |
||
135 | * Returns a boolean value set in the vocabularies.ttl config. |
||
136 | * @return boolean |
||
137 | */ |
||
138 | public function showChangeList() |
||
142 | |||
143 | /** |
||
144 | * get the URLs from which the vocabulary data can be downloaded |
||
145 | * @return array Array with MIME type as key, URL as value |
||
146 | */ |
||
147 | public function getDataURLs() |
||
174 | |||
175 | /** |
||
176 | * Returns the main Concept Scheme URI of that Vocabulary, |
||
177 | * or null if not set. |
||
178 | * @return string concept scheme URI or null |
||
179 | */ |
||
180 | |||
181 | public function getMainConceptSchemeURI() |
||
190 | |||
191 | |||
192 | |||
193 | /** |
||
194 | * Returns the class URI used for concept groups in this vocabulary, |
||
195 | * or null if not set. |
||
196 | * @return string group class URI or null |
||
197 | */ |
||
198 | |||
199 | public function getGroupClassURI() |
||
208 | |||
209 | /** |
||
210 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
211 | * or null if not set. |
||
212 | * @return string array class URI or null |
||
213 | */ |
||
214 | |||
215 | public function getArrayClassURI() |
||
224 | |||
225 | /** |
||
226 | * Returns custom properties displayed on the search page if configured. |
||
227 | * @return string array class URI or null |
||
228 | */ |
||
229 | |||
230 | View Code Duplication | public function getAdditionalSearchProperties() |
|
245 | |||
246 | /** |
||
247 | * Queries whether the property should be shown with all the label language variations. |
||
248 | * @param string $property |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function hasMultiLingualProperty($property) |
||
268 | |||
269 | /** |
||
270 | * Returns a boolean value set in the vocabularies.ttl config. |
||
271 | * @return boolean |
||
272 | */ |
||
273 | public function getShowHierarchy() |
||
277 | |||
278 | /** |
||
279 | * Returns a boolean value set in the vocabularies.ttl config. |
||
280 | * @return boolean |
||
281 | */ |
||
282 | public function showConceptSchemesInHierarchy() |
||
286 | |||
287 | /** |
||
288 | * Returns a boolean value set in the vocabularies.ttl config. |
||
289 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
290 | */ |
||
291 | public function getExternalResourcesLoading() |
||
295 | |||
296 | /** |
||
297 | * Returns a boolean value set in the vocabularies.ttl config. |
||
298 | * @return boolean |
||
299 | */ |
||
300 | public function getShowLangCodes() |
||
304 | |||
305 | /** |
||
306 | * Returns a boolean value set in the vocabularies.ttl config. |
||
307 | * @return array array of concept class URIs (can be empty) |
||
308 | */ |
||
309 | public function getIndexClasses() |
||
318 | |||
319 | /** |
||
320 | * Get the languages supported by this vocabulary |
||
321 | * @return array languages supported by this vocabulary (as language tag strings) |
||
322 | */ |
||
323 | public function getLanguages() |
||
335 | |||
336 | /** |
||
337 | * Returns the vocabulary default sidebar view. |
||
338 | * @return string name of the view |
||
339 | */ |
||
340 | public function getDefaultSidebarView() |
||
359 | |||
360 | /** |
||
361 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
362 | * @return string identifier eg. 'mesh'. |
||
363 | */ |
||
364 | public function getId() |
||
378 | |||
379 | public function getShowStatistics() { |
||
382 | |||
383 | public function getPlugins() |
||
387 | |||
388 | /** |
||
389 | * Returns the property/properties used for visualizing concept hierarchies. |
||
390 | * @return string array class URI or null |
||
391 | */ |
||
392 | |||
393 | View Code Duplication | public function getHierarchyProperty() |
|
408 | |||
409 | /** |
||
410 | * Returns a boolean value set in the vocabularies.ttl config. |
||
411 | * @return boolean |
||
412 | */ |
||
413 | public function showNotation() |
||
417 | |||
418 | /** |
||
419 | * Returns a boolean value set in the vocabularies.ttl config. |
||
420 | * @return boolean |
||
421 | */ |
||
422 | public function showAlphabeticalIndex() |
||
426 | |||
427 | /** |
||
428 | * Returns a boolean value set in the vocabularies.ttl config. |
||
429 | * @return boolean |
||
430 | */ |
||
431 | public function getShowDeprecated() |
||
435 | |||
436 | /** |
||
437 | * Returns the vocabulary dc:type value(s) with their labels and uris, if set in the vocabulary configuration. |
||
438 | * @return array of objects or an empty array |
||
439 | */ |
||
440 | public function getTypes($lang = null) |
||
451 | |||
452 | /** |
||
453 | * Returns an array of fallback languages that is ordered by priority and |
||
454 | * defined in the vocabulary configuration as a collection. |
||
455 | * Additionally, the chosen content language is inserted with the highest priority |
||
456 | * and the vocab default language is inserted with the lowest priority. |
||
457 | * @param string $clang |
||
458 | * @return array of language code strings |
||
459 | */ |
||
460 | public function getLanguageOrder($clang) |
||
474 | } |
||
475 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.