Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | abstract class Controller extends ContainerAware |
||
|
|
|||
| 69 | { |
||
| 70 | /** |
||
| 71 | * Parameters specified by the route |
||
| 72 | * @var ParameterBag |
||
| 73 | */ |
||
| 74 | protected $parameters; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The first controller that was invoked |
||
| 78 | * @var Controller |
||
| 79 | */ |
||
| 80 | protected $parent; |
||
| 81 | |||
| 82 | /* |
||
| 83 | * An array of data to pass between different parts of the application |
||
| 84 | * |
||
| 85 | * @var ParameterBag |
||
| 86 | */ |
||
| 87 | public $data; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param ParameterBag $parameters The array returned by $request->attributes |
||
| 91 | * @param Controller|null $parent The controller who invoked this controller |
||
| 92 | */ |
||
| 93 | 1 | public function __construct($parameters, Controller $parent = null) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Returns the controller that is assigned to a route |
||
| 104 | * |
||
| 105 | * @param ParameterBag $parameters The array returned by $request->attributes |
||
| 106 | * @return Controller The controller |
||
| 107 | */ |
||
| 108 | 1 | public static function getController($parameters) |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Call the controller's action specified by the $parameters array |
||
| 118 | * |
||
| 119 | * @param string|null $action The action name to call (e.g. `show`), null to invoke the default one |
||
| 120 | * @return Response The action's response |
||
| 121 | */ |
||
| 122 | 1 | public function callAction($action = null) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get the controller's default action name |
||
| 133 | * |
||
| 134 | * @return string The action's name without the `Action` suffix |
||
| 135 | */ |
||
| 136 | 1 | protected function getDefaultActionName() |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Get a controller's action |
||
| 143 | * |
||
| 144 | * @param string|null $action The action name to call (e.g. `show`), null to invoke the default one |
||
| 145 | * @return \ReflectionMethod The action method |
||
| 146 | */ |
||
| 147 | 1 | public function getAction($action = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Forward the request to another action |
||
| 158 | * |
||
| 159 | * Please note that this doesn't generate an HTTP redirect, but an |
||
| 160 | * internal one - the user sees the original URL, but a different page |
||
| 161 | * |
||
| 162 | * @param string $action The action to forward the request to |
||
| 163 | * @param array $params An additional associative array of parameters to |
||
| 164 | * provide to the action |
||
| 165 | * @param string|null $controllerName The name of the controller of the |
||
| 166 | * action, without the 'Controller' |
||
| 167 | * suffix (defaults to the current |
||
| 168 | * controller) |
||
| 169 | * @return Response |
||
| 170 | */ |
||
| 171 | 1 | protected function forward($action, $params = array(), $controllerName = null) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Method that will be called before any action |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | 1 | public function setup() |
|
| 200 | |||
| 201 | /** |
||
| 202 | * Method that will be called after all actions |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | 1 | public function cleanup() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Call one of the controller's methods |
||
| 212 | * |
||
| 213 | * The arguments are passed dynamically to the method, based on its |
||
| 214 | * definition - check the description of the Controller class for more |
||
| 215 | * information |
||
| 216 | * |
||
| 217 | * @param \ReflectionMethod $method The method |
||
| 218 | * @param ParameterBag $parameters The parameter bag representing the route's parameters |
||
| 219 | * @return mixed The return value of the called method |
||
| 220 | */ |
||
| 221 | 1 | protected function callMethod($method, $parameters) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Find what to pass as an argument on an action |
||
| 245 | * |
||
| 246 | * @param ReflectionParameter $modelParameter The model's parameter we want to investigate |
||
| 247 | * @param ParameterBag $routeParameters The route's parameters |
||
| 248 | */ |
||
| 249 | 1 | protected function getObjectFromParameters($modelParameter, $routeParameters) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Try locating a method's parameter in an array |
||
| 292 | * |
||
| 293 | * @param ReflectionParameter $modelParameter The model's parameter we want to investigate |
||
| 294 | * @param ParameterBag $routeParameters The route's parameters |
||
| 295 | * @return Model|null A Model or null if it couldn't be found |
||
| 296 | */ |
||
| 297 | 1 | protected function findModelInParameters($modelParameter, $routeParameters) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Render the action's template |
||
| 320 | * @param array $params The variables to pass to the template |
||
| 321 | * @param string $action The controller's action |
||
| 322 | * @return string The content |
||
| 323 | */ |
||
| 324 | 1 | protected function renderDefault($params, $action) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Get a Response from the return value of an action |
||
| 333 | * @param mixed $return Whatever the method returned |
||
| 334 | * @param string $action The name of the action |
||
| 335 | * @return Response The response that the controller wants us to send to the client |
||
| 336 | */ |
||
| 337 | 1 | protected function handleReturnValue($return, $action) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the name of the controller without the "Controller" part |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | 1 | public static function getName() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Returns a configured QueryBuilder for the corresponding model |
||
| 366 | * |
||
| 367 | * The returned QueryBuilder will only show models visible to the currently |
||
| 368 | * logged in user |
||
| 369 | * |
||
| 370 | * @param string|null The model whose query builder we should get (null |
||
| 371 | * to get the builder of the controller's model) |
||
| 372 | * @param string $type |
||
| 373 | * @return QueryBuilder |
||
| 374 | */ |
||
| 375 | 1 | public static function getQueryBuilder($type = null) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Generates a URL from the given parameters. |
||
| 385 | * @param string $name The name of the route |
||
| 386 | * @param mixed $parameters An array of parameters |
||
| 387 | * @param bool $absolute Whether to generate an absolute URL |
||
| 388 | * @return string The generated URL |
||
| 389 | */ |
||
| 390 | public static function generate($name, $parameters = array(), $absolute = false) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Gets the browser's request |
||
| 397 | * @return Symfony\Component\HttpFoundation\Request |
||
| 398 | */ |
||
| 399 | 1 | public static function getRequest() |
|
| 403 | |||
| 404 | /** |
||
| 405 | * Gets the currently logged in player |
||
| 406 | * |
||
| 407 | * If the user is not logged in, a Player object that is invalid will be |
||
| 408 | * returned |
||
| 409 | * |
||
| 410 | * @return Player |
||
| 411 | */ |
||
| 412 | 1 | public static function getMe() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Find out whether debugging is enabled |
||
| 419 | * |
||
| 420 | * @return bool |
||
| 421 | */ |
||
| 422 | 1 | public function isDebug() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Gets the monolog logger |
||
| 429 | * |
||
| 430 | * @param string $channel The log channel, defaults to the Controller's default |
||
| 431 | * @return Monolog\Logger |
||
| 432 | */ |
||
| 433 | protected static function getLogger($channel = null) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Gets the logging channel for monolog |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | protected static function getLogChannel() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Uses symfony's dispatcher to announce an event |
||
| 453 | * @param string $eventName The name of the event to dispatch. |
||
| 454 | * @param Event $event The event to pass to the event handlers/listeners. |
||
| 455 | * @return Event |
||
| 456 | */ |
||
| 457 | 1 | protected function dispatch($eventName, Event $event = null) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Renders a view |
||
| 464 | * @param string $view The view name |
||
| 465 | * @param array $parameters An array of parameters to pass to the view |
||
| 466 | * @return string The rendered view |
||
| 467 | */ |
||
| 468 | 1 | protected function render($view, $parameters = array()) |
|
| 478 | } |
||
| 479 | |||
| 483 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.