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 | ||
| 29 | abstract class Container extends \Nette\Application\UI\Control | ||
| 30 | 1 | { | |
| 31 | /** @var bool */ | ||
| 32 | protected $hasColumns; | ||
| 33 | |||
| 34 | /** @var bool */ | ||
| 35 | protected $hasFilters; | ||
| 36 | |||
| 37 | /** @var bool */ | ||
| 38 | protected $hasActions; | ||
| 39 | |||
| 40 | /** @var bool */ | ||
| 41 | protected $hasOperation; | ||
| 42 | |||
| 43 | /** @var bool */ | ||
| 44 | protected $hasExport; | ||
| 45 | |||
| 46 | /** @var bool */ | ||
| 47 | protected $hasButtons; | ||
| 48 | 1 | ||
| 49 | /** | ||
| 50 | * Returns column component. | ||
| 51 | 1 | * @param string $name | |
| 52 | * @param bool $need | ||
| 53 | * @return Editable | ||
| 54 | */ | ||
| 55 | public function getColumn($name, $need = TRUE) | ||
| 61 | |||
| 62 | /** | ||
| 63 | * Returns filter component. | ||
| 64 | * @param string $name | ||
| 65 | * @param bool $need | ||
| 66 | * @return Filter | ||
| 67 | */ | ||
| 68 | public function getFilter($name, $need = TRUE) | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Returns action component. | ||
| 77 | * @param string $name | ||
| 78 | * @param bool $need | ||
| 79 | * @return Action | ||
| 80 | */ | ||
| 81 | public function getAction($name, $need = TRUE) | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Returns operations component. | ||
| 90 | * @param bool $need | ||
| 91 | * @return Operation | ||
| 92 | */ | ||
| 93 | public function getOperation($need = TRUE) | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Returns export component. | ||
| 100 | * @param bool $need | ||
| 101 | * @return Export | ||
| 102 | */ | ||
| 103 | public function getExport($need = TRUE) | ||
| 107 | |||
| 108 | /** | ||
| 109 | * Returns toolbar button component. | ||
| 110 | * @param bool $need | ||
| 111 | * @return Button | ||
| 112 | */ | ||
| 113 | public function getButton($name, $need = TRUE) | ||
| 119 | |||
| 120 | /**********************************************************************************************/ | ||
| 121 | |||
| 122 | /** | ||
| 123 | * @param bool $useCache | ||
| 124 | * @return bool | ||
| 125 | * @internal | ||
| 126 | */ | ||
| 127 | public function hasColumns($useCache = TRUE) | ||
| 139 | |||
| 140 | /** | ||
| 141 | * @param bool $useCache | ||
| 142 | * @return bool | ||
| 143 | * @internal | ||
| 144 | */ | ||
| 145 | public function hasFilters($useCache = TRUE) | ||
| 157 | |||
| 158 | /** | ||
| 159 | * @param bool $useCache | ||
| 160 | * @return bool | ||
| 161 | * @internal | ||
| 162 | */ | ||
| 163 | public function hasActions($useCache = TRUE) | ||
| 175 | |||
| 176 | /** | ||
| 177 | * @param bool $useCache | ||
| 178 | * @return bool | ||
| 179 | * @internal | ||
| 180 | */ | ||
| 181 | public function hasOperation($useCache = TRUE) | ||
| 192 | |||
| 193 | /** | ||
| 194 | * @param bool $useCache | ||
| 195 | * @return bool | ||
| 196 | * @internal | ||
| 197 | */ | ||
| 198 | public function hasExport($useCache = TRUE) | ||
| 209 | |||
| 210 | /** | ||
| 211 | * @param bool $useCache | ||
| 212 | * @return bool | ||
| 213 | * @internal | ||
| 214 | */ | ||
| 215 | public function hasButtons($useCache = TRUE) | ||
| 226 | |||
| 227 | /**********************************************************************************************/ | ||
| 228 | |||
| 229 | /** | ||
| 230 | * @param string $name | ||
| 231 | * @param string $label | ||
| 232 | * @return Columns\Text | ||
| 233 | */ | ||
| 234 | public function addColumnText($name, $label) | ||
| 238 | |||
| 239 | /** | ||
| 240 | * @param string $name | ||
| 241 | * @param string $label | ||
| 242 | * @return Columns\Email | ||
| 243 | */ | ||
| 244 | public function addColumnEmail($name, $label) | ||
| 248 | |||
| 249 | /** | ||
| 250 | * @param string $name | ||
| 251 | * @param string $label | ||
| 252 | * @return Columns\Link | ||
| 253 | */ | ||
| 254 | public function addColumnLink($name, $label) | ||
| 258 | |||
| 259 | /** | ||
| 260 | * @param string $name | ||
| 261 | * @param string $label | ||
| 262 | * @param string $dateFormat | ||
| 263 | * @return Columns\Date | ||
| 264 | */ | ||
| 265 | public function addColumnDate($name, $label, $dateFormat = NULL) | ||
| 269 | |||
| 270 | /** | ||
| 271 | * @param string $name | ||
| 272 | * @param string $label | ||
| 273 | * @param int $decimals number of decimal points | ||
| 274 | * @param string $decPoint separator for the decimal point | ||
| 275 | * @param string $thousandsSep thousands separator | ||
| 276 | * @return Columns\Number | ||
| 277 | */ | ||
| 278 | public function addColumnNumber($name, $label, $decimals = NULL, $decPoint = NULL, $thousandsSep = NULL) | ||
| 282 | |||
| 283 | /**********************************************************************************************/ | ||
| 284 | |||
| 285 | /** | ||
| 286 | * @param string $name | ||
| 287 | * @param string $label | ||
| 288 | * @return Filters\Text | ||
| 289 | */ | ||
| 290 | public function addFilterText($name, $label) | ||
| 294 | |||
| 295 | /** | ||
| 296 | * @param string $name | ||
| 297 | * @param string $label | ||
| 298 | * @return Filters\Date | ||
| 299 | */ | ||
| 300 | public function addFilterDate($name, $label) | ||
| 304 | |||
| 305 | /** | ||
| 306 | * @param string $name | ||
| 307 | * @param string $label | ||
| 308 | * @return Filters\DateRange | ||
| 309 | */ | ||
| 310 | public function addFilterDateRange($name, $label) | ||
| 314 | |||
| 315 | /** | ||
| 316 | * @param string $name | ||
| 317 | * @param string $label | ||
| 318 | * @return Filters\Check | ||
| 319 | */ | ||
| 320 | public function addFilterCheck($name, $label) | ||
| 324 | |||
| 325 | /** | ||
| 326 | * @param string $name | ||
| 327 | * @param string $label | ||
| 328 | * @param array $items | ||
| 329 | * @return Filters\Select | ||
| 330 | */ | ||
| 331 | public function addFilterSelect($name, $label, array $items = NULL) | ||
| 335 | |||
| 336 | /** | ||
| 337 | * @param string $name | ||
| 338 | * @param string $label | ||
| 339 | * @return Filters\Number | ||
| 340 | */ | ||
| 341 | public function addFilterNumber($name, $label) | ||
| 345 | |||
| 346 | /** | ||
| 347 | * @param string $name | ||
| 348 | * @param \Nette\Forms\IControl $formControl | ||
| 349 | * @return Filters\Custom | ||
| 350 | */ | ||
| 351 | public function addFilterCustom($name, \Nette\Forms\IControl $formControl) | ||
| 355 | |||
| 356 | /**********************************************************************************************/ | ||
| 357 | |||
| 358 | /** | ||
| 359 | * @param string $name | ||
| 360 | * @param string $label | ||
| 361 | * @param string $destination | ||
| 362 | * @param array $arguments | ||
| 363 | * @return Actions\Href | ||
| 364 | */ | ||
| 365 | public function addActionHref($name, $label, $destination = NULL, array $arguments = []) | ||
| 369 | |||
| 370 | /** | ||
| 371 | * @param string $name | ||
| 372 | * @param string $label | ||
| 373 | * @param callback $onClick | ||
| 374 | * @return Actions\Event | ||
| 375 | */ | ||
| 376 | public function addActionEvent($name, $label, $onClick = NULL) | ||
| 380 | |||
| 381 | /**********************************************************************************************/ | ||
| 382 | |||
| 383 | /** | ||
| 384 | * @param array $operations | ||
| 385 | * @param callback $onSubmit - callback after operation submit | ||
| 386 | * @return Operation | ||
| 387 | */ | ||
| 388 | public function setOperation(array $operations, $onSubmit) | ||
| 392 | |||
| 393 | /** | ||
| 394 | * @param string $label of exporting file | ||
| 395 | * @return Export | ||
| 396 | */ | ||
| 397 | public function setExport($label = NULL) | ||
| 401 | |||
| 402 | |||
| 403 | /** | ||
| 404 | * @param string $name | ||
| 405 | * @param string $label | ||
| 406 | * @param string $destination - first param for method $presenter->link() | ||
| 407 | * @param array $arguments - second param for method $presenter->link() | ||
| 408 | * @return Button | ||
| 409 | */ | ||
| 410 | public function addButton($name, $label = NULL, $destination = NULL, array $arguments = []) | ||
| 414 | |||
| 415 | /** | ||
| 416 | * Sets all columns as editable. | ||
| 417 | * First parameter is optional and is for implementation of method for saving modified data. | ||
| 418 |      * @param callback $callback function($id, $newValue, $oldValue, Editable $column) {} | ||
| 419 | * @return Grid | ||
| 420 | */ | ||
| 421 | public function setEditableColumns($callback = NULL) | ||
| 437 | } | ||
| 438 | 
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: