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 WebController 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 WebController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class WebController extends Controller |
||
|
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Provides access to the templating engine. |
||
| 21 | * @property object $twig the twig templating engine. |
||
| 22 | */ |
||
| 23 | public $twig; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor for the WebController. |
||
| 27 | * @param Model $model |
||
| 28 | */ |
||
| 29 | public function __construct($model) |
||
| 30 | { |
||
| 31 | parent::__construct($model); |
||
| 32 | |||
| 33 | // initialize Twig templates |
||
| 34 | $tmpDir = $model->getConfig()->getTemplateCache(); |
||
| 35 | |||
| 36 | // check if the cache pointed by config.inc exists, if not we create it. |
||
| 37 | if (!file_exists($tmpDir)) { |
||
| 38 | mkdir($tmpDir); |
||
| 39 | } |
||
| 40 | |||
| 41 | // specify where to look for templates and cache |
||
| 42 | $loader = new Twig_Loader_Filesystem('view'); |
||
| 43 | // initialize Twig environment |
||
| 44 | $this->twig = new Twig_Environment($loader, array('cache' => $tmpDir, |
||
| 45 | 'auto_reload' => true, 'debug' => true)); |
||
| 46 | $this->twig->addExtension(new Twig_Extensions_Extension_I18n()); |
||
| 47 | //ENABLES DUMP() method for easy and fun debugging! |
||
| 48 | $this->twig->addExtension(new Twig_Extension_Debug()); |
||
| 49 | // used for setting the base href for the relative urls |
||
| 50 | $this->twig->addGlobal("BaseHref", $this->getBaseHref()); |
||
| 51 | // setting the service name string from the config.inc |
||
| 52 | $this->twig->addGlobal("ServiceName", $this->model->getConfig()->getServiceName()); |
||
| 53 | // setting the service logo location from the config.inc |
||
| 54 | if ($this->model->getConfig()->getServiceLogo() !== null) { |
||
| 55 | $this->twig->addGlobal("ServiceLogo", $this->model->getConfig()->getServiceLogo()); |
||
| 56 | } |
||
| 57 | |||
| 58 | // setting the service custom css file from the config.inc |
||
| 59 | if ($this->model->getConfig()->getCustomCss() !== null) { |
||
| 60 | $this->twig->addGlobal("ServiceCustomCss", $this->model->getConfig()->getCustomCss()); |
||
| 61 | } |
||
| 62 | // used for displaying the ui language selection as a dropdown |
||
| 63 | if ($this->model->getConfig()->getUiLanguageDropdown() !== null) { |
||
| 64 | $this->twig->addGlobal("LanguageDropdown", $this->model->getConfig()->getUiLanguageDropdown()); |
||
| 65 | } |
||
| 66 | |||
| 67 | // setting the list of properties to be displayed in the search results |
||
| 68 | $this->twig->addGlobal("PreferredProperties", array('skos:prefLabel', 'skos:narrower', 'skos:broader', 'skosmos:memberOf', 'skos:altLabel', 'skos:related')); |
||
| 69 | |||
| 70 | // register a Twig filter for generating URLs for vocabulary resources (concepts and groups) |
||
| 71 | $controller = $this; // for use by anonymous function below |
||
| 72 | $urlFilter = new Twig_SimpleFilter('link_url', function ($uri, $vocab, $lang, $type = 'page', $clang = null, $term = null) use ($controller) { |
||
| 73 | // $vocab can either be null, a vocabulary id (string) or a Vocabulary object |
||
| 74 | if ($vocab === null) { |
||
| 75 | // target vocabulary is unknown, best bet is to link to the plain URI |
||
| 76 | return $uri; |
||
| 77 | } elseif (is_string($vocab)) { |
||
| 78 | $vocid = $vocab; |
||
| 79 | $vocab = $controller->model->getVocabulary($vocid); |
||
| 80 | } else { |
||
| 81 | $vocid = $vocab->getId(); |
||
| 82 | } |
||
| 83 | |||
| 84 | $params = array(); |
||
| 85 | if (isset($clang) && $clang !== $lang) { |
||
| 86 | $params['clang'] = $clang; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($term)) { |
||
| 90 | $params['q'] = $term; |
||
| 91 | } |
||
| 92 | |||
| 93 | // case 1: URI within vocabulary namespace: use only local name |
||
| 94 | $localname = $vocab->getLocalName($uri); |
||
| 95 | if ($localname !== $uri && $localname === urlencode($localname)) { |
||
| 96 | // check that the prefix stripping worked, and there are no problematic chars in localname |
||
| 97 | $paramstr = sizeof($params) > 0 ? '?' . http_build_query($params) : ''; |
||
| 98 | if ($type && $type !== '' && $type !== 'vocab' && !($localname === '' && $type === 'page')) { |
||
| 99 | return "$vocid/$lang/$type/$localname" . $paramstr; |
||
| 100 | } |
||
| 101 | |||
| 102 | return "$vocid/$lang/$localname" . $paramstr; |
||
| 103 | } |
||
| 104 | |||
| 105 | // case 2: URI outside vocabulary namespace, or has problematic chars |
||
| 106 | // pass the full URI as parameter instead |
||
| 107 | $params['uri'] = $uri; |
||
| 108 | return "$vocid/$lang/$type/?" . http_build_query($params); |
||
| 109 | }); |
||
| 110 | $this->twig->addFilter($urlFilter); |
||
| 111 | |||
| 112 | // register a Twig filter for generating strings from language codes with CLDR |
||
| 113 | $langFilter = new Twig_SimpleFilter('lang_name', function ($langcode, $lang) { |
||
| 114 | return Language::getName($langcode, $lang); |
||
| 115 | }); |
||
| 116 | $this->twig->addFilter($langFilter); |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Guess the language of the user. Return a language string that is one |
||
| 122 | * of the supported languages defined in the $LANGUAGES setting, e.g. "fi". |
||
| 123 | * @param string $vocid identifier for the vocabulary eg. 'yso'. |
||
| 124 | * @return string returns the language choice as a numeric string value |
||
| 125 | */ |
||
| 126 | public function guessLanguage($vocid = null) |
||
| 127 | { |
||
| 128 | // 1. select language based on SKOSMOS_LANGUAGE cookie |
||
| 129 | if (filter_input(INPUT_COOKIE, 'SKOSMOS_LANGUAGE', FILTER_SANITIZE_STRING)) { |
||
| 130 | return filter_input(INPUT_COOKIE, 'SKOSMOS_LANGUAGE', FILTER_SANITIZE_STRING); |
||
| 131 | } |
||
| 132 | |||
| 133 | // 2. if vocabulary given, select based on the default language of the vocabulary |
||
| 134 | if ($vocid !== null && $vocid !== '') { |
||
| 135 | try { |
||
| 136 | $vocab = $this->model->getVocabulary($vocid); |
||
| 137 | return $vocab->getConfig()->getDefaultLanguage(); |
||
| 138 | } catch (Exception $e) { |
||
| 139 | // vocabulary id not found, move on to the next selection method |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | // 3. select language based on Accept-Language header |
||
| 144 | header('Vary: Accept-Language'); // inform caches that a decision was made based on Accept header |
||
| 145 | $this->negotiator = new \Negotiation\LanguageNegotiator(); |
||
| 146 | $langcodes = array_keys($this->languages); |
||
| 147 | $acceptLanguage = filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING) ? filter_input(INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE', FILTER_SANITIZE_STRING) : ''; |
||
| 148 | $bestLang = $this->negotiator->getBest($acceptLanguage, $langcodes); |
||
| 149 | if (isset($bestLang) && in_array($bestLang, $langcodes)) { |
||
| 150 | return $bestLang->getValue(); |
||
| 151 | } |
||
| 152 | |||
| 153 | // show default site or prompt for language |
||
| 154 | return $langcodes[0]; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Loads and renders the view containing all the vocabularies. |
||
| 159 | * @param Request $request |
||
| 160 | */ |
||
| 161 | public function invokeVocabularies($request) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Invokes the concept page of a single concept in a specific vocabulary. |
||
| 185 | */ |
||
| 186 | public function invokeVocabularyConcept($request) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Invokes the feedback page with information of the users current vocabulary. |
||
| 216 | */ |
||
| 217 | public function invokeFeedbackForm($request) |
||
| 218 | { |
||
| 219 | $template = $this->twig->loadTemplate('feedback.twig'); |
||
| 220 | $this->setLanguageProperties($request->getLang()); |
||
| 221 | $vocabList = $this->model->getVocabularyList(false); |
||
| 222 | $vocab = $request->getVocab(); |
||
| 223 | |||
| 224 | $feedbackSent = false; |
||
| 225 | $feedbackMsg = null; |
||
| 226 | if ($request->getQueryParamPOST('message')) { |
||
| 227 | $feedbackSent = true; |
||
| 228 | $feedbackMsg = $request->getQueryParamPOST('message'); |
||
| 229 | } |
||
| 230 | $feedbackName = $request->getQueryParamPOST('name'); |
||
| 231 | $feedbackEmail = $request->getQueryParamPOST('email'); |
||
| 232 | $feedbackVocab = $request->getQueryParamPOST('vocab'); |
||
| 233 | $feedbackVocabEmail = ($vocab !== null) ? $vocab->getConfig()->getFeedbackRecipient() : null; |
||
| 234 | |||
| 235 | // if the hidden field has been set a value we have found a spam bot |
||
| 236 | // and we do not actually send the message. |
||
| 237 | if ($feedbackSent && $request->getQueryParamPOST('trap') === '') { |
||
| 238 | $this->sendFeedback($request, $feedbackMsg, $feedbackName, $feedbackEmail, $feedbackVocab, $feedbackVocabEmail); |
||
| 239 | } |
||
| 240 | |||
| 241 | echo $template->render( |
||
| 242 | array( |
||
| 243 | 'languages' => $this->languages, |
||
| 244 | 'vocab' => $vocab, |
||
| 245 | 'vocabList' => $vocabList, |
||
| 246 | 'feedback_sent' => $feedbackSent, |
||
| 247 | 'request' => $request, |
||
| 248 | )); |
||
| 249 | } |
||
| 250 | |||
| 251 | private function createFeedbackHeaders($fromName, $fromEmail, $toMail) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Sends the user entered message through the php's mailer. |
||
| 265 | * @param string $message only required parameter is the actual message. |
||
| 266 | * @param string $fromName senders own name. |
||
| 267 | * @param string $fromEmail senders email adress. |
||
| 268 | * @param string $fromVocab which vocabulary is the feedback related to. |
||
| 269 | */ |
||
| 270 | public function sendFeedback($request, $message, $fromName = null, $fromEmail = null, $fromVocab = null, $toMail = null) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Invokes the about page for the Skosmos service. |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function invokeAboutPage($request) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Invokes the search for concepts in all the availible ontologies. |
||
| 328 | */ |
||
| 329 | public function invokeGlobalSearch($request) |
||
| 330 | { |
||
| 331 | $lang = $request->getLang(); |
||
| 332 | $template = $this->twig->loadTemplate('vocab-search-listing.twig'); |
||
| 333 | $this->setLanguageProperties($lang); |
||
| 334 | |||
| 335 | $parameters = new ConceptSearchParameters($request, $this->model->getConfig()); |
||
| 336 | |||
| 337 | $vocabs = $request->getQueryParam('vocabs'); # optional |
||
| 338 | // convert to vocids array to support multi-vocabulary search |
||
| 339 | $vocids = ($vocabs !== null && $vocabs !== '') ? explode(' ', $vocabs) : null; |
||
| 340 | $vocabObjects = array(); |
||
| 341 | if ($vocids) { |
||
| 342 | foreach($vocids as $vocid) { |
||
| 343 | $vocabObjects[] = $this->model->getVocabulary($vocid); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | $parameters->setVocabularies($vocabObjects); |
||
| 347 | |||
| 348 | try { |
||
| 349 | $countAndResults = $this->model->searchConceptsAndInfo($parameters); |
||
| 350 | } catch (Exception $e) { |
||
| 351 | header("HTTP/1.0 404 Not Found"); |
||
| 352 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
| 353 | error_log('Caught exception: ' . $e->getMessage()); |
||
| 354 | } |
||
| 355 | $this->invokeGenericErrorPage($request, $e->getMessage()); |
||
| 356 | return; |
||
| 357 | } |
||
| 358 | $counts = $countAndResults['count']; |
||
| 359 | $searchResults = $countAndResults['results']; |
||
| 360 | $vocabList = $this->model->getVocabularyList(); |
||
| 361 | $sortedVocabs = $this->model->getVocabularyList(false, true); |
||
| 362 | $langList = $this->model->getLanguages($lang); |
||
| 363 | |||
| 364 | echo $template->render( |
||
| 365 | array( |
||
| 366 | 'search_count' => $counts, |
||
| 367 | 'languages' => $this->languages, |
||
| 368 | 'search_results' => $searchResults, |
||
| 369 | 'rest' => $parameters->getOffset()>0, |
||
| 370 | 'global_search' => true, |
||
| 371 | 'term' => $request->getQueryParam('q'), |
||
| 372 | 'lang_list' => $langList, |
||
| 373 | 'vocabs' => str_replace(' ', '+', $vocabs), |
||
| 374 | 'vocab_list' => $vocabList, |
||
| 375 | 'sorted_vocabs' => $sortedVocabs, |
||
| 376 | 'request' => $request, |
||
| 377 | 'parameters' => $parameters |
||
| 378 | )); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Invokes the search for a single vocabs concepts. |
||
| 383 | */ |
||
| 384 | public function invokeVocabularySearch($request) |
||
| 385 | { |
||
| 386 | $template = $this->twig->loadTemplate('vocab-search-listing.twig'); |
||
| 387 | $this->setLanguageProperties($request->getLang()); |
||
| 388 | $vocab = $request->getVocab(); |
||
| 389 | try { |
||
| 390 | $vocabTypes = $this->model->getTypes($request->getVocabid(), $request->getLang()); |
||
| 391 | } catch (Exception $e) { |
||
| 392 | header("HTTP/1.0 404 Not Found"); |
||
| 393 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
| 394 | error_log('Caught exception: ' . $e->getMessage()); |
||
| 395 | } |
||
| 396 | |||
| 397 | echo $template->render( |
||
| 398 | array( |
||
| 399 | 'languages' => $this->languages, |
||
| 400 | )); |
||
| 401 | |||
| 402 | return; |
||
| 403 | } |
||
| 404 | |||
| 405 | $langcodes = $vocab->getConfig()->getShowLangCodes(); |
||
| 406 | $parameters = new ConceptSearchParameters($request, $this->model->getConfig()); |
||
| 407 | |||
| 408 | try { |
||
| 409 | $countAndResults = $this->model->searchConceptsAndInfo($parameters); |
||
| 410 | $counts = $countAndResults['count']; |
||
| 411 | $searchResults = $countAndResults['results']; |
||
| 412 | } catch (Exception $e) { |
||
| 413 | header("HTTP/1.0 404 Not Found"); |
||
| 414 | if ($this->model->getConfig()->getLogCaughtExceptions()) { |
||
| 415 | error_log('Caught exception: ' . $e->getMessage()); |
||
| 416 | } |
||
| 417 | |||
| 418 | echo $template->render( |
||
| 419 | array( |
||
| 420 | 'languages' => $this->languages, |
||
| 421 | 'vocab' => $vocab, |
||
| 422 | 'term' => $request->getQueryParam('q'), |
||
| 423 | )); |
||
| 424 | return; |
||
| 425 | } |
||
| 426 | echo $template->render( |
||
| 427 | array( |
||
| 428 | 'languages' => $this->languages, |
||
| 429 | 'vocab' => $vocab, |
||
| 430 | 'search_results' => $searchResults, |
||
| 431 | 'search_count' => $counts, |
||
| 432 | 'rest' => $parameters->getOffset()>0, |
||
| 433 | 'limit_parent' => $parameters->getParentLimit(), |
||
| 434 | 'limit_type' => $request->getQueryParam('type') ? explode('+', $request->getQueryParam('type')) : null, |
||
| 435 | 'limit_group' => $parameters->getGroupLimit(), |
||
| 436 | 'limit_scheme' => $request->getQueryParam('scheme') ? explode('+', $request->getQueryParam('scheme')) : null, |
||
| 437 | 'group_index' => $vocab->listConceptGroups($request->getContentLang()), |
||
| 438 | 'parameters' => $parameters, |
||
| 439 | 'term' => $request->getQueryParam('q'), |
||
| 440 | 'types' => $vocabTypes, |
||
| 441 | 'explicit_langcodes' => $langcodes, |
||
| 442 | 'request' => $request, |
||
| 443 | )); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Invokes the alphabetical listing for a specific vocabulary. |
||
| 448 | */ |
||
| 449 | public function invokeAlphabeticalIndex($request) |
||
| 450 | { |
||
| 451 | $lang = $request->getLang(); |
||
| 452 | $this->setLanguageProperties($lang); |
||
| 453 | $template = $this->twig->loadTemplate('alphabetical-index.twig'); |
||
| 454 | $vocab = $request->getVocab(); |
||
| 455 | |||
| 456 | $offset = ($request->getQueryParam('offset') && is_numeric($request->getQueryParam('offset')) && $request->getQueryParam('offset') >= 0) ? $request->getQueryParam('offset') : 0; |
||
| 457 | if ($request->getQueryParam('limit')) { |
||
| 458 | $count = $request->getQueryParam('limit'); |
||
| 459 | } else { |
||
| 460 | $count = ($offset > 0) ? null : 250; |
||
| 461 | } |
||
| 462 | |||
| 463 | $contentLang = $request->getContentLang(); |
||
| 464 | |||
| 465 | $allAtOnce = $vocab->getConfig()->getAlphabeticalFull(); |
||
| 466 | if (!$allAtOnce) { |
||
| 467 | $searchResults = $vocab->searchConceptsAlphabetical($request->getLetter(), $count, $offset, $contentLang); |
||
| 468 | $letters = $vocab->getAlphabet($contentLang); |
||
| 469 | } else { |
||
| 470 | $searchResults = $vocab->searchConceptsAlphabetical('*', null, null, $contentLang); |
||
| 471 | $letters = null; |
||
| 472 | } |
||
| 473 | |||
| 474 | $request->setContentLang($contentLang); |
||
| 475 | |||
| 476 | echo $template->render( |
||
| 477 | array( |
||
| 478 | 'languages' => $this->languages, |
||
| 479 | 'vocab' => $vocab, |
||
| 480 | 'alpha_results' => $searchResults, |
||
| 481 | 'letters' => $letters, |
||
| 482 | 'all_letters' => $allAtOnce, |
||
| 483 | 'request' => $request, |
||
| 484 | )); |
||
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Invokes the vocabulary group index page template. |
||
| 489 | * @param boolean $stats set to true to get vocabulary statistics visible. |
||
| 490 | */ |
||
| 491 | View Code Duplication | public function invokeGroupIndex($request, $stats = false) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Loads and renders the view containing a specific vocabulary. |
||
| 509 | */ |
||
| 510 | public function invokeVocabularyHome($request) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Invokes a very generic errorpage. |
||
| 538 | */ |
||
| 539 | public function invokeGenericErrorPage($request, $message = null) |
||
| 552 | |||
| 553 | /** |
||
| 554 | * Loads and renders the view containing a list of recent changes in the vocabulary. |
||
| 555 | * @param Request $request |
||
| 556 | */ |
||
| 557 | public function invokeChangeList($request, $prop='dc:created') |
||
| 576 | |||
| 577 | } |
||
| 578 |
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.