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 bool $need |
||
| 100 | * @return CsvExport |
||
| 101 | * |
||
| 102 | * @deprecated |
||
| 103 | */ |
||
| 104 | public function getExport($need = TRUE) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param bool $need |
||
| 115 | * @return BaseExport[] |
||
| 116 | */ |
||
| 117 | public function getExports($need = TRUE) |
||
| 125 | |||
| 126 | /**********************************************************************************************/ |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param bool $useCache |
||
| 130 | * @return bool |
||
| 131 | * @internal |
||
| 132 | */ |
||
| 133 | public function hasColumns($useCache = TRUE) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param bool $useCache |
||
| 148 | * @return bool |
||
| 149 | * @internal |
||
| 150 | */ |
||
| 151 | public function hasFilters($useCache = TRUE) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param bool $useCache |
||
| 166 | 1 | * @return bool |
|
| 167 | * @internal |
||
| 168 | */ |
||
| 169 | public function hasActions($useCache = TRUE) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param bool $useCache |
||
| 184 | * @return bool |
||
| 185 | * @internal |
||
| 186 | */ |
||
| 187 | public function hasOperation($useCache = TRUE) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param bool $useCache |
||
| 201 | * @return bool |
||
| 202 | * @internal |
||
| 203 | */ |
||
| 204 | public function hasExport($useCache = TRUE) |
||
| 215 | |||
| 216 | /**********************************************************************************************/ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $name |
||
| 220 | * @param string $label |
||
| 221 | * @return Columns\Text |
||
| 222 | */ |
||
| 223 | public function addColumnText($name, $label) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $name |
||
| 230 | * @param string $label |
||
| 231 | * @return Columns\Email |
||
| 232 | */ |
||
| 233 | public function addColumnEmail($name, $label) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param string $name |
||
| 240 | * @param string $label |
||
| 241 | * @return Columns\Link |
||
| 242 | */ |
||
| 243 | public function addColumnLink($name, $label) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param string $name |
||
| 250 | * @param string $label |
||
| 251 | * @param string $dateFormat |
||
| 252 | * @return Columns\Date |
||
| 253 | */ |
||
| 254 | public function addColumnDate($name, $label, $dateFormat = NULL) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param string $name |
||
| 261 | * @param string $label |
||
| 262 | * @param int $decimals number of decimal points |
||
| 263 | * @param string $decPoint separator for the decimal point |
||
| 264 | * @param string $thousandsSep thousands separator |
||
| 265 | * @return Columns\Number |
||
| 266 | */ |
||
| 267 | public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) |
||
| 271 | |||
| 272 | /**********************************************************************************************/ |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $name |
||
| 276 | * @param string $label |
||
| 277 | * @return Filters\Text |
||
| 278 | */ |
||
| 279 | public function addFilterText($name, $label) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param string $name |
||
| 286 | * @param string $label |
||
| 287 | * @return Filters\Date |
||
| 288 | */ |
||
| 289 | public function addFilterDate($name, $label) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $name |
||
| 296 | * @param string $label |
||
| 297 | * @return Filters\DateRange |
||
| 298 | */ |
||
| 299 | public function addFilterDateRange($name, $label) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param string $name |
||
| 306 | * @param string $label |
||
| 307 | * @return Filters\Check |
||
| 308 | */ |
||
| 309 | public function addFilterCheck($name, $label) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param string $name |
||
| 316 | * @param string $label |
||
| 317 | * @param array $items |
||
| 318 | * @return Filters\Select |
||
| 319 | */ |
||
| 320 | public function addFilterSelect($name, $label, array $items = NULL) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $name |
||
| 327 | * @param string $label |
||
| 328 | * @return Filters\Number |
||
| 329 | */ |
||
| 330 | public function addFilterNumber($name, $label) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param string $name |
||
| 337 | * @param \Nette\Forms\IControl $formControl |
||
| 338 | * @return Filters\Custom |
||
| 339 | */ |
||
| 340 | public function addFilterCustom($name, \Nette\Forms\IControl $formControl) |
||
| 344 | |||
| 345 | /**********************************************************************************************/ |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $name |
||
| 349 | * @param string $label |
||
| 350 | * @param string $destination |
||
| 351 | * @param array $arguments |
||
| 352 | * @return Actions\Href |
||
| 353 | */ |
||
| 354 | public function addActionHref($name, $label, $destination = NULL, array $arguments = []) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $name |
||
| 361 | * @param string $label |
||
| 362 | * @param callback $onClick |
||
| 363 | * @return Actions\Event |
||
| 364 | */ |
||
| 365 | public function addActionEvent($name, $label, $onClick = NULL) |
||
| 369 | |||
| 370 | /**********************************************************************************************/ |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param array $operations |
||
| 374 | * @param callback $onSubmit - callback after operation submit |
||
| 375 | * @return Operation |
||
| 376 | */ |
||
| 377 | public function setOperation(array $operations, $onSubmit) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param string $label of exporting file |
||
| 384 | * @return Export |
||
| 385 | * |
||
| 386 | * @deprecated |
||
| 387 | */ |
||
| 388 | public function setExport($label = NULL) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param BaseExport $export |
||
| 395 | * @param string $name Component name |
||
| 396 | * @return BaseExport |
||
| 397 | */ |
||
| 398 | public function addExport(BaseExport $export, $name) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Sets all columns as editable. |
||
| 411 | * First parameter is optional and is for implementation of method for saving modified data. |
||
| 412 | * @param callback $callback function($id, $newValue, $oldValue, Editable $column) {} |
||
| 413 | * @return Grid |
||
| 414 | */ |
||
| 415 | public function setEditableColumns($callback = NULL) |
||
| 431 | } |
||
| 432 |
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: