Complex classes like BasePresenter 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 BasePresenter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | abstract class BasePresenter extends Nette\Application\UI\Presenter |
||
| 18 | { |
||
| 19 | |||
| 20 | use Loggable; |
||
| 21 | |||
| 22 | const FLASH_TYPE_OK = 'success'; |
||
| 23 | const FLASH_TYPE_ERROR = 'error'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public $backlink = ''; |
||
| 29 | |||
| 30 | /** @var Model */ |
||
| 31 | protected $model; |
||
| 32 | |||
| 33 | /** @var Nette\DI\Container */ |
||
| 34 | protected $container; |
||
| 35 | |||
| 36 | /** @var Latte */ |
||
| 37 | protected $latte; |
||
| 38 | |||
| 39 | /** @var Router */ |
||
| 40 | protected $router; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | protected $action; |
||
| 44 | |||
| 45 | /** @var Nette\Http\Request */ |
||
| 46 | protected $request; |
||
| 47 | |||
| 48 | /** @var integer */ |
||
| 49 | protected $meetingId; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Startup |
||
| 53 | */ |
||
| 54 | protected function startup() |
||
| 81 | |||
| 82 | |||
| 83 | /** |
||
| 84 | * Before render |
||
| 85 | * Prepare variables for template |
||
| 86 | */ |
||
| 87 | public function beforeRender() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * template |
||
| 127 | * @var string |
||
| 128 | */ |
||
| 129 | protected $template = 'listing'; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * template directory |
||
| 133 | * @var string |
||
| 134 | */ |
||
| 135 | protected $templateDir = ''; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * category ID |
||
| 139 | * @var integer |
||
| 140 | */ |
||
| 141 | protected $itemId = NULL; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * action what to do |
||
| 145 | * @var string |
||
| 146 | */ |
||
| 147 | protected $cms = ''; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * page where to return |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $page = ''; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * heading tetxt |
||
| 157 | * @var string |
||
| 158 | */ |
||
| 159 | protected $heading = ''; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * action what to do next |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | protected $todo = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * data |
||
| 169 | * @var array |
||
| 170 | */ |
||
| 171 | protected $data = array(); |
||
| 172 | |||
| 173 | /** |
||
| 174 | * error handler |
||
| 175 | * @var string |
||
| 176 | */ |
||
| 177 | protected $error = ''; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * database connection |
||
| 181 | * @var string |
||
| 182 | */ |
||
| 183 | protected $database = NULL; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * debug mode |
||
| 187 | * @var boolean |
||
| 188 | */ |
||
| 189 | protected $debugMode = false; |
||
| 190 | |||
| 191 | protected $sunlight; |
||
| 192 | |||
| 193 | protected function parseTutorEmail($item) |
||
| 211 | |||
| 212 | // zaheshovane udaje, aby se nedali jen tak ziskat data z databaze |
||
| 213 | protected function formKeyHash($id, $meetingId) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return Model |
||
| 220 | */ |
||
| 221 | protected function getModel() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param Model $model |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | protected function setModel($model) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return Container |
||
| 238 | */ |
||
| 239 | protected function getContainer() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param Container $container |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | protected function setContainer($container) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return Router |
||
| 256 | */ |
||
| 257 | protected function getRouter() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param Router $router |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | protected function setRouter($router) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return Latte |
||
| 274 | */ |
||
| 275 | protected function getLatte() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Latte $latte |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | protected function setLatte($latte) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return string |
||
| 292 | */ |
||
| 293 | public function getAction($fullyQualified = false) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $action |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | public function setAction($action) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return SunlightModel |
||
| 310 | */ |
||
| 311 | public function getSunlight() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param SunlightModel $sunlight |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setSunlight(SunlightModel $sunlight) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @return integer |
||
| 332 | */ |
||
| 333 | protected function getMeetingId() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param integer $meetingId |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | protected function setMeetingId($meetingId) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $guid |
||
| 350 | * @param array $data |
||
| 351 | * @return ActiveRow |
||
| 352 | */ |
||
| 353 | protected function updateByGuid($guid, array $data) |
||
| 357 | |||
| 358 | public function remember($key, $minutes, \Closure $callback) |
||
| 393 | |||
| 394 | protected function getCache() |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | protected function getDebugMode() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | protected function getBacklink() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $backlink |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | protected function setBacklink($backlink) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | protected function getBacklinkUrl() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param Nette\Utils\ArrayHash $array |
||
| 443 | * @return self |
||
| 444 | */ |
||
| 445 | protected function setBacklinkFromArray(ArrayHash $array): self |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Flashes success message |
||
| 457 | * |
||
| 458 | * @param string $message Message |
||
| 459 | */ |
||
| 460 | protected function flashSuccess(string $message = '') |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Flashes failure message |
||
| 467 | * |
||
| 468 | * @param string $message Message |
||
| 469 | */ |
||
| 470 | protected function flashFailure(string $message = '') |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Flashes error message |
||
| 477 | * |
||
| 478 | * @param string $message Message |
||
| 479 | */ |
||
| 480 | protected function flashError(string $message = '') |
||
| 484 | |||
| 485 | } |
||
| 486 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: