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 | |||
| 49 | /** |
||
| 50 | * Returns column component. |
||
| 51 | * @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) |
||
| 82 | 1 | { |
|
| 83 | return $this->hasActions() |
||
| 84 | ? $this->getComponent(Action::ID)->getComponent($name, $need) |
||
| 85 | : NULL; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Returns operations component. |
||
| 90 | * @param bool $need |
||
| 91 | * @return Operation |
||
| 92 | */ |
||
| 93 | public function getOperation($need = TRUE) |
||
| 94 | { |
||
| 95 | return $this->getComponent(Operation::ID, $need); |
||
| 96 | } |
||
| 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) |
||
| 114 | { |
||
| 115 | return $this->hasButtons() |
||
| 116 | ? $this->getComponent(Button::ID)->getComponent($name, $need) |
||
| 117 | : NULL; |
||
| 118 | } |
||
| 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) |
||
| 164 | { |
||
| 165 | $hasActions = $this->hasActions; |
||
| 166 | |||
| 167 | if ($hasActions === NULL || $useCache === FALSE) { |
||
| 168 | $container = $this->getComponent(Action::ID, FALSE); |
||
| 169 | $hasActions = $container && count($container->getComponents()) > 0; |
||
| 170 | 1 | $this->hasActions = $useCache ? $hasActions : NULL; |
|
| 171 | } |
||
| 172 | |||
| 173 | return $hasActions; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param bool $useCache |
||
| 178 | * @return bool |
||
| 179 | * @internal |
||
| 180 | */ |
||
| 181 | public function hasOperation($useCache = TRUE) |
||
| 182 | { |
||
| 183 | $hasOperation = $this->hasOperation; |
||
| 184 | |||
| 185 | if ($hasOperation === NULL || $useCache === FALSE) { |
||
| 186 | $hasOperation = (bool) $this->getComponent(Operation::ID, FALSE); |
||
| 187 | $this->hasOperation = $useCache ? $hasOperation : NULL; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $hasOperation; |
||
| 191 | } |
||
| 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) |
||
| 216 | { |
||
| 217 | $hasButtons = $this->hasButtons; |
||
| 218 | |||
| 219 | if ($hasButtons === NULL || $useCache === FALSE) { |
||
| 220 | $hasButtons = (bool) $this->getComponent(Button::ID, FALSE); |
||
| 221 | $this->hasButtons = $useCache ? $hasButtons : NULL; |
||
| 222 | } |
||
| 223 | |||
| 224 | return $hasButtons; |
||
| 225 | } |
||
| 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 = []) |
||
| 366 | { |
||
| 367 | return new Actions\Href($this, $name, $label, $destination, $arguments); |
||
| 368 | } |
||
| 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) |
||
| 377 | { |
||
| 378 | return new Actions\Event($this, $name, $label, $onClick); |
||
| 379 | } |
||
| 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) |
||
| 389 | { |
||
| 390 | return new Operation($this, $operations, $onSubmit); |
||
| 391 | } |
||
| 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: