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 |
||
11 | abstract class HTMLController extends Controller |
||
12 | { |
||
13 | /** |
||
14 | * Whether twig has been prepared |
||
15 | * @var bool |
||
16 | */ |
||
17 | public $twigReady = false; |
||
18 | |||
19 | /** |
||
20 | * Prepare the twig global variables |
||
21 | */ |
||
22 | 1 | private function addTwigGlobals() |
|
43 | |||
44 | 1 | protected function prepareTwig() |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | * @param string $view |
||
51 | */ |
||
52 | 1 | protected function render($view, $parameters = array()) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | * |
||
62 | * @throws ModelNotFoundException |
||
63 | */ |
||
64 | 1 | protected function findModelInParameters($modelParameter, $routeParameters) |
|
84 | |||
85 | 1 | /** |
|
86 | * {@inheritdoc} |
||
87 | 1 | */ |
|
88 | 1 | public function callAction($action = null) |
|
99 | |||
100 | /** |
||
101 | 1 | * Save the URL of the current page so that the user can be redirected back to it |
|
102 | * if they login |
||
103 | 1 | */ |
|
104 | protected function saveURL() |
||
119 | |||
120 | /** |
||
121 | 1 | * Returns the path to the home page |
|
122 | * @return string |
||
123 | 1 | */ |
|
124 | protected function getHomeURL() |
||
128 | |||
129 | /** |
||
130 | 1 | * Returns the URL of the previous page |
|
131 | * @return string |
||
132 | 1 | */ |
|
133 | protected function getPreviousURL() |
||
148 | |||
149 | /** |
||
150 | * Returns a redirect response to the previous page |
||
151 | * @return RedirectResponse |
||
152 | */ |
||
153 | protected function goBack() |
||
157 | |||
158 | /** |
||
159 | 1 | * Returns a redirect response to the home page |
|
160 | * @return RedirectResponse |
||
161 | 1 | */ |
|
162 | protected function goHome() |
||
166 | |||
167 | /** |
||
168 | 1 | * Get the current pagination value |
|
169 | * |
||
170 | 1 | * @return int Always a positive value greater than 0 |
|
171 | */ |
||
172 | protected function getCurrentPage() |
||
178 | |||
179 | /** |
||
180 | * Get the session's flash bag |
||
181 | * @return Symfony\Component\HttpFoundation\Session\Flash\FlashBag |
||
182 | */ |
||
183 | 1 | public static function getFlashBag() |
|
187 | |||
188 | /** |
||
189 | 1 | * Find out whether the currently logged in user can see a model |
|
190 | * |
||
191 | * Apart from the permissions of the user, this method takes the request |
||
192 | * query into consideration to find out if the user wants to see deleted |
||
193 | * models or not. |
||
194 | * |
||
195 | * @param Model $model Model in question |
||
196 | * @param bool $restoring If we're attempting to restore the object, we should always be able to see it |
||
197 | * @return bool |
||
198 | 1 | */ |
|
199 | public static function canSee($model, $restoring = false) |
||
207 | |||
208 | /** |
||
209 | * Assert that the user is logged in |
||
210 | * @param string $message The message to show if the user is not logged in |
||
211 | * @throws HTTPException |
||
212 | * @return void |
||
213 | */ |
||
214 | protected function requireLogin( |
||
221 | |||
222 | /** |
||
223 | * Show a confirmation (Yes, No) form to the user |
||
224 | * |
||
225 | 1 | * @param callable $onYes What to do if the user clicks on "Yes" |
|
226 | * @param string $message The message to show to the user, asking them to confirm their action (RAW text |
||
227 | * is shown - don't forget to properly escape your parameters!) |
||
228 | * @param string $action The text to show on the "Yes" button |
||
229 | * @param string $successMessage A message to add on the session's flashbag on success (flashbags automatically escape text) |
||
230 | * @param callable $onNo What to do if the user presses "No" - defaults to redirecting them back |
||
231 | * @param string $view The view to redirect to |
||
232 | * @param bool $noButton Whether to show a "No" instead of a "Cancel" button |
||
233 | * @param bool $destructive Whether or not to mark the action button as being a destructive operation |
||
234 | 1 | * |
|
235 | 1 | * @return mixed The response |
|
236 | */ |
||
237 | 1 | protected function showConfirmationForm( |
|
272 | |||
273 | /** |
||
274 | * Decompose a list of object IDs into the corresponding IDs |
||
275 | * |
||
276 | * @param string $query The user's query |
||
277 | * @param array $types A list of the acceptable model types (will NOT be sanitized) |
||
278 | * @param bool $models Whether to return an array of models instead of an array of IDs |
||
279 | * @param int|null $max The largest number of models to accept, or null for infinite models |
||
280 | * |
||
281 | * @throws BadRequestException |
||
282 | * @return int[]|Model[] An array of models or IDs (depending on the $models parameter) |
||
283 | */ |
||
284 | protected function decompose($query, array $types, $models = true, $max = null) |
||
341 | |||
342 | /** |
||
343 | * Throw an innocent exception if a player can't see a Model or if it |
||
344 | * doesn't exist |
||
345 | * |
||
346 | * @param PermissionModel $model The model to test |
||
347 | * |
||
348 | * @throws BadRequestException |
||
349 | */ |
||
350 | private function assertVisibility(PermissionModel $model) |
||
356 | } |
||
357 |