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 Context 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 Context, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | class Context |
||
| 58 | { |
||
| 59 | /** |
||
| 60 | * @var string The name of the Context. |
||
| 61 | */ |
||
| 62 | protected $name = ''; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Dispatcher A Dispatcher instance. |
||
| 66 | */ |
||
| 67 | protected $dispatcher = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var array An array of class names for frequently used factories. |
||
| 71 | */ |
||
| 72 | protected $factories = array( |
||
| 73 | 'dispatch_filter' => null, |
||
| 74 | 'execution_container' => null, |
||
| 75 | 'execution_filter' => null, |
||
| 76 | 'filter_chain' => null, |
||
| 77 | 'response' => null, |
||
| 78 | 'security_filter' => null, |
||
| 79 | 'validation_manager' => null, |
||
| 80 | ); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var DatabaseManager A DatabaseManager instance. |
||
| 84 | */ |
||
| 85 | protected $databaseManager = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var LoggerManager A LoggerManager instance. |
||
| 89 | */ |
||
| 90 | protected $loggerManager = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var Request A Request instance. |
||
| 94 | */ |
||
| 95 | protected $request = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var Routing A Routing instance. |
||
| 99 | */ |
||
| 100 | protected $routing = null; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var Storage A Storage instance. |
||
| 104 | */ |
||
| 105 | protected $storage = null; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var TranslationManager A TranslationManager instance. |
||
| 109 | */ |
||
| 110 | protected $translationManager = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var User A User instance. |
||
| 114 | */ |
||
| 115 | protected $user = null; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var array The array used for the shutdown sequence. |
||
| 119 | */ |
||
| 120 | protected $shutdownSequence = array(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var array An array of Context instances. |
||
| 124 | */ |
||
| 125 | protected static $instances = array(); |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var array An array of SingletonModel instances. |
||
| 129 | */ |
||
| 130 | protected $singletonModelInstances = array(); |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Clone method, overridden to prevent cloning, there can be only one. |
||
| 134 | * |
||
| 135 | * @author Mike Vincent <[email protected]> |
||
| 136 | * @since 0.9.0 |
||
| 137 | */ |
||
| 138 | public function __clone() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Constructor method, intentionally made protected so the context cannot be |
||
| 145 | * created directly. |
||
| 146 | * |
||
| 147 | * @param string $name The name of this context. |
||
| 148 | * |
||
| 149 | * @author David Zülke <[email protected]> |
||
| 150 | * @author Mike Vincent <[email protected]> |
||
| 151 | * @since 0.9.0 |
||
| 152 | */ |
||
| 153 | protected function __construct($name) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * __toString overload, returns the name of the Context. |
||
| 160 | * |
||
| 161 | * @return string The context name. |
||
| 162 | * |
||
| 163 | * @see Context::getName() |
||
| 164 | * |
||
| 165 | * @author David Zülke <[email protected]> |
||
| 166 | * @since 0.11.0 |
||
| 167 | */ |
||
| 168 | public function __toString() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get information on a frequently used class. |
||
| 175 | * |
||
| 176 | * @param string $for The factory identifier. |
||
| 177 | * |
||
| 178 | * @return array An associative array (keys 'class' and 'parameters'). |
||
| 179 | * |
||
| 180 | * @author David Zülke <[email protected]> |
||
| 181 | * @since 0.11.0 |
||
| 182 | */ |
||
| 183 | public function getFactoryInfo($for) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set information on a frequently used class. |
||
| 192 | * |
||
| 193 | * @param string $for The factory identifier. |
||
| 194 | * @param array $info An associative array (keys 'class' and 'parameters'). |
||
| 195 | * |
||
| 196 | * @author Felix Gilcher <[email protected]> |
||
| 197 | * @since 1.0.1 |
||
| 198 | */ |
||
| 199 | public function setFactoryInfo($for, array $info) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Factory for frequently used classes from factories.xml |
||
| 206 | * |
||
| 207 | * @param string $for The factory identifier. |
||
| 208 | * |
||
| 209 | * @return mixed An instance, already initialized with parameters. |
||
| 210 | * |
||
| 211 | * @throws AgaviException If no such identifier exists. |
||
| 212 | * |
||
| 213 | * @author David Zülke <[email protected]> |
||
| 214 | * @since 1.0.0 |
||
| 215 | */ |
||
| 216 | public function createInstanceFor($for) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Retrieve the Dispatcher. |
||
| 230 | * |
||
| 231 | * @return Dispatcher The current Dispatcher implementation instance. |
||
| 232 | * |
||
| 233 | * @author Sean Kerr <[email protected]> |
||
| 234 | * @since 0.9.0 |
||
| 235 | */ |
||
| 236 | public function getDispatcher() |
||
| 237 | { |
||
| 238 | return $this->dispatcher; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Retrieve a database connection from the database manager. |
||
| 243 | * |
||
| 244 | * This is a shortcut to manually getting a connection from an existing |
||
| 245 | * database implementation instance. |
||
| 246 | * |
||
| 247 | * If the core.use_database setting is off, this will return null. |
||
| 248 | * |
||
| 249 | * @param string $name A database name. |
||
| 250 | * |
||
| 251 | * @return mixed A database connection. |
||
| 252 | * |
||
| 253 | * @throws DatabaseException If the requested database name |
||
| 254 | * does not exist. |
||
| 255 | * |
||
| 256 | * @author Sean Kerr <[email protected]> |
||
| 257 | * @since 0.9.0 |
||
| 258 | */ |
||
| 259 | public function getDatabaseConnection($name = null) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Retrieve the database manager. |
||
| 268 | * |
||
| 269 | * @return DatabaseManager The current DatabaseManager instance. |
||
| 270 | * |
||
| 271 | * @author Sean Kerr <[email protected]> |
||
| 272 | * @since 0.9.0 |
||
| 273 | */ |
||
| 274 | public function getDatabaseManager() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Retrieve the Context instance. |
||
| 281 | * |
||
| 282 | * If you don't supply a profile name this will try to return the context |
||
| 283 | * specified in the <kbd>core.default_context</kbd> setting. |
||
| 284 | * |
||
| 285 | * @param string $profile A name corresponding to a section of the config |
||
| 286 | * |
||
| 287 | * @return Context An context instance initialized with the |
||
| 288 | * settings of the requested context name |
||
| 289 | * |
||
| 290 | * @author Dominik del Bondio <[email protected]> |
||
| 291 | * @author David Zülke <[email protected]> |
||
| 292 | * @author Mike Vincent <[email protected]> |
||
| 293 | * @since 0.9.0 |
||
| 294 | */ |
||
| 295 | public static function getInstance($profile = null) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Retrieve the LoggerManager |
||
| 318 | * |
||
| 319 | * @return LoggerManager The current LoggerManager implementation |
||
| 320 | * instance. |
||
| 321 | * |
||
| 322 | * @author David Zülke <[email protected]> |
||
| 323 | * @since 0.11.0 |
||
| 324 | */ |
||
| 325 | public function getLoggerManager() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * (re)Initialize the Context instance. |
||
| 332 | * |
||
| 333 | * @author Dominik del Bondio <[email protected]> |
||
| 334 | * @author David Zülke <[email protected]> |
||
| 335 | * @author Mike Vincent <[email protected]> |
||
| 336 | * @since 0.10.0 |
||
| 337 | */ |
||
| 338 | public function initialize() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Shut down this Context and all related factories. |
||
| 351 | * |
||
| 352 | * @author David Zülke <[email protected]> |
||
| 353 | * @since 0.11.0 |
||
| 354 | */ |
||
| 355 | public function shutdown() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Retrieve a Model implementation instance. |
||
| 364 | * |
||
| 365 | * @param string $modelName A model name. |
||
| 366 | * @param string $moduleNameA module name, if the requested model is a module model, |
||
|
|
|||
| 367 | * or null for global models. |
||
| 368 | * @param array $parameters An array of parameters to be passed to initialize() or |
||
| 369 | * the constructor. |
||
| 370 | * |
||
| 371 | * @return Model A Model implementation instance. |
||
| 372 | * |
||
| 373 | * @throws AutoloadException if class is ultimately not found. |
||
| 374 | * |
||
| 375 | * |
||
| 376 | * @author David Zülke <[email protected]> |
||
| 377 | * @since 0.11.0 |
||
| 378 | */ |
||
| 379 | public function getModel($modelName, $moduleName = null, array $parameters = null) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Retrieve the name of this Context. |
||
| 480 | * |
||
| 481 | * @return string A context name. |
||
| 482 | * |
||
| 483 | * @author David Zülke <[email protected]> |
||
| 484 | * @since 0.11.0 |
||
| 485 | */ |
||
| 486 | public function getName() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Retrieve the request. |
||
| 493 | * |
||
| 494 | * @return Request The current Request implementation instance. |
||
| 495 | * |
||
| 496 | * @author Sean Kerr <[email protected]> |
||
| 497 | * @since 0.9.0 |
||
| 498 | */ |
||
| 499 | public function getRequest() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Retrieve the routing. |
||
| 506 | * |
||
| 507 | * @return Routing The current Routing implementation instance. |
||
| 508 | * |
||
| 509 | * @author Dominik del Bondio <[email protected]> |
||
| 510 | * @since 0.11.0 |
||
| 511 | */ |
||
| 512 | public function getRouting() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Retrieve the storage. |
||
| 519 | * |
||
| 520 | * @return Storage The current Storage implementation instance. |
||
| 521 | * |
||
| 522 | * @author Sean Kerr <[email protected]> |
||
| 523 | * @since 0.9.0 |
||
| 524 | */ |
||
| 525 | public function getStorage() |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Retrieve the translation manager. |
||
| 532 | * |
||
| 533 | * @return TranslationManager The current TranslationManager |
||
| 534 | * implementation instance. |
||
| 535 | * |
||
| 536 | * @author Dominik del Bondio <[email protected]> |
||
| 537 | * @since 0.11.0 |
||
| 538 | */ |
||
| 539 | public function getTranslationManager() |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Retrieve the user. |
||
| 546 | * |
||
| 547 | * @return User The current User implementation instance. |
||
| 548 | * |
||
| 549 | * @author Sean Kerr <[email protected]> |
||
| 550 | * @since 0.9.0 |
||
| 551 | */ |
||
| 552 | public function getUser() |
||
| 556 | } |
||
| 557 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.