Complex classes like ControllerServlet 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 ControllerServlet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 50 | class ControllerServlet extends HttpServlet implements ControllerInterface |
||
| 51 | { |
||
| 52 | |||
| 53 | /** |
||
| 54 | * The key for the init parameter with the action namespace. |
||
| 55 | * |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | const INIT_PARAMETER_ACTION_NAMESPACE = 'action.namespace'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * The key for the init parameter with the path to the configuration file. |
||
| 62 | * |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | const INIT_PARAMETER_ROUTLT_CONFIGURATION_FILE = 'routlt.configuration.file'; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The default action if no valid action name was found in the path info. |
||
| 69 | * |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | const DEFAULT_ROUTE = '/index'; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The array with the initialized routes. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $routes = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The array with the path descriptors. |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | protected $paths = array(); |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The array with request method action -> route mappings. |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $actionMappings = array(); |
||
| 94 | 4 | ||
| 95 | /** |
||
| 96 | * Initializes the servlet with the passed configuration. |
||
| 97 | * |
||
| 98 | 4 | * @param \AppserverIo\Psr\Servlet\ServletConfigInterface $config The configuration to initialize the servlet with |
|
| 99 | * |
||
| 100 | * @return void |
||
| 101 | 4 | */ |
|
| 102 | public function init(ServletConfigInterface $config) |
||
| 103 | { |
||
| 104 | 4 | ||
| 105 | 4 | // call parent method |
|
| 106 | parent::init($config); |
||
| 107 | |||
| 108 | // load the values from the configuration file |
||
| 109 | $this->initConfiguration(); |
||
| 110 | |||
| 111 | // initialize the routing |
||
| 112 | $this->initRoutes(); |
||
| 113 | } |
||
| 114 | 1 | ||
| 115 | /** |
||
| 116 | 1 | * Returns the available routes. |
|
| 117 | * |
||
| 118 | * @return array The array with the available routes |
||
| 119 | */ |
||
| 120 | public function getRoutes() |
||
| 121 | { |
||
| 122 | return $this->routes; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | 1 | * Returns the array with request method action -> route mappings. |
|
| 127 | * |
||
| 128 | 1 | * @return array The request method action -> route mappings |
|
| 129 | */ |
||
| 130 | public function getActionMappings() |
||
| 131 | { |
||
| 132 | return $this->actionMappings; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | 1 | * Returns the naming directoy instance (the application). |
|
| 137 | * |
||
| 138 | 1 | * @return \AppserverIo\Psr\Naming\NamingDirectoryInterface The naming directory instance |
|
| 139 | */ |
||
| 140 | public function getNamingDirectory() |
||
| 141 | { |
||
| 142 | return $this->getServletContext()->getApplication(); |
||
|
|
|||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | 1 | * Returns the object manager instance |
|
| 147 | * |
||
| 148 | 1 | * @return \AppserverIo\Psr\Di\ObjectManagerInterface The object manager instance |
|
| 149 | */ |
||
| 150 | public function getObjectManager() |
||
| 151 | { |
||
| 152 | return $this->getNamingDirectory()->search(ObjectManagerInterface::IDENTIFIER); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | 2 | * Returns the DI provider instance. |
|
| 157 | * |
||
| 158 | 2 | * @return \AppserverIo\Psr\Di\ProviderInterface The DI provider instance |
|
| 159 | */ |
||
| 160 | public function getProvider() |
||
| 161 | { |
||
| 162 | return $this->getNamingDirectory()->search(ProviderInterface::IDENTIFIER); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | 1 | * This method returns the default route we'll invoke if the path info doesn't contain one. |
|
| 167 | * |
||
| 168 | 1 | * @return string The default route |
|
| 169 | */ |
||
| 170 | public function getDefaultRoute() |
||
| 171 | { |
||
| 172 | return ControllerServlet::DEFAULT_ROUTE; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Returns the array with the path descriptors. |
||
| 177 | * |
||
| 178 | 1 | * @return array The array with the path descriptors |
|
| 179 | */ |
||
| 180 | 1 | public function getPathDescriptors() |
|
| 181 | 1 | { |
|
| 182 | return $this->paths; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Adds a path descriptor to the controller. |
||
| 187 | * |
||
| 188 | * @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The path descriptor to add |
||
| 189 | 3 | * |
|
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function addPathDescriptor(PathDescriptorInterface $pathDescriptor) |
||
| 196 | 3 | ||
| 197 | /** |
||
| 198 | * Returns the path descriptor with the passed name. |
||
| 199 | 3 | * |
|
| 200 | * @param string $name The name of the path descriptor to return |
||
| 201 | 1 | * |
|
| 202 | 1 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The path descriptor instance |
|
| 203 | * @throws \Exception |
||
| 204 | */ |
||
| 205 | 1 | public function getPathDescriptor($name) |
|
| 216 | 3 | ||
| 217 | /** |
||
| 218 | * Loads the values found in the configuration file and merges |
||
| 219 | * them with the servlet context initialization parameters. |
||
| 220 | 3 | * |
|
| 221 | * @return void |
||
| 222 | */ |
||
| 223 | 3 | protected function initConfiguration() |
|
| 244 | |||
| 245 | 2 | /** |
|
| 246 | 1 | * Initializes the available routes. |
|
| 247 | 2 | * |
|
| 248 | * @return void |
||
| 249 | */ |
||
| 250 | 2 | protected function initRoutes() |
|
| 322 | |||
| 323 | /** |
||
| 324 | 1 | * Creates a new instance of the action from the passed path descriptor instance. |
|
| 325 | * |
||
| 326 | * @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The path descriptor to create the action from |
||
| 327 | 1 | * |
|
| 328 | 1 | * @return \AppserverIo\Routlt\ActionInterface The action instance |
|
| 329 | 1 | */ |
|
| 330 | protected function initActionInstance(PathDescriptorInterface $pathDescriptor) |
||
| 349 | |||
| 350 | 1 | /** |
|
| 351 | * Creates a new instance of the action result the passed descriptor. |
||
| 352 | 1 | * |
|
| 353 | * @param \AppserverIo\Routlt\Description\ResultDescriptorInterface $resultDescriptor The action result descriptor |
||
| 354 | * @param \AppserverIo\Routlt\ActionInterface $action The action instance the result is bound to |
||
| 355 | * |
||
| 356 | * @return \AppserverIo\Routlt\Results\ResultInterface The result instance |
||
| 357 | */ |
||
| 358 | protected function initResultInstance(ResultDescriptorInterface $resultDescriptor, ActionInterface $action) |
||
| 385 | |||
| 386 | 2 | /** |
|
| 387 | * Checks whether or not an action is generally available for any request method. |
||
| 388 | * Will return TRUE if so, FALSE otherwise. |
||
| 389 | * This method replicates a lot of the checks generally necessary but omits the request method check. |
||
| 390 | * Still best called in exception- or edge-cases |
||
| 391 | * |
||
| 392 | * @param string $pathInfo The action path which has been requested |
||
| 393 | * |
||
| 394 | * @return boolean |
||
| 395 | */ |
||
| 396 | public function checkGeneralActionAvailability($pathInfo) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Returns the array with request method action -> route mappings |
||
| 423 | * for the passed servlet request. |
||
| 424 | 7 | * |
|
| 425 | * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
||
| 426 | * |
||
| 427 | * @return array The request method action -> route mappings for the passed request method |
||
| 428 | */ |
||
| 429 | 7 | public function getActionMappingsForServletRequest(ServletRequestInterface $servletRequest) |
|
| 445 | |||
| 446 | /** |
||
| 447 | 6 | * Delegates to HTTP method specific functions like doPost() for POST e.g. |
|
| 448 | * |
||
| 449 | * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
||
| 450 | 6 | * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client |
|
| 451 | * |
||
| 452 | * @return void |
||
| 453 | 5 | * |
|
| 454 | * @throws \AppserverIo\Psr\Servlet\ServletException If no action has been found for the requested path |
||
| 455 | */ |
||
| 456 | 5 | public function service(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) |
|
| 570 | } |
||
| 571 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.