Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | abstract class Container extends \Nette\Application\UI\Control |
||
32 | 1 | { |
|
33 | /** @var bool */ |
||
34 | protected $hasColumns; |
||
35 | |||
36 | /** @var bool */ |
||
37 | protected $hasFilters; |
||
38 | |||
39 | /** @var bool */ |
||
40 | protected $hasActions; |
||
41 | |||
42 | /** @var bool */ |
||
43 | protected $hasOperation; |
||
44 | |||
45 | /** @var bool */ |
||
46 | protected $hasExport; |
||
47 | |||
48 | 1 | /** |
|
49 | * Returns column component. |
||
50 | * @param string $name |
||
51 | 1 | * @param bool $need |
|
52 | * @return Editable |
||
53 | */ |
||
54 | public function getColumn($name, $need = TRUE) |
||
60 | |||
61 | /** |
||
62 | * Returns filter component. |
||
63 | * @param string $name |
||
64 | * @param bool $need |
||
65 | * @return Filter |
||
66 | */ |
||
67 | public function getFilter($name, $need = TRUE) |
||
73 | |||
74 | /** |
||
75 | * Returns action component. |
||
76 | * @param string $name |
||
77 | * @param bool $need |
||
78 | * @return Action |
||
79 | */ |
||
80 | public function getAction($name, $need = TRUE) |
||
86 | |||
87 | /** |
||
88 | * Returns operations component. |
||
89 | * @param bool $need |
||
90 | * @return Operation |
||
91 | */ |
||
92 | public function getOperation($need = TRUE) |
||
96 | |||
97 | /** |
||
98 | * Returns export component. |
||
99 | * @param string $name |
||
100 | * @param bool $need |
||
101 | * @return CsvExport |
||
102 | */ |
||
103 | public function getExport($name, $need = TRUE) |
||
117 | |||
118 | /** |
||
119 | * @param bool $need |
||
120 | * @return BaseExport[] |
||
121 | */ |
||
122 | public function getExports($need = TRUE) |
||
130 | |||
131 | /**********************************************************************************************/ |
||
132 | |||
133 | /** |
||
134 | * @param bool $useCache |
||
135 | * @return bool |
||
136 | * @internal |
||
137 | */ |
||
138 | public function hasColumns($useCache = TRUE) |
||
150 | |||
151 | /** |
||
152 | * @param bool $useCache |
||
153 | * @return bool |
||
154 | * @internal |
||
155 | */ |
||
156 | public function hasFilters($useCache = TRUE) |
||
168 | |||
169 | /** |
||
170 | * @param bool $useCache |
||
171 | * @return bool |
||
172 | * @internal |
||
173 | */ |
||
174 | public function hasActions($useCache = TRUE) |
||
186 | |||
187 | /** |
||
188 | * @param bool $useCache |
||
189 | * @return bool |
||
190 | * @internal |
||
191 | */ |
||
192 | public function hasOperation($useCache = TRUE) |
||
203 | |||
204 | /** |
||
205 | * @param bool $useCache |
||
206 | * @return bool |
||
207 | * @internal |
||
208 | */ |
||
209 | public function hasExport($useCache = TRUE) |
||
220 | |||
221 | /**********************************************************************************************/ |
||
222 | |||
223 | /** |
||
224 | * @param string $name |
||
225 | * @param string $label |
||
226 | * @return Columns\Text |
||
227 | */ |
||
228 | public function addColumnText($name, $label) |
||
232 | |||
233 | /** |
||
234 | * @param string $name |
||
235 | * @param string $label |
||
236 | * @return Columns\Email |
||
237 | */ |
||
238 | public function addColumnEmail($name, $label) |
||
242 | |||
243 | /** |
||
244 | * @param string $name |
||
245 | * @param string $label |
||
246 | * @return Columns\Link |
||
247 | */ |
||
248 | public function addColumnLink($name, $label) |
||
252 | |||
253 | /** |
||
254 | * @param string $name |
||
255 | * @param string $label |
||
256 | * @param string $dateFormat |
||
257 | * @return Columns\Date |
||
258 | */ |
||
259 | public function addColumnDate($name, $label, $dateFormat = NULL) |
||
263 | |||
264 | /** |
||
265 | * @param string $name |
||
266 | * @param string $label |
||
267 | * @param int $decimals number of decimal points |
||
268 | * @param string $decPoint separator for the decimal point |
||
269 | * @param string $thousandsSep thousands separator |
||
270 | * @return Columns\Number |
||
271 | */ |
||
272 | public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) |
||
276 | |||
277 | /**********************************************************************************************/ |
||
278 | |||
279 | /** |
||
280 | * @param string $name |
||
281 | * @param string $label |
||
282 | * @return Filters\Text |
||
283 | */ |
||
284 | public function addFilterText($name, $label) |
||
288 | |||
289 | /** |
||
290 | * @param string $name |
||
291 | * @param string $label |
||
292 | * @return Filters\Date |
||
293 | */ |
||
294 | public function addFilterDate($name, $label) |
||
298 | |||
299 | /** |
||
300 | * @param string $name |
||
301 | * @param string $label |
||
302 | * @return Filters\DateRange |
||
303 | */ |
||
304 | public function addFilterDateRange($name, $label) |
||
308 | |||
309 | /** |
||
310 | * @param string $name |
||
311 | * @param string $label |
||
312 | * @return Filters\Check |
||
313 | */ |
||
314 | public function addFilterCheck($name, $label) |
||
318 | |||
319 | /** |
||
320 | * @param string $name |
||
321 | * @param string $label |
||
322 | * @param array $items |
||
323 | * @return Filters\Select |
||
324 | */ |
||
325 | public function addFilterSelect($name, $label, array $items = NULL) |
||
329 | |||
330 | /** |
||
331 | * @param string $name |
||
332 | * @param string $label |
||
333 | * @return Filters\Number |
||
334 | */ |
||
335 | public function addFilterNumber($name, $label) |
||
339 | |||
340 | /** |
||
341 | * @param string $name |
||
342 | * @param \Nette\Forms\IControl $formControl |
||
343 | * @return Filters\Custom |
||
344 | */ |
||
345 | public function addFilterCustom($name, \Nette\Forms\IControl $formControl) |
||
349 | |||
350 | /**********************************************************************************************/ |
||
351 | |||
352 | /** |
||
353 | * @param string $name |
||
354 | * @param string $label |
||
355 | * @param string $destination |
||
356 | * @param array $arguments |
||
357 | * @return Actions\Href |
||
358 | */ |
||
359 | public function addActionHref($name, $label, $destination = NULL, array $arguments = []) |
||
363 | |||
364 | /** |
||
365 | * @param string $name |
||
366 | * @param string $label |
||
367 | * @param callback $onClick |
||
368 | * @return Actions\Event |
||
369 | */ |
||
370 | public function addActionEvent($name, $label, $onClick = NULL) |
||
374 | |||
375 | /**********************************************************************************************/ |
||
376 | |||
377 | /** |
||
378 | * @param array $operations |
||
379 | * @param callback $onSubmit - callback after operation submit |
||
380 | * @return Operation |
||
381 | */ |
||
382 | public function setOperation(array $operations, $onSubmit) |
||
386 | |||
387 | /** |
||
388 | * @param string $label of exporting file |
||
389 | * @return Export |
||
390 | * |
||
391 | * @deprecated |
||
392 | */ |
||
393 | public function setExport($label = NULL) |
||
397 | |||
398 | /** |
||
399 | * @param BaseExport $export |
||
400 | * @param string $name Component name |
||
401 | * @return BaseExport |
||
402 | */ |
||
403 | public function addExport(BaseExport $export, $name) |
||
413 | |||
414 | /** |
||
415 | * Sets all columns as editable. |
||
416 | * First parameter is optional and is for implementation of method for saving modified data. |
||
417 | * @param callback $callback function($id, $newValue, $oldValue, Editable $column) {} |
||
418 | * @return Grid |
||
419 | */ |
||
420 | public function setEditableColumns($callback = NULL) |
||
436 | } |
||
437 |
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: