Complex classes like HTMLController 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 HTMLController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | abstract class HTMLController extends Controller |
||
13 | { |
||
14 | /** |
||
15 | * Whether twig has been prepared |
||
16 | * @var bool |
||
17 | */ |
||
18 | public $twigReady = false; |
||
19 | |||
20 | /** |
||
21 | * Prepare the twig global variables |
||
22 | */ |
||
23 | 1 | private function addTwigGlobals() |
|
44 | |||
45 | 1 | protected function prepareTwig() |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | * @param string $view |
||
52 | */ |
||
53 | 1 | protected function render($view, $parameters = array()) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | * |
||
63 | * @throws ModelNotFoundException |
||
64 | */ |
||
65 | 1 | protected function findModelInParameters($modelParameter, $routeParameters) |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 1 | public function callAction($action = null) |
|
97 | |||
98 | /** |
||
99 | * Save the URL of the current page so that the user can be redirected back to it |
||
100 | * if they login |
||
101 | */ |
||
102 | 1 | protected function saveURL() |
|
117 | |||
118 | /** |
||
119 | * Returns the path to the home page |
||
120 | * @return string |
||
121 | */ |
||
122 | 1 | protected function getHomeURL() |
|
126 | |||
127 | /** |
||
128 | * Returns the URL of the previous page |
||
129 | * @return string |
||
130 | */ |
||
131 | 1 | protected function getPreviousURL() |
|
146 | |||
147 | /** |
||
148 | * Returns a redirect response to the previous page |
||
149 | * @return RedirectResponse |
||
150 | */ |
||
151 | protected function goBack() |
||
155 | |||
156 | /** |
||
157 | * Returns a redirect response to the home page |
||
158 | * @return RedirectResponse |
||
159 | */ |
||
160 | 1 | protected function goHome() |
|
164 | |||
165 | /** |
||
166 | * Get the session's flash bag |
||
167 | * @return Symfony\Component\HttpFoundation\Session\Flash\FlashBag |
||
168 | */ |
||
169 | 1 | public static function getFlashBag() |
|
173 | |||
174 | /** |
||
175 | * Find out whether the currently logged in user can see a model |
||
176 | * |
||
177 | * Apart from the permissions of the user, this method takes the request |
||
178 | * query into consideration to find out if the user wants to see deleted |
||
179 | * models or not. |
||
180 | * |
||
181 | * @param Model Model Model in question |
||
182 | * @return bool |
||
183 | */ |
||
184 | 1 | public static function canSee($model) |
|
192 | |||
193 | /** |
||
194 | * Assert that the user is logged in |
||
195 | * @param string $message The message to show if the user is not logged in |
||
196 | * @throws HTTPException |
||
197 | * @return void |
||
198 | */ |
||
199 | 1 | protected function requireLogin( |
|
206 | |||
207 | /** |
||
208 | * Show a confirmation (Yes, No) form to the user |
||
209 | * |
||
210 | * @param callable $onYes What to do if the user clicks on "Yes" |
||
211 | * @param string $message The message to show to the user, asking |
||
212 | * them to confirm their action (RAW text |
||
213 | * is shown - don't forget to properly |
||
214 | * escape your parameters!) |
||
215 | * @param string $action The text to show on the "Yes" button |
||
216 | * @param string $successMessage A message to add on the session's |
||
217 | * flashbag on success (flashbags |
||
218 | * automatically escape text) |
||
219 | * @param callable $onNo What to do if the user presses "No" - |
||
220 | * defaults to redirecting them back |
||
221 | * @param string $view The view to redirect to |
||
222 | * @param bool $noButton Whether to show a "No" instead of a |
||
223 | * "Cancel" button |
||
224 | * @return mixed The response |
||
225 | */ |
||
226 | 1 | protected function showConfirmationForm( |
|
260 | |||
261 | /** |
||
262 | * Decompose a list of object IDs into the corresponding IDs |
||
263 | * |
||
264 | * @param string $query The user's query |
||
265 | * @param array $types A list of the acceptable model types (will NOT be sanitized) |
||
266 | * @param bool $models Whether to return an array of models instead of an array of IDs |
||
267 | * @param int|null $max The largest number of models to accept, or null for infinite models |
||
268 | * |
||
269 | * @throws BadRequestException |
||
270 | */ |
||
271 | protected function decompose($query, array $types, $models = true, $max = null) |
||
328 | |||
329 | /** |
||
330 | * Throw an innocent exception if a player can't see a Model or if it |
||
331 | * doesn't exist |
||
332 | * |
||
333 | * @param $model The model to test |
||
334 | * |
||
335 | * @throws BadRequestException |
||
336 | */ |
||
337 | private function assertVisibility(PermissionModel $model) |
||
343 | } |
||
344 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: