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 Dispatcher 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 Dispatcher, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class Dispatcher extends ParameterHolder |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * @var int The number of execution containers run so far. |
||
| 55 | */ |
||
| 56 | protected $numExecutions = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Context An Context instance. |
||
| 60 | */ |
||
| 61 | protected $context = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var Response The global response. |
||
| 65 | */ |
||
| 66 | protected $response = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var FilterChain The global filter chain. |
||
| 70 | */ |
||
| 71 | protected $filterChain = null; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var array An array of filter instances for reuse. |
||
| 75 | */ |
||
| 76 | protected $filters = array( |
||
| 77 | 'global' => array(), |
||
| 78 | 'controller' => array( |
||
| 79 | '*' => null |
||
| 80 | ), |
||
| 81 | 'dispatch' => null, |
||
| 82 | 'execution' => null, |
||
| 83 | 'security' => null |
||
| 84 | ); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var string The default Output Type. |
||
| 88 | */ |
||
| 89 | protected $defaultOutputType = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var array An array of registered Output Types. |
||
| 93 | */ |
||
| 94 | protected $outputTypes = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array Ref to the request data object from the request. |
||
| 98 | */ |
||
| 99 | private $requestData = null; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Increment the execution counter. |
||
| 103 | * Will throw an exception if the maximum amount of runs is exceeded. |
||
| 104 | * |
||
| 105 | * @throws DispatcherException If too many execution runs were made. |
||
| 106 | * |
||
| 107 | * @author David Zülke <[email protected]> |
||
| 108 | * @since 0.11.0 |
||
| 109 | */ |
||
| 110 | public function countExecution() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Create and initialize new execution container instance. |
||
| 121 | * |
||
| 122 | * @param string $moduleName The name of the module. |
||
| 123 | * @param string $controllerName The name of the controller. |
||
| 124 | * @param RequestDataHolder $arguments A RequestDataHolder with additional |
||
| 125 | * request arguments. |
||
| 126 | * @param string $outputType Optional name of an initial output type |
||
| 127 | * to set. |
||
| 128 | * @param string $requestMethod Optional name of the request method to |
||
| 129 | * be used in this container. |
||
| 130 | * |
||
| 131 | * @return ExecutionContainer A new execution container instance, |
||
| 132 | * fully initialized. |
||
| 133 | * |
||
| 134 | * @author David Zülke <[email protected]> |
||
| 135 | * @since 0.11.0 |
||
| 136 | */ |
||
| 137 | public function createExecutionContainer($moduleName = null, $controllerName = null, RequestDataHolder $arguments = null, $outputType = null, $requestMethod = null) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Initialize a module and load its autoload, module config etc. |
||
| 158 | * |
||
| 159 | * @param string $moduleName The name of the module to initialize. |
||
| 160 | * |
||
| 161 | * @author Felix Gilcher <[email protected]> |
||
| 162 | * @since 1.0.0 |
||
| 163 | */ |
||
| 164 | public function initializeModule($moduleName) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Dispatch a request |
||
| 212 | * |
||
| 213 | * @param RequestDataHolder $arguments An optional request data holder object |
||
| 214 | * with additional request data. |
||
| 215 | * @param ExecutionContainer $container An optional execution container that, |
||
| 216 | * if given, will be executed right away, |
||
| 217 | * skipping routing execution. |
||
| 218 | * |
||
| 219 | * @return Response The response produced during this dispatch call. |
||
| 220 | * |
||
| 221 | * @author David Zülke <[email protected]> |
||
| 222 | * @since 0.9.0 |
||
| 223 | */ |
||
| 224 | public function dispatch(RequestDataHolder $arguments = null, ExecutionContainer $container = null) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Get the global response instance. |
||
| 306 | * |
||
| 307 | * @return Response The global response. |
||
| 308 | * |
||
| 309 | * @author David Zülke <[email protected]> |
||
| 310 | * @since 0.11.0 |
||
| 311 | */ |
||
| 312 | public function getGlobalResponse() |
||
| 316 | |||
| 317 | |||
| 318 | /** |
||
| 319 | * Indicates whether or not a module has a specific controller file. |
||
| 320 | * |
||
| 321 | * Please note that this is only a cursory check and does not |
||
| 322 | * check whether the file actually contains the proper class |
||
| 323 | * |
||
| 324 | * @param string $moduleName A module name. |
||
| 325 | * @param string $controllerName An controller name. |
||
| 326 | * |
||
| 327 | * @return mixed the path to the controller file if the controller file |
||
| 328 | * exists and is readable, false in any other case |
||
| 329 | * |
||
| 330 | * @author Felix Gilcher <[email protected]> |
||
| 331 | * @since 1.0.0 |
||
| 332 | */ |
||
| 333 | View Code Duplication | public function checkControllerFile($moduleName, $controllerName) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Retrieve an Controller implementation instance. |
||
| 356 | * |
||
| 357 | * @param string $moduleName A module name. |
||
| 358 | * @param string $controllerName An controller name. |
||
| 359 | * |
||
| 360 | * @return Controller An Controller implementation instance |
||
| 361 | * |
||
| 362 | * @throws FileNotFoundException|ClassNotFoundException if the controller could not be found. |
||
| 363 | * |
||
| 364 | * @author Sean Kerr <[email protected]> |
||
| 365 | * @author Mike Vincent <[email protected]> |
||
| 366 | * @author David Zülke <[email protected]> |
||
| 367 | * @since 0.9.0 |
||
| 368 | */ |
||
| 369 | public function createControllerInstance($moduleName, $controllerName) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieve the current application context. |
||
| 395 | * |
||
| 396 | * @return Context An Context instance. |
||
| 397 | * |
||
| 398 | * @author Sean Kerr <[email protected]> |
||
| 399 | * @since 0.9.0 |
||
| 400 | */ |
||
| 401 | public final function getContext() |
||
| 405 | |||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * Indicates whether or not a module has a specific view file. |
||
| 410 | * |
||
| 411 | * Please note that this is only a cursory check and does not |
||
| 412 | * check whether the file actually contains the proper class |
||
| 413 | * |
||
| 414 | * @param string $moduleName A module name. |
||
| 415 | * @param string $viewName A view name. |
||
| 416 | * |
||
| 417 | * @return mixed the path to the view file if the view file |
||
| 418 | * exists and is readable, false in any other case |
||
| 419 | * |
||
| 420 | * @author Felix Gilcher <[email protected]> |
||
| 421 | * @since 1.0.0 |
||
| 422 | */ |
||
| 423 | View Code Duplication | public function checkViewFile($moduleName, $viewName) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Retrieve a View implementation instance. |
||
| 446 | * |
||
| 447 | * @param string $moduleName A module name. |
||
| 448 | * @param string $viewName A view name. |
||
| 449 | * |
||
| 450 | * @return View A View implementation instance, |
||
| 451 | * |
||
| 452 | * @throws AgaviException if the view could not be found. |
||
| 453 | * |
||
| 454 | * @author Sean Kerr <[email protected]> |
||
| 455 | * @author Mike Vincent <[email protected]> |
||
| 456 | * @author David Zülke <[email protected]> |
||
| 457 | * @since 0.9.0 |
||
| 458 | */ |
||
| 459 | public function createViewInstance($moduleName, $viewName) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Constructor. |
||
| 491 | * |
||
| 492 | * @author David Zülke <[email protected]> |
||
| 493 | * @since 0.11.0 |
||
| 494 | */ |
||
| 495 | public function __construct() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Initialize this Dispatcher. |
||
| 506 | * |
||
| 507 | * @param Context $context A Context instance. |
||
| 508 | * @param array $parameters An array of initialization parameters. |
||
| 509 | * |
||
| 510 | * @author David Zülke <[email protected]> |
||
| 511 | * @since 0.9.0 |
||
| 512 | */ |
||
| 513 | public function initialize(Context $context, array $parameters = array()) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get a filter. |
||
| 535 | * |
||
| 536 | * @param string $which The name of the filter list section. |
||
| 537 | * |
||
| 538 | * @return Filter A filter instance, or null. |
||
| 539 | * |
||
| 540 | * @author David Zülke <[email protected]> |
||
| 541 | * @since 0.11.0 |
||
| 542 | */ |
||
| 543 | public function getFilter($which) |
||
| 547 | |||
| 548 | /** |
||
| 549 | * Get the global filter chain. |
||
| 550 | * |
||
| 551 | * @return FilterChain The global filter chain. |
||
| 552 | * |
||
| 553 | * @author David Zülke <[email protected]> |
||
| 554 | * @since 1.1.0 |
||
| 555 | */ |
||
| 556 | View Code Duplication | public function getFilterChain() |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Load filters. |
||
| 568 | * |
||
| 569 | * @param FilterChain $filterChain A FilterChain instance. |
||
| 570 | * @param string $which "global" or "controller". |
||
| 571 | * @param string $module A module name, or "*" for the generic config. |
||
| 572 | * |
||
| 573 | * @author David Zülke <[email protected]> |
||
| 574 | * @since 0.11.0 |
||
| 575 | */ |
||
| 576 | public function loadFilters(FilterChain $filterChain, $which = 'global', $module = null) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Indicates whether or not a module has a specific model. |
||
| 609 | * |
||
| 610 | * @param string $moduleName A module name. |
||
| 611 | * @param string $modelName A model name. |
||
| 612 | * |
||
| 613 | * @return bool true, if the model exists, otherwise false. |
||
| 614 | * |
||
| 615 | * @author Sean Kerr <[email protected]> |
||
| 616 | * @since 0.9.0 |
||
| 617 | */ |
||
| 618 | public function modelExists($moduleName, $modelName) |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Indicates whether or not a module exists. |
||
| 627 | * |
||
| 628 | * @param string $moduleName A module name. |
||
| 629 | * |
||
| 630 | * @return bool true, if the module exists, otherwise false. |
||
| 631 | * |
||
| 632 | * @author Sean Kerr <[email protected]> |
||
| 633 | * @since 0.9.0 |
||
| 634 | */ |
||
| 635 | public function moduleExists($moduleName) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Do any necessary startup work after initialization. |
||
| 643 | * |
||
| 644 | * This method is not called directly after initialize(). |
||
| 645 | * |
||
| 646 | * @author David Zülke <[email protected]> |
||
| 647 | * @since 0.11.0 |
||
| 648 | */ |
||
| 649 | public function startup() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Execute the shutdown procedure for this Dispatcher. |
||
| 657 | * |
||
| 658 | * @author Sean Kerr <[email protected]> |
||
| 659 | * @since 0.9.0 |
||
| 660 | */ |
||
| 661 | public function shutdown() |
||
| 664 | |||
| 665 | /** |
||
| 666 | * Indicates whether or not a module has a specific controller. |
||
| 667 | * |
||
| 668 | * @param string $moduleName A module name. |
||
| 669 | * @param string $controllerName A view name. |
||
| 670 | * |
||
| 671 | * @return bool true, if the controller exists, otherwise false. |
||
| 672 | * |
||
| 673 | * @author David Zülke <[email protected]> |
||
| 674 | * @since 1.0.1 |
||
| 675 | */ |
||
| 676 | public function controllerExists($moduleName, $controllerName) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Indicates whether or not a module has a specific view. |
||
| 683 | * |
||
| 684 | * @param string $moduleName A module name. |
||
| 685 | * @param string $viewName A view name. |
||
| 686 | * |
||
| 687 | * @return bool true, if the view exists, otherwise false. |
||
| 688 | * |
||
| 689 | * @author Sean Kerr <[email protected]> |
||
| 690 | * @since 0.9.0 |
||
| 691 | */ |
||
| 692 | public function viewExists($moduleName, $viewName) |
||
| 696 | |||
| 697 | /** |
||
| 698 | * Retrieve an Output Type object |
||
| 699 | * |
||
| 700 | * @param string $name The optional output type name. |
||
| 701 | * |
||
| 702 | * @return OutputType An Output Type object. |
||
| 703 | * |
||
| 704 | * @author David Zülke <[email protected]> |
||
| 705 | * @since 0.11.0 |
||
| 706 | */ |
||
| 707 | View Code Duplication | public function getOutputType($name = null) |
|
| 718 | } |
||
| 719 | |||
| 720 | ?> |
||
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: