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 | const ROLE_ADMIN = 'admin'; |
||
| 25 | const ROLE_GUEST = 'guest'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | public $backlink = ''; |
||
| 31 | |||
| 32 | /** @var Model */ |
||
| 33 | protected $model; |
||
| 34 | |||
| 35 | /** @var Nette\DI\Container */ |
||
| 36 | protected $container; |
||
| 37 | |||
| 38 | /** @var Latte */ |
||
| 39 | protected $latte; |
||
| 40 | |||
| 41 | /** @var Router */ |
||
| 42 | protected $router; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | protected $action; |
||
| 46 | |||
| 47 | /** @var Nette\Http\Request */ |
||
| 48 | protected $request; |
||
| 49 | |||
| 50 | /** @var integer */ |
||
| 51 | protected $meetingId; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var INavbarRightControlFactory |
||
| 55 | */ |
||
| 56 | protected $navbarRightControlFactory; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param INavbarRightControlFactory $factory |
||
| 60 | * @return BasePresenter |
||
| 61 | */ |
||
| 62 | public function injectNavbarRightControlFactory(INavbarRightControlFactory $factory): self |
||
| 63 | { |
||
| 64 | $this->navbarRightControlFactory = $factory; |
||
| 65 | |||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Startup |
||
| 71 | */ |
||
| 72 | protected function startup() |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * Before render |
||
| 103 | * Prepare variables for template |
||
| 104 | */ |
||
| 105 | public function beforeRender() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * template |
||
| 145 | * @var string |
||
| 146 | */ |
||
| 147 | protected $template = 'listing'; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * template directory |
||
| 151 | * @var string |
||
| 152 | */ |
||
| 153 | protected $templateDir = ''; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * category ID |
||
| 157 | * @var integer |
||
| 158 | */ |
||
| 159 | protected $itemId = NULL; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * action what to do |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | protected $cms = ''; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * page where to return |
||
| 169 | * @var string |
||
| 170 | */ |
||
| 171 | protected $page = ''; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * heading tetxt |
||
| 175 | * @var string |
||
| 176 | */ |
||
| 177 | protected $heading = ''; |
||
| 178 | |||
| 179 | /** |
||
| 180 | * action what to do next |
||
| 181 | * @var string |
||
| 182 | */ |
||
| 183 | protected $todo = ''; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * data |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | protected $data = array(); |
||
| 190 | |||
| 191 | /** |
||
| 192 | * error handler |
||
| 193 | * @var string |
||
| 194 | */ |
||
| 195 | protected $error = ''; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * database connection |
||
| 199 | * @var string |
||
| 200 | */ |
||
| 201 | protected $database = NULL; |
||
| 202 | |||
| 203 | /** |
||
| 204 | * debug mode |
||
| 205 | * @var boolean |
||
| 206 | */ |
||
| 207 | protected $debugMode = false; |
||
| 208 | |||
| 209 | protected $sunlight; |
||
| 210 | |||
| 211 | protected function parseTutorEmail($item) |
||
| 229 | |||
| 230 | // zaheshovane udaje, aby se nedali jen tak ziskat data z databaze |
||
| 231 | protected function formKeyHash($id, $meetingId) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return Model |
||
| 238 | */ |
||
| 239 | protected function getModel() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param Model $model |
||
| 246 | * @return $this |
||
| 247 | */ |
||
| 248 | protected function setModel($model) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return Container |
||
| 256 | */ |
||
| 257 | protected function getContainer() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param Container $container |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | protected function setContainer($container) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @return Router |
||
| 274 | */ |
||
| 275 | protected function getRouter() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param Router $router |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | protected function setRouter($router) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @return Latte |
||
| 292 | */ |
||
| 293 | protected function getLatte() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param Latte $latte |
||
| 300 | * @return $this |
||
| 301 | */ |
||
| 302 | protected function setLatte($latte) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getAction($fullyQualified = false) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * @param string $action |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function setAction($action) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return SunlightModel |
||
| 328 | */ |
||
| 329 | public function getSunlight() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param SunlightModel $sunlight |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function setSunlight(SunlightModel $sunlight) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return integer |
||
| 350 | */ |
||
| 351 | protected function getMeetingId() |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param integer $meetingId |
||
| 358 | * @return $this |
||
| 359 | */ |
||
| 360 | protected function setMeetingId($meetingId) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $guid |
||
| 368 | * @param array $data |
||
| 369 | * @return ActiveRow |
||
| 370 | */ |
||
| 371 | protected function updateByGuid($guid, array $data) |
||
| 375 | |||
| 376 | public function remember($key, $minutes, \Closure $callback) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @return NavbarRightControl |
||
| 414 | */ |
||
| 415 | protected function createComponentNavbarRight(): NavbarRightControl |
||
| 419 | |||
| 420 | protected function getCache() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | protected function getDebugMode() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | protected function getBacklink() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param string $backlink |
||
| 443 | * @return $this |
||
| 444 | */ |
||
| 445 | protected function setBacklink($backlink) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | protected function getBacklinkUrl() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param Nette\Utils\ArrayHash $array |
||
| 469 | * @return self |
||
| 470 | */ |
||
| 471 | protected function setBacklinkFromArray(ArrayHash $array): self |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Flashes success message |
||
| 483 | * |
||
| 484 | * @param string $message Message |
||
| 485 | */ |
||
| 486 | protected function flashSuccess(string $message = '') |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Flashes failure message |
||
| 493 | * |
||
| 494 | * @param string $message Message |
||
| 495 | */ |
||
| 496 | protected function flashFailure(string $message = '') |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Flashes error message |
||
| 503 | * |
||
| 504 | * @param string $message Message |
||
| 505 | */ |
||
| 506 | protected function flashError(string $message = '') |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @throws Nette\Application\AbortException |
||
| 513 | */ |
||
| 514 | protected function unauthorized() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @throws Nette\Application\AbortException |
||
| 523 | */ |
||
| 524 | protected function allowAdminAccessOnly() |
||
| 531 | |||
| 532 | } |
||
| 533 |
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: