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 |
||
51 | class ControllerServlet extends HttpServlet implements ControllerInterface |
||
52 | { |
||
53 | |||
54 | /** |
||
55 | * The key for the init parameter with the action namespace. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | const INIT_PARAMETER_ACTION_NAMESPACE = 'action.namespace'; |
||
60 | |||
61 | /** |
||
62 | * The key for the init parameter with the path to the configuration file. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | const INIT_PARAMETER_ROUTLT_CONFIGURATION_FILE = 'routlt.configuration.file'; |
||
67 | |||
68 | /** |
||
69 | * The default action if no valid action name was found in the path info. |
||
70 | * |
||
71 | * @var string |
||
72 | */ |
||
73 | const DEFAULT_ROUTE = '/index'; |
||
74 | |||
75 | /** |
||
76 | * The array with the initialized routes. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $routes = array(); |
||
81 | |||
82 | /** |
||
83 | * The array with the path descriptors. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $paths = array(); |
||
88 | |||
89 | /** |
||
90 | * The array with request method action -> route mappings. |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $actionMappings = array(); |
||
95 | |||
96 | /** |
||
97 | * Initializes the servlet with the passed configuration. |
||
98 | * |
||
99 | * @param \AppserverIo\Psr\Servlet\ServletConfigInterface $config The configuration to initialize the servlet with |
||
100 | * |
||
101 | * @return void |
||
102 | */ |
||
103 | 4 | public function init(ServletConfigInterface $config) |
|
104 | { |
||
105 | |||
106 | // call parent method |
||
107 | 4 | parent::init($config); |
|
108 | |||
109 | // load the values from the configuration file |
||
110 | 4 | $this->initConfiguration(); |
|
111 | |||
112 | // initialize the routing |
||
113 | 4 | $this->initRoutes(); |
|
114 | 4 | } |
|
115 | |||
116 | /** |
||
117 | * Returns the available routes. |
||
118 | * |
||
119 | * @return array The array with the available routes |
||
120 | */ |
||
121 | 2 | public function getRoutes() |
|
122 | { |
||
123 | 2 | return $this->routes; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * Returns the array with request method action -> route mappings. |
||
128 | * |
||
129 | * @return array The request method action -> route mappings |
||
130 | */ |
||
131 | 1 | public function getActionMappings() |
|
132 | { |
||
133 | 1 | return $this->actionMappings; |
|
134 | } |
||
135 | |||
136 | /** |
||
137 | * Returns the naming directoy instance (the application). |
||
138 | * |
||
139 | * @return \AppserverIo\Psr\Naming\NamingDirectoryInterface The naming directory instance |
||
140 | */ |
||
141 | 1 | public function getNamingDirectory() |
|
142 | { |
||
143 | 1 | return $this->getServletContext()->getApplication(); |
|
|
|||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Returns the object manager instance |
||
148 | * |
||
149 | * @return \AppserverIo\Psr\Di\ObjectManagerInterface The object manager instance |
||
150 | */ |
||
151 | 1 | public function getObjectManager() |
|
152 | { |
||
153 | 1 | return $this->getNamingDirectory()->search(ObjectManagerInterface::IDENTIFIER); |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * Returns the DI provider instance. |
||
158 | * |
||
159 | * @return \AppserverIo\Psr\Di\ProviderInterface The DI provider instance |
||
160 | */ |
||
161 | 1 | public function getProvider() |
|
162 | { |
||
163 | 1 | return $this->getNamingDirectory()->search(ProviderInterface::IDENTIFIER); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * This method returns the default route we'll invoke if the path info doesn't contain one. |
||
168 | * |
||
169 | * @return string The default route |
||
170 | */ |
||
171 | 2 | public function getDefaultRoute() |
|
172 | { |
||
173 | 2 | return ControllerServlet::DEFAULT_ROUTE; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Returns the array with the path descriptors. |
||
178 | * |
||
179 | * @return array The array with the path descriptors |
||
180 | */ |
||
181 | 1 | public function getPathDescriptors() |
|
182 | { |
||
183 | 1 | return $this->paths; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Adds a path descriptor to the controller. |
||
188 | * |
||
189 | * @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The path descriptor to add |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | 1 | public function addPathDescriptor(PathDescriptorInterface $pathDescriptor) |
|
194 | { |
||
195 | 1 | $this->paths[$pathDescriptor->getName()] = $pathDescriptor; |
|
196 | 1 | } |
|
197 | |||
198 | /** |
||
199 | * Returns the path descriptor with the passed name. |
||
200 | * |
||
201 | * @param string $name The name of the path descriptor to return |
||
202 | * |
||
203 | * @return \AppserverIo\Routlt\Description\PathDescriptorInterface The path descriptor instance |
||
204 | * @throws \Exception |
||
205 | */ |
||
206 | public function getPathDescriptor($name) |
||
207 | { |
||
208 | |||
209 | // query whether or not the path descriptor exists |
||
210 | if (isset($this->paths[$name])) { |
||
211 | return $this->paths[$name]; |
||
212 | } |
||
213 | |||
214 | // throw an exception if the requested path descriptor ist NOT available |
||
215 | throw new \Exception(sprintf('Can\'t find path descriptor with name "%s"', $name)); |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Loads the values found in the configuration file and merges |
||
220 | * them with the servlet context initialization parameters. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | 3 | protected function initConfiguration() |
|
245 | |||
246 | /** |
||
247 | * Initializes the available routes. |
||
248 | * |
||
249 | * @return void |
||
250 | */ |
||
251 | 3 | protected function initRoutes() |
|
252 | { |
||
253 | |||
254 | // load the action namespace |
||
255 | 3 | $actionNamespace = strtolower($this->getInitParameter(ControllerServlet::INIT_PARAMETER_ACTION_NAMESPACE)); |
|
320 | |||
321 | /** |
||
322 | * Creates a new instance of the action from the passed path descriptor instance. |
||
323 | * |
||
324 | * @param \AppserverIo\Routlt\Description\PathDescriptorInterface $pathDescriptor The path descriptor to create the action from |
||
325 | * |
||
326 | * @return \AppserverIo\Routlt\ActionInterface The action instance |
||
327 | */ |
||
328 | protected function initActionInstance(PathDescriptorInterface $pathDescriptor) |
||
347 | |||
348 | /** |
||
349 | * Creates a new instance of the action result the passed descriptor. |
||
350 | * |
||
351 | * @param \AppserverIo\Routlt\Description\ResultConfigurationDescriptorInterface $resultConfigurationDescriptor The action result configuration descriptor |
||
352 | * @param \AppserverIo\Routlt\ActionInterface $action The action instance the result is bound to |
||
353 | * |
||
354 | * @return \AppserverIo\Routlt\Results\ResultInterface The result instance |
||
355 | */ |
||
356 | protected function initResultInstance(ResultConfigurationDescriptorInterface $resultConfigurationDescriptor, ActionInterface $action) |
||
396 | |||
397 | /** |
||
398 | * Checks whether or not an action is generally available for any request method. |
||
399 | * Will return TRUE if so, FALSE otherwise. |
||
400 | * This method replicates a lot of the checks generally necessary but omits the request method check. |
||
401 | * Still best called in exception- or edge-cases |
||
402 | * |
||
403 | * @param string $pathInfo The action path which has been requested |
||
404 | * |
||
405 | * @return boolean |
||
406 | */ |
||
407 | 3 | public function checkGeneralActionAvailability($pathInfo) |
|
431 | |||
432 | /** |
||
433 | * Returns the array with request method action -> route mappings |
||
434 | * for the passed servlet request. |
||
435 | * |
||
436 | * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
||
437 | * |
||
438 | * @return array The request method action -> route mappings for the passed request method |
||
439 | */ |
||
440 | 4 | public function getActionMappingsForServletRequest(ServletRequestInterface $servletRequest) |
|
456 | |||
457 | /** |
||
458 | * Delegates to HTTP method specific functions like doPost() for POST e.g. |
||
459 | * |
||
460 | * @param \AppserverIo\Psr\Servlet\ServletRequestInterface $servletRequest The request instance |
||
461 | * @param \AppserverIo\Psr\Servlet\ServletResponseInterface $servletResponse The response sent back to the client |
||
462 | * |
||
463 | * @return void |
||
464 | * |
||
465 | * @throws \AppserverIo\Psr\Servlet\ServletException If no action has been found for the requested path |
||
466 | */ |
||
467 | 7 | public function service(ServletRequestInterface $servletRequest, ServletResponseInterface $servletResponse) |
|
581 | } |
||
582 |
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.