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 | public function __construct($resource) |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 15 | * @param string $property the property to query |
||
| 16 | * @param boolean $default the default value if the value is not set in configuration |
||
| 17 | */ |
||
| 18 | private function getBoolean($property, $default = false) |
||
| 19 | { |
||
| 20 | $val = $this->resource->getLiteral($property); |
||
| 21 | if ($val) { |
||
| 22 | return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN); |
||
| 23 | } |
||
| 24 | |||
| 25 | return $default; |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Returns a boolean value based on a literal value from the vocabularies.ttl configuration. |
||
| 30 | * @param string $property the property to query |
||
| 31 | * @param string $lang preferred language for the literal, |
||
| 32 | */ |
||
| 33 | private function getLiteral($property, $lang=null) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get the default language of this vocabulary |
||
| 52 | * @return string default language, e.g. 'en' |
||
| 53 | */ |
||
| 54 | |||
| 55 | public function getDefaultLanguage() |
||
| 56 | { |
||
| 57 | $deflang = $this->resource->getLiteral('skosmos:defaultLanguage'); |
||
| 58 | if ($deflang) { |
||
| 59 | return $deflang->getValue(); |
||
| 60 | } |
||
| 61 | |||
| 62 | $langs = $this->getLanguages(); |
||
| 63 | $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric |
||
| 64 | if (sizeof($langs) > 1) { |
||
| 65 | trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING); |
||
| 66 | } |
||
| 67 | |||
| 68 | return $deflang; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Wether the alphabetical index is small enough to be shown all at once. |
||
| 73 | * @return boolean true if all concepts can be shown at once. |
||
| 74 | */ |
||
| 75 | public function getAlphabeticalFull() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Returns a short name for a vocabulary if configured. If that has not been set |
||
| 82 | * using vocabId as a fallback. |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | public function getShortName() |
||
| 86 | { |
||
| 87 | $shortname = $this->getLiteral('skosmos:shortName'); |
||
| 88 | if ($shortname) |
||
| 89 | return $shortname; |
||
| 90 | |||
| 91 | // if no shortname exists fall back to the id |
||
| 92 | return $this->getId(); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get the vocabulary feedback e-mail address and return it. |
||
| 97 | * |
||
| 98 | * @return string e-mail address or null if not defined. |
||
| 99 | */ |
||
| 100 | public function getFeedbackRecipient() |
||
| 101 | { |
||
| 102 | $email = $this->resource->get('skosmos:feedbackRecipient'); |
||
| 103 | return isset($email) ? $email->getValue() : null; |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Returns the human readable vocabulary title. |
||
| 108 | * @return string the title of the vocabulary |
||
| 109 | */ |
||
| 110 | public function getTitle($lang = null) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 117 | * @return boolean |
||
| 118 | */ |
||
| 119 | public function sortByNotation() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | public function showChangeList() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * get the URLs from which the vocabulary data can be downloaded |
||
| 135 | * @return array Array with MIME type as key, URL as value |
||
| 136 | */ |
||
| 137 | public function getDataURLs() |
||
| 138 | { |
||
| 139 | $ret = array(); |
||
| 140 | $urls = $this->resource->allResources("void:dataDump"); |
||
| 141 | foreach ($urls as $url) { |
||
| 142 | // first try dc:format and dc11:format |
||
| 143 | $mimetypelit = $url->getLiteral('dc:format'); |
||
| 144 | if ($mimetypelit === null) { |
||
| 145 | $mimetypelit = $url->getLiteral('dc11:format'); |
||
| 146 | } |
||
| 147 | |||
| 148 | // if still not found, guess MIME type using file extension |
||
| 149 | if ($mimetypelit !== null) { |
||
| 150 | $mimetype = $mimetypelit->getValue(); |
||
| 151 | } else { |
||
| 152 | $format = EasyRdf_Format::guessFormat(null, $url->getURI()); |
||
| 153 | if ($format === null) { |
||
| 154 | trigger_error("Could not guess format for <$url>.", E_USER_WARNING); |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | $mimetypes = array_keys($format->getMimeTypes()); |
||
| 158 | $mimetype = $mimetypes[0]; |
||
| 159 | } |
||
| 160 | $ret[$mimetype] = $url->getURI(); |
||
| 161 | } |
||
| 162 | return $ret; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Returns the class URI used for concept groups in this vocabulary, |
||
| 167 | * or null if not set. |
||
| 168 | * @return string group class URI or null |
||
| 169 | */ |
||
| 170 | |||
| 171 | public function getGroupClassURI() |
||
| 172 | { |
||
| 173 | $val = $this->resource->getResource("skosmos:groupClass"); |
||
| 174 | if ($val) { |
||
| 175 | return $val->getURI(); |
||
| 176 | } |
||
| 177 | |||
| 178 | return null; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the class URI used for thesaurus arrays in this vocabulary, |
||
| 183 | * or null if not set. |
||
| 184 | * @return string array class URI or null |
||
| 185 | */ |
||
| 186 | |||
| 187 | public function getArrayClassURI() |
||
| 188 | { |
||
| 189 | $val = $this->resource->getResource("skosmos:arrayClass"); |
||
| 190 | if ($val) { |
||
| 191 | return $val->getURI(); |
||
| 192 | } |
||
| 193 | |||
| 194 | return null; |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Returns custom properties displayed on the search page if configured. |
||
| 199 | * @return string array class URI or null |
||
| 200 | */ |
||
| 201 | |||
| 202 | public function getAdditionalSearchProperties() |
||
| 203 | { |
||
| 204 | $resources = $this->resource->allResources("skosmos:showPropertyInSearch"); |
||
| 205 | $ret = array(); |
||
| 206 | View Code Duplication | foreach ($resources as $res) { |
|
| 207 | $prop = $res->getURI(); |
||
| 208 | if (EasyRdf_Namespace::shorten($prop) !== null) // shortening property labels if possible |
||
| 209 | { |
||
| 210 | $prop = EasyRdf_Namespace::shorten($prop); |
||
| 211 | } |
||
| 212 | |||
| 213 | $ret[] = $prop; |
||
| 214 | } |
||
| 215 | return $ret; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Queries whether the property should be shown with all the label language variations. |
||
| 220 | * @param string $property |
||
| 221 | * @return boolean |
||
| 222 | */ |
||
| 223 | public function hasMultiLingualProperty($property) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | public function getShowHierarchy() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 252 | * @return boolean |
||
| 253 | */ |
||
| 254 | public function showConceptSchemesInHierarchy() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 261 | * @return boolean defaults to true if fetching hasn't been explicitly denied. |
||
| 262 | */ |
||
| 263 | public function getExternalResourcesLoading() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 270 | * @return boolean |
||
| 271 | */ |
||
| 272 | public function getShowLangCodes() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns a boolean value set in the vocabularies.ttl config. |
||
| 279 | * @return array array of concept class URIs (can be empty) |
||
| 280 | */ |
||
| 281 | public function getIndexClasses() |
||
| 282 | { |
||
| 283 | $resources = $this->resource->allResources("skosmos:indexShowClass"); |
||
| 284 | $ret = array(); |
||
| 285 | foreach ($resources as $res) { |
||
| 286 | $ret[] = $res->getURI(); |
||
| 287 | } |
||
| 288 | return $ret; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Get the languages supported by this vocabulary |
||
| 293 | * @return array languages supported by this vocabulary (as language tag strings) |
||
| 294 | */ |
||
| 295 | public function getLanguages() |
||
| 296 | { |
||
| 297 | $langs = $this->resource->allLiterals('skosmos:language'); |
||
| 298 | $ret = array(); |
||
| 299 | foreach ($langs as $lang) { |
||
| 300 | $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang()); |
||
| 301 | $ret[$langlit] = $lang->getValue(); |
||
| 302 | } |
||
| 303 | ksort($ret); |
||
| 304 | |||
| 305 | return $ret; |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the vocabulary default sidebar view. |
||
| 310 | * @return string name of the view |
||
| 311 | */ |
||
| 312 | public function getDefaultSidebarView() |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Extracts the vocabulary id string from the baseuri of the vocabulary. |
||
| 327 | * @return string identifier eg. 'mesh'. |
||
| 328 | */ |
||
| 329 | public function getId() |
||
| 330 | { |
||
| 331 | $uriparts = explode("#", $this->resource->getURI()); |
||
| 332 | if (count($uriparts) != 1) |
||
| 333 | // hash namespace |
||
| 334 | { |
||
| 343 | |||
| 344 | public function getShowStatistics() { |
||
| 347 | } |
||
| 348 |
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.