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 Model 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 Model, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Model |
||
| 22 | { |
||
| 23 | /** EasyRdf_Graph graph instance */ |
||
| 24 | private $graph; |
||
| 25 | /** cache for Vocabulary objects */ |
||
| 26 | private $all_vocabularies = null; |
||
| 27 | /** cache for Vocabulary objects */ |
||
| 28 | private $vocabs_by_graph = null; |
||
| 29 | /** cache for Vocabulary objects */ |
||
| 30 | private $vocabs_by_urispace = null; |
||
| 31 | /** how long to store retrieved URI information in APC cache */ |
||
| 32 | private $URI_FETCH_TTL = 86400; // 1 day |
||
| 33 | private $global_config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Initializes the Model object |
||
| 37 | */ |
||
| 38 | public function __construct($config) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Returns the GlobalConfig object given to the Model as a constructor parameter. |
||
| 46 | * @return GlobalConfig |
||
| 47 | */ |
||
| 48 | public function getConfig() { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initializes the configuration from the vocabularies.ttl file |
||
| 54 | */ |
||
| 55 | private function initializeVocabularies() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Return the version of this Skosmos installation, or "unknown" if |
||
| 82 | * it cannot be determined. The version information is based on Git tags. |
||
| 83 | * @return string version |
||
| 84 | */ |
||
| 85 | public function getVersion() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Return all the vocabularies available. |
||
| 101 | * @param boolean $categories whether you want everything included in a subarray of |
||
| 102 | * a category. |
||
| 103 | * @param boolean $shortname set to true if you want the vocabularies sorted by |
||
| 104 | * their shortnames instead of ther titles. |
||
| 105 | */ |
||
| 106 | public function getVocabularyList($categories = true, $shortname = false) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Return all types (RDFS/OWL classes) present in the specified vocabulary or all vocabularies. |
||
| 137 | * @return array Array with URIs (string) as key and array of (label, superclassURI) as value |
||
| 138 | */ |
||
| 139 | public function getTypes($vocid = null, $lang = null) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Return the languages present in the configured vocabularies. |
||
| 161 | * @return array Array with lang codes (string) |
||
| 162 | */ |
||
| 163 | public function getLanguages($lang) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * returns a concept's RDF data in downloadable format |
||
| 179 | * @param string $vocid vocabulary id, or null for global data (from all vocabularies) |
||
| 180 | * @param string $uri concept URI |
||
| 181 | * @param string $format the format in which you want to get the result, currently this function supports |
||
| 182 | * text/turtle, application/rdf+xml and application/json |
||
| 183 | * @return string RDF data in the requested serialization |
||
| 184 | */ |
||
| 185 | public function getRDF($vocid, $uri, $format) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Makes a SPARQL-query to the endpoint that retrieves concept |
||
| 216 | * references as it's search results. |
||
| 217 | * @param string $term the term that is looked for eg. 'cat'. |
||
| 218 | * @param mixed $vocids vocabulary id eg. 'yso', array of such ids for multi-vocabulary search, or null for global search. |
||
| 219 | * @param string $lang language code to show labels in, eg. 'fi' for Finnish. |
||
| 220 | * @param string $search_lang language code used for matching, eg. 'fi' for Finnish, null for any language |
||
| 221 | * @param string $type limit search to concepts of the given type |
||
| 222 | * @param string $parent limit search to concepts which have the given concept as parent in the transitive broader hierarchy |
||
| 223 | * @param string $group limit search to concepts which are in the given group |
||
| 224 | * @param int $offset optional parameter for search offset. |
||
| 225 | * @param int $limit optional paramater for maximum amount of results. |
||
| 226 | * @param boolean $hidden include matches on hidden labels (default: true). |
||
| 227 | * @param array $fields extra fields to include in the result (array of strings). (default: null = none) |
||
| 228 | * @return array search results |
||
| 229 | */ |
||
| 230 | public function searchConcepts($term, $vocids, $lang, $search_lang, $type = null, $parent = null, $group = null, $offset = 0, $limit = null, $hidden = true, $fields = null) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Function for performing a search for concepts and their data fields. |
||
| 303 | * @param string $term searchterm eg. 'cat' |
||
| 304 | * @param mixed $vocids vocabulary id eg. 'yso', array of such ids for multi-vocabulary search, or null for global search. |
||
| 305 | * @param string $lang language code of returned labels, eg. 'fi' |
||
| 306 | * @param string $search_lang language code used for matching, eg. 'fi', or null for anything |
||
| 307 | * @param integer $offset used for offsetting the result set eg. '20' |
||
| 308 | * @param integer $limit upper count for the search results eg. '10' |
||
| 309 | * @param string $type limit search to concepts of the given type |
||
| 310 | * @param string $parent limit search to concepts which have the given concept as parent in the transitive broader hierarchy |
||
| 311 | * @param string $group limit search to concepts which are in the given group |
||
| 312 | * @return array array with keys 'count' and 'results' |
||
| 313 | */ |
||
| 314 | public function searchConceptsAndInfo($term, $vocids, $lang, $search_lang, $offset = 0, $limit = 20, $type = null, $parent = null, $group = null) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Creates dataobjects from an input array. |
||
| 372 | * @param string $class the type of class eg. 'Vocabulary'. |
||
| 373 | * @param array $resarr contains the EasyRdf_Resources. |
||
| 374 | */ |
||
| 375 | private function createDataObjects($class, $resarr) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the cached vocabularies. |
||
| 387 | * @return array of Vocabulary dataobjects |
||
| 388 | */ |
||
| 389 | public function getVocabularies() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Returns the cached vocabularies from a category. |
||
| 414 | * @param EasyRdf_Resource $cat the category in question |
||
| 415 | * @return array of vocabulary dataobjects |
||
| 416 | */ |
||
| 417 | public function getVocabulariesInCategory($cat) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Creates dataobjects of all the different vocabulary categories (Health etc.). |
||
| 426 | * @return array of Dataobjects of the type VocabularyCategory. |
||
| 427 | */ |
||
| 428 | public function getVocabularyCategories() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Returns the label defined in vocabularies.ttl with the appropriate language. |
||
| 437 | * @param string $lang language code of returned labels, eg. 'fi' |
||
| 438 | * @return string the label for vocabulary categories. |
||
| 439 | */ |
||
| 440 | public function getClassificationLabel($lang) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Returns a single cached vocabulary. |
||
| 450 | * @param string $vocid the vocabulary id eg. 'mesh'. |
||
| 451 | * @return vocabulary dataobject |
||
| 452 | */ |
||
| 453 | public function getVocabulary($vocid) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Return the vocabulary that is stored in the given graph on the given endpoint. |
||
| 466 | * |
||
| 467 | * @param $graph string graph URI |
||
| 468 | * @param $endpoint string endpoint URL (default SPARQL endpoint if omitted) |
||
| 469 | * @return Vocabulary vocabulary of this URI, or null if not found |
||
| 470 | */ |
||
| 471 | public function getVocabularyByGraph($graph, $endpoint = null) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Guess which vocabulary a URI originates from, based on the declared |
||
| 495 | * vocabulary URI spaces. |
||
| 496 | * |
||
| 497 | * @param $uri string URI to search |
||
| 498 | * @return Vocabulary vocabulary of this URI, or null if not found |
||
| 499 | */ |
||
| 500 | public function guessVocabularyFromURI($uri) |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get the label for a resource, preferring 1. the given language 2. configured languages 3. any language. |
||
| 529 | * @param EasyRdf_Resource $res resource whose label to return |
||
| 530 | * @param string $lang preferred language |
||
| 531 | * @return EasyRdf_Literal label as an EasyRdf_Literal object, or null if not found |
||
| 532 | */ |
||
| 533 | public function getResourceLabel($res, $lang) |
||
| 545 | |||
| 546 | private function fetchResourceFromUri($uri) |
||
| 560 | |||
| 561 | public function getResourceFromUri($uri) |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Returns a SPARQL endpoint object. |
||
| 580 | * @param string $dialect eg. 'JenaText'. |
||
| 581 | * @param string $endpoint url address of endpoint |
||
| 582 | * @param string $graph uri for the target graph. |
||
| 583 | */ |
||
| 584 | public function getSparqlImplementation($dialect, $endpoint, $graph) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns a SPARQL endpoint object using the default implementation set in the config.inc. |
||
| 593 | */ |
||
| 594 | public function getDefaultSparql() |
||
| 598 | |||
| 599 | } |
||
| 600 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.