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 AbstractController 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 AbstractController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | abstract class AbstractController |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var object The rendering engine / view object |
||
| 42 | */ |
||
| 43 | public $view = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string Name of the rendering engine |
||
| 47 | */ |
||
| 48 | public $renderEngineName = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string The name of the template to render |
||
| 52 | */ |
||
| 53 | public $template = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Koch\Http\HttpResponse |
||
| 57 | */ |
||
| 58 | public $response = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \Koch\Http\HttpRequest |
||
| 62 | */ |
||
| 63 | public $request = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var \Doctrine\ORM\EntityManager |
||
| 67 | */ |
||
| 68 | public $entityManager = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array The Module Configuration Array |
||
| 72 | */ |
||
| 73 | public static $moduleconfig = null; |
||
| 74 | |||
| 75 | public function __construct(HttpRequestInterface $request, HttpResponseInterface $response) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Returns the Doctrine Entity Manager. |
||
| 83 | * |
||
| 84 | * @return \Doctrine\ORM\EntityManager |
||
| 85 | */ |
||
| 86 | public function getEntityManager() |
||
| 90 | |||
| 91 | public function setEntityManager($em) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The name of the entity extracted from the classname. |
||
| 98 | * |
||
| 99 | * @param string Classname |
||
| 100 | * |
||
| 101 | * @return string The name of the entity extracted from classname |
||
| 102 | */ |
||
| 103 | public function getEntityNameFromClassname() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Proxy/Convenience Getter Method for the Repository of the current Module. |
||
| 119 | * |
||
| 120 | * |
||
| 121 | * @param string $entityName Name of an Entity, like "\Entity\User". |
||
| 122 | * |
||
| 123 | * @return \Doctrine\ORM\EntityRepository |
||
| 124 | */ |
||
| 125 | public function getModel($entityName = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Saves this and all others models (calls persist + flush) |
||
| 136 | * Save (save one) |
||
| 137 | * Flush (save all). |
||
| 138 | * |
||
| 139 | * @param \Doctrine\ORM\Mapping\Entity $model Entity. |
||
| 140 | * @param bool $flush Uses flush on true, save on false. Defaults to flush (true). |
||
| 141 | */ |
||
| 142 | public function saveModel(\Doctrine\ORM\Mapping\Entity $model, $flush = true) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Initializes the model (active records/entities/repositories) of the module. |
||
| 155 | * |
||
| 156 | * @param $modulename Modulname |
||
| 157 | * @param $recordname Recordname |
||
| 158 | */ |
||
| 159 | public static function setModel($modulename = null, $entity = null) |
||
| 160 | { |
||
| 161 | $module_models_path = ''; |
||
| 162 | |||
| 163 | /* |
||
| 164 | * Load the Records for the current module, if no modulename is specified. |
||
| 165 | * This is for lazy usage in the modulecontroller: $this->initModel(); |
||
| 166 | */ |
||
| 167 | if ($modulename === null) { |
||
| 168 | $modulename = HttpRequest::getRoute()->getModuleName(); |
||
| 169 | } |
||
| 170 | |||
| 171 | $module_models_path = APPLICATION_MODULES_PATH . mb_strtolower($modulename) . '/model/'; |
||
| 172 | |||
| 173 | // check if the module has a models dir |
||
| 174 | if (is_dir($module_models_path)) { |
||
| 175 | if ($entity !== null) { |
||
| 176 | // use second parameter of method |
||
| 177 | $entity = $module_models_path . 'Entities/' . ucfirst($entity) . '.php'; |
||
| 178 | } else { |
||
| 179 | // build entity filename by modulename |
||
| 180 | $entity = $module_models_path . 'Entities/' . ucfirst($modulename) . '.php'; |
||
| 181 | } |
||
| 182 | |||
| 183 | View Code Duplication | if (is_file($entity) && class_exists('Entity\\' . ucfirst($modulename), false)) { |
|
| 184 | include $entity; |
||
| 185 | } |
||
| 186 | |||
| 187 | $repos = $module_models_path . 'Repositories/' . ucfirst($modulename) . 'Repository.php'; |
||
| 188 | |||
| 189 | View Code Duplication | if (is_file($repos) && class_exists('Entity\\' . ucfirst($modulename), false)) { |
|
| 190 | include $repos; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | // else Module has no Model Data |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Gets a Module Config. |
||
| 198 | * |
||
| 199 | * @param string $modulename Modulename. |
||
| 200 | * |
||
| 201 | * @return array configuration array of module |
||
| 202 | */ |
||
| 203 | public static function getModuleConfig($modulename = null) |
||
| 204 | { |
||
| 205 | $config = self::getInjector()->instantiate('\Koch\Config\Config'); |
||
| 206 | |||
| 207 | return self::$moduleconfig = $config->readModuleConfig($modulename); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Gets a Config Value or sets a default value. |
||
| 212 | * |
||
| 213 | * @example |
||
| 214 | * Usage for one default variable: |
||
| 215 | * self::getConfigValue('items_newswidget', '8'); |
||
| 216 | * Gets the value for the key items_newswidget from the moduleconfig or sets the value to 8. |
||
| 217 | * |
||
| 218 | * Usage for two default variables: |
||
| 219 | * self::getConfigValue('items_newswidget', $_GET['numberNews'], '8'); |
||
| 220 | * Gets the value for the key items_newswidget from the moduleconfig or sets the value |
||
| 221 | * incomming via GET, if nothing is incomming, sets the default value of 8. |
||
| 222 | * |
||
| 223 | * @param string $keyname The keyname to find in the array. |
||
| 224 | * @param mixed $default_one A default value returned, when keyname was not found. |
||
| 225 | * @param mixed $default_two A default value returned, when keyname was not found and default_one is null. |
||
| 226 | * |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | public static function getConfigValue($keyname, $default_one = null, $default_two = null) |
||
| 230 | { |
||
| 231 | // if we don't have a moduleconfig array yet, get it |
||
| 232 | if (self::$moduleconfig === null) { |
||
| 233 | self::$moduleconfig = self::getModuleConfig(); |
||
| 234 | } |
||
| 235 | |||
| 236 | // try a lookup of the value by keyname |
||
| 237 | $value = \Koch\Functions\Functions::array_find_element_by_key($keyname, self::$moduleconfig); |
||
| 238 | |||
| 239 | // return value or default |
||
| 240 | View Code Duplication | if (empty($value) === false) { |
|
| 241 | return $value; |
||
| 242 | } elseif ($default_one !== null) { |
||
| 243 | return $default_one; |
||
| 244 | } elseif ($default_two !== null) { |
||
| 245 | return $default_two; |
||
| 246 | } else { |
||
| 247 | return; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the dependency injector. |
||
| 253 | * |
||
| 254 | * @return Returns a static reference to the Dependency Injector |
||
| 255 | */ |
||
| 256 | public static function getInjector() |
||
| 257 | { |
||
| 258 | return \Clansuite\Application::getInjector(); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set view. |
||
| 263 | * |
||
| 264 | * @param object $view RenderEngine Object |
||
| 265 | */ |
||
| 266 | public function setView($view) |
||
| 267 | { |
||
| 268 | $this->view = $view; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get view returns the render engine. |
||
| 273 | * |
||
| 274 | * @param string $renderEngineName Name of the render engine, like smarty, phptal. |
||
| 275 | * |
||
| 276 | * @return Returns the View Object (Rendering Engine) |
||
| 277 | */ |
||
| 278 | public function getView($renderEngineName = null) |
||
| 279 | { |
||
| 280 | // set the renderengine name |
||
| 281 | if ($renderEngineName !== null) { |
||
| 282 | $this->setRenderEngine($renderEngineName); |
||
| 283 | } |
||
| 284 | |||
| 285 | // if already set, get the rendering engine from the view variable |
||
| 286 | if ($this->view !== null) { |
||
| 287 | return $this->view; |
||
| 288 | } else { |
||
| 289 | // else, set the RenderEngine to the view variable and return it |
||
| 290 | $this->view = $this->getRenderEngine(); |
||
| 291 | |||
| 292 | return $this->view; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * sets the Rendering Engine. |
||
| 298 | * |
||
| 299 | * @param string $renderEngineName Name of the RenderEngine |
||
| 300 | */ |
||
| 301 | public function setRenderEngine($renderEngineName) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the Name of the Rendering Engine. |
||
| 310 | * Returns Json if an XMLHttpRequest is given. |
||
| 311 | * Returns Smarty as default if no rendering engine is set. |
||
| 312 | * |
||
| 313 | * @return string object, smarty as default |
||
| 314 | */ |
||
| 315 | public function getRenderEngineName() |
||
| 316 | { |
||
| 317 | // check if the requesttype is xmlhttprequest (ajax) is incomming, then we will return data in json format |
||
| 318 | if ($this->request->isAjax()) { |
||
| 319 | $this->setRenderEngine('json'); |
||
| 320 | } |
||
| 321 | |||
| 322 | // use smarty as default, if renderEngine is not set and it's not an ajax request |
||
| 323 | if (empty($this->renderEngineName)) { |
||
| 324 | $this->setRenderEngine('smarty'); |
||
| 325 | } |
||
| 326 | |||
| 327 | return $this->renderEngineName; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns the Rendering Engine Object via view_factory. |
||
| 332 | * |
||
| 333 | * @return renderengine object |
||
| 334 | */ |
||
| 335 | public function getRenderEngine() |
||
| 336 | { |
||
| 337 | return \Koch\View\Factory::getRenderer($this->getRenderEngineName(), self::getInjector()); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Sets the Render Mode. |
||
| 342 | * |
||
| 343 | * @param string $mode The RenderModes are LAYOUT or PARTIAL. |
||
| 344 | */ |
||
| 345 | public function setRenderMode($mode) |
||
| 346 | { |
||
| 347 | $this->getView()->renderMode = $mode; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the Render Mode. |
||
| 352 | * |
||
| 353 | * @return string LAYOUT|PARTIAL |
||
| 354 | */ |
||
| 355 | public function getRenderMode() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * modulecontroller->display();. |
||
| 366 | * |
||
| 367 | * All Output is done via the Response Object. |
||
| 368 | * ModelData -> View -> Response Object |
||
| 369 | * |
||
| 370 | * 1. getTemplateName() - get the template to render. |
||
| 371 | * 2. getView() - gets an instance of the render engine. |
||
| 372 | * 3. assign model data to that view object (a,b,c) |
||
| 373 | * 5. set data to response object |
||
| 374 | * |
||
| 375 | * @param $templates mixed|array|string Array with keys 'layout_template' / 'content_template' and templates |
||
| 376 | * as values or just content template name. |
||
| 377 | */ |
||
| 378 | public function display($templates = null) |
||
| 379 | { |
||
| 380 | // get the view |
||
| 381 | $this->view = $this->getView(); |
||
| 382 | |||
| 383 | // get the view mapper |
||
| 384 | $view_mapper = $this->view->getViewMapper(); |
||
| 385 | |||
| 386 | // set layout and content template by parameter array |
||
| 387 | if (is_array($templates)) { |
||
| 388 | if ($templates['layout_template'] !== null) { |
||
| 389 | $view_mapper->setLayoutTemplate($templates['layout_template']); |
||
| 390 | } |
||
| 391 | |||
| 392 | if ($templates['content_template'] !== null) { |
||
| 393 | $view_mapper->setTemplate($templates['content_template']); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | // only the "content template" was set |
||
| 398 | if (is_string($templates)) { |
||
| 399 | $view_mapper->setTemplate($templates); |
||
| 400 | } |
||
| 401 | |||
| 402 | // get templatename |
||
| 403 | $template = $view_mapper->getTemplateName(); |
||
| 404 | |||
| 405 | // Debug display of Layout Template and Content Template |
||
| 406 | //\Koch\Debug\Debug::firebug('Layout/Wrapper Template: ' . $this->view->getLayoutTemplate() . '<br />'); |
||
| 407 | //\Koch\Debug\Debug::firebug('Template Name: ' . $template . '<br />'); |
||
| 408 | |||
| 409 | // render the content / template |
||
| 410 | $content = $this->view->render($template); |
||
| 411 | |||
| 412 | // push content to the response object |
||
| 413 | $this->response->setContent($content); |
||
| 414 | |||
| 415 | unset($content, $template); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * This loads and initializes a formular from the module directory. |
||
| 420 | * |
||
| 421 | * @param string $formname The name of the formular. |
||
| 422 | * @param string $module The name of the action. |
||
| 423 | * @param bool $assign_to_view If true, the form is directly assigned as formname to the view |
||
| 424 | */ |
||
| 425 | public function loadForm($formname = null, $module = null, $action = null, $assign_to_view = true) |
||
| 426 | { |
||
| 427 | if (null === $module) { |
||
| 428 | $module = $this->request->getRoute()->getModule(); |
||
| 429 | } |
||
| 430 | |||
| 431 | if (null === $action) { |
||
| 432 | $action = $this->request->getRoute()->getAction(); |
||
| 433 | } |
||
| 434 | |||
| 435 | if (null === $formname) { |
||
| 436 | // construct form name like "news"_"action_show" |
||
| 437 | $formname = ucfirst($module) . '_' . ucfirst($action); // @todo adjust to PSR0 |
||
| 438 | } |
||
| 439 | |||
| 440 | // construct formname, classname, filename, load file, instantiate the form |
||
| 441 | $classname = 'Koch\Form\\' . $formname; |
||
| 442 | $filename = mb_strtolower($formname) . 'Form.php'; |
||
| 443 | $directory = APPLICATION_MODULES_PATH . mb_strtolower($module) . '/Form/'; |
||
| 444 | |||
| 445 | Loader::requireFile($directory . $filename, $classname); |
||
| 446 | |||
| 447 | // form preparation stage (combine description and add additional formelements) |
||
| 448 | $form = new $classname(); |
||
| 449 | |||
| 450 | // assign form object directly to the view or return to work with it |
||
| 451 | if ($assign_to_view === true) { |
||
| 452 | // do not call $form->render(), it's already done |
||
| 453 | $this->getView()->assign('form', $form); |
||
| 454 | } else { |
||
| 455 | return $form; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Redirect to Referer. |
||
| 461 | */ |
||
| 462 | public function redirectToReferer() |
||
| 463 | { |
||
| 464 | $referer = $this->request->getReferer(); |
||
| 465 | |||
| 466 | // we have a referer in the environment |
||
| 467 | if (empty($referer) === false) { |
||
| 468 | $this->redirect(SERVER_URL . $referer); |
||
| 469 | } else { // build referer on base of the current module |
||
| 470 | $route = $this->request->getRoute(); |
||
| 471 | |||
| 472 | // we use internal rewrite style here: /module/action |
||
| 473 | $redirect_to = '/' . $route->getModuleName(); |
||
| 474 | |||
| 475 | $submodule = $route->getSubModuleName(); |
||
| 476 | |||
| 477 | if (empty($submodule) === false) { |
||
| 478 | $redirect_to .= '/' . $submodule; |
||
| 479 | } |
||
| 480 | |||
| 481 | // redirect() builds the url |
||
| 482 | $this->response->redirect($redirect_to); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Shortcut for Redirect with an 404 Response Code. |
||
| 488 | * |
||
| 489 | * @param string $url Redirect to this URL |
||
| 490 | * @param int $time seconds before redirecting (for the html tag "meta refresh") |
||
| 491 | */ |
||
| 492 | public function redirect404($url, $time = 5) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Redirects to a new URL. |
||
| 499 | * It's a proxy method using the HttpResponse Object. |
||
| 500 | * |
||
| 501 | * @param string $url Redirect to this URL. |
||
| 502 | * @param int $time Seconds before redirecting (for the html tag "meta refresh") |
||
| 503 | * @param int $statusCode Http status code, default: '303' => 'See other' |
||
| 504 | * @param string $message Text of redirect message. |
||
| 505 | * @param string $mode The redirect mode: LOCATION, REFRESH, JS, HTML. |
||
| 506 | */ |
||
| 507 | public function redirect($url, $time = 0, $statusCode = 303, $message = null, $mode = null) |
||
| 508 | { |
||
| 509 | $this->response->redirect($url, $time, $statusCode, $message, $mode); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * addEvent (shortcut for usage in modules). |
||
| 514 | * |
||
| 515 | * @param string Name of the Event |
||
| 516 | * @param object Eventobject |
||
| 517 | */ |
||
| 518 | public function addEvent($eventName, \Koch\Event\Event $event) |
||
| 519 | { |
||
| 520 | \Koch\Event\Dispatcher::instantiate()->addEventHandler($eventName, $event); |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * triggerEvent is shortcut/convenience method for Eventdispatcher->triggerEvent. |
||
| 525 | * |
||
| 526 | * @param mixed (string|object) $event Name of Event or Event object to trigger. |
||
| 527 | * @param object $context Context of the event triggering, often simply ($this). Default Null. |
||
| 528 | * @param string $info Some pieces of information. Default Null. |
||
| 529 | */ |
||
| 530 | public function triggerEvent($event, $context = null, $info = null) |
||
| 531 | { |
||
| 532 | \Koch\Event\Dispatcher::instantiate()->triggerEvent($event, $context = null, $info = null); |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Shortcut to set a Flashmessage. |
||
| 537 | * |
||
| 538 | * @param string $type string error, warning, notice, success, debug |
||
| 539 | * @param string $message string A textmessage. |
||
| 540 | */ |
||
| 541 | public static function setFlashmessage($type, $message) |
||
| 542 | { |
||
| 543 | \Koch\Session\Flashmessages::setMessage($type, $message); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Adds a new breadcrumb. |
||
| 548 | * |
||
| 549 | * @param string $title Name of the trail element. Use Gettext _('Title')! |
||
| 550 | * @param string $link Link of the trail element |
||
| 551 | * @param string $replace_array_position Position in the array to replace with name/trail. Start = 0. |
||
| 552 | */ |
||
| 553 | public static function addBreadcrumb($title, $link = '', $replace_array_position = null) |
||
| 554 | { |
||
| 555 | \Koch\View\Helper\Breadcrumb::add($title, $link, $replace_array_position); |
||
| 556 | } |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Returns the HttpRequest Object. |
||
| 560 | * |
||
| 561 | * @return HttpRequest |
||
| 562 | */ |
||
| 563 | public function getHttpRequest() |
||
| 564 | { |
||
| 565 | return $this->request; |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Returns the HttpResponse Object. |
||
| 570 | * |
||
| 571 | * @return \Koch\Http\HttpResponse |
||
| 572 | */ |
||
| 573 | public function getHttpResponse() |
||
| 577 | } |
||
| 578 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.