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 | { |
||
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 | 1 | 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 | 1 | 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 | 1 | public function getAction($name, $need = TRUE) |
|
86 | |||
87 | /** |
||
88 | * Returns operations component. |
||
89 | * @param bool $need |
||
90 | * @return Operation |
||
91 | */ |
||
92 | 1 | public function getOperation($need = TRUE) |
|
96 | |||
97 | /** |
||
98 | * Returns export component. |
||
99 | * @param string $name |
||
100 | * @param bool $need |
||
101 | * @return CsvExport |
||
102 | 1 | */ |
|
103 | public function getExport($name = NULL, $need = TRUE) |
||
118 | 1 | ||
119 | 1 | /** |
|
120 | 1 | * @param bool $need |
|
121 | * @return BaseExport[] |
||
122 | 1 | */ |
|
123 | public function getExports($need = TRUE) |
||
131 | |||
132 | 1 | /**********************************************************************************************/ |
|
133 | |||
134 | 1 | /** |
|
135 | 1 | * @param bool $useCache |
|
136 | 1 | * @return bool |
|
137 | 1 | * @internal |
|
138 | 1 | */ |
|
139 | public function hasColumns($useCache = TRUE) |
||
151 | |||
152 | 1 | /** |
|
153 | 1 | * @param bool $useCache |
|
154 | 1 | * @return bool |
|
155 | 1 | * @internal |
|
156 | 1 | */ |
|
157 | public function hasFilters($useCache = TRUE) |
||
169 | |||
170 | 1 | /** |
|
171 | 1 | * @param bool $useCache |
|
172 | 1 | * @return bool |
|
173 | 1 | * @internal |
|
174 | */ |
||
175 | 1 | public function hasActions($useCache = TRUE) |
|
187 | 1 | ||
188 | 1 | /** |
|
189 | 1 | * @param bool $useCache |
|
190 | 1 | * @return bool |
|
191 | * @internal |
||
192 | 1 | */ |
|
193 | public function hasOperation($useCache = TRUE) |
||
204 | 1 | ||
205 | /** |
||
206 | * @param bool $useCache |
||
207 | * @return bool |
||
208 | * @internal |
||
209 | */ |
||
210 | public function hasExport($useCache = TRUE) |
||
221 | |||
222 | /**********************************************************************************************/ |
||
223 | |||
224 | 1 | /** |
|
225 | * @param string $name |
||
226 | * @param string $label |
||
227 | * @return Columns\Text |
||
228 | */ |
||
229 | public function addColumnText($name, $label) |
||
233 | |||
234 | /** |
||
235 | 1 | * @param string $name |
|
236 | * @param string $label |
||
237 | * @return Columns\Email |
||
238 | */ |
||
239 | public function addColumnEmail($name, $label) |
||
243 | |||
244 | /** |
||
245 | * @param string $name |
||
246 | * @param string $label |
||
247 | * @return Columns\Link |
||
248 | 1 | */ |
|
249 | public function addColumnLink($name, $label) |
||
253 | |||
254 | /** |
||
255 | * @param string $name |
||
256 | * @param string $label |
||
257 | * @param string $dateFormat |
||
258 | * @return Columns\Date |
||
259 | */ |
||
260 | 1 | public function addColumnDate($name, $label, $dateFormat = NULL) |
|
264 | |||
265 | /** |
||
266 | * @param string $name |
||
267 | * @param string $label |
||
268 | * @param int $decimals number of decimal points |
||
269 | * @param string $decPoint separator for the decimal point |
||
270 | 1 | * @param string $thousandsSep thousands separator |
|
271 | * @return Columns\Number |
||
272 | */ |
||
273 | public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) |
||
277 | |||
278 | /**********************************************************************************************/ |
||
279 | |||
280 | 1 | /** |
|
281 | * @param string $name |
||
282 | * @param string $label |
||
283 | * @return Filters\Text |
||
284 | */ |
||
285 | public function addFilterText($name, $label) |
||
289 | |||
290 | 1 | /** |
|
291 | * @param string $name |
||
292 | * @param string $label |
||
293 | * @return Filters\Date |
||
294 | */ |
||
295 | public function addFilterDate($name, $label) |
||
299 | |||
300 | /** |
||
301 | 1 | * @param string $name |
|
302 | * @param string $label |
||
303 | * @return Filters\DateRange |
||
304 | */ |
||
305 | public function addFilterDateRange($name, $label) |
||
309 | |||
310 | /** |
||
311 | 1 | * @param string $name |
|
312 | * @param string $label |
||
313 | * @return Filters\Check |
||
314 | */ |
||
315 | public function addFilterCheck($name, $label) |
||
319 | |||
320 | /** |
||
321 | 1 | * @param string $name |
|
322 | * @param string $label |
||
323 | * @param array $items |
||
324 | * @return Filters\Select |
||
325 | */ |
||
326 | public function addFilterSelect($name, $label, array $items = NULL) |
||
330 | |||
331 | /** |
||
332 | * @param string $name |
||
333 | * @param string $label |
||
334 | * @return Filters\Number |
||
335 | 1 | */ |
|
336 | public function addFilterNumber($name, $label) |
||
340 | |||
341 | /** |
||
342 | * @param string $name |
||
343 | * @param \Nette\Forms\IControl $formControl |
||
344 | * @return Filters\Custom |
||
345 | */ |
||
346 | 1 | public function addFilterCustom($name, \Nette\Forms\IControl $formControl) |
|
350 | |||
351 | /**********************************************************************************************/ |
||
352 | |||
353 | /** |
||
354 | * @param string $name |
||
355 | * @param string $label |
||
356 | * @param string $destination |
||
357 | * @param array $arguments |
||
358 | 1 | * @return Actions\Href |
|
359 | */ |
||
360 | public function addActionHref($name, $label, $destination = NULL, array $arguments = []) |
||
364 | |||
365 | /** |
||
366 | * @param string $name |
||
367 | 1 | * @param string $label |
|
368 | * @param callback $onClick |
||
369 | * @return Actions\Event |
||
370 | */ |
||
371 | public function addActionEvent($name, $label, $onClick = NULL) |
||
375 | |||
376 | /**********************************************************************************************/ |
||
377 | |||
378 | 1 | /** |
|
379 | 1 | * @param array $operations |
|
380 | * @param callback $onSubmit - callback after operation submit |
||
381 | * @return Operation |
||
382 | */ |
||
383 | 1 | public function setOperation(array $operations, $onSubmit) |
|
387 | 1 | ||
388 | 1 | /** |
|
389 | * @param string $label of exporting file |
||
390 | 1 | * @return Export |
|
391 | * |
||
392 | * @deprecated |
||
393 | 1 | */ |
|
394 | public function setExport($label = NULL) |
||
399 | |||
400 | /** |
||
401 | * @param BaseExport $export |
||
402 | * @param string $name Component name |
||
403 | * @return BaseExport |
||
404 | */ |
||
405 | public function addExport(BaseExport $export, $name) |
||
415 | |||
416 | /** |
||
417 | * Sets all columns as editable. |
||
418 | * First parameter is optional and is for implementation of method for saving modified data. |
||
419 | * @param callback $callback function($id, $newValue, $oldValue, Editable $column) {} |
||
420 | * @return Grid |
||
421 | */ |
||
422 | public function setEditableColumns($callback = NULL) |
||
438 | } |
||
439 |
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: