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 | protected $days = [ |
||
| 52 | 'pátek' => 'pátek', |
||
| 53 | 'sobota' => 'sobota', |
||
| 54 | 'neděle' => 'neděle', |
||
| 55 | ]; |
||
| 56 | |||
| 57 | protected $hours = [ |
||
| 58 | 0 => "00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23" |
||
| 59 | ]; |
||
| 60 | |||
| 61 | protected $minutes = [ |
||
| 62 | 00 => "00", |
||
| 63 | 05 => "05", |
||
| 64 | 10 => "10", |
||
| 65 | 15 => "15", |
||
| 66 | 20 => "20", |
||
| 67 | 25 => "25", |
||
| 68 | 30 => "30", |
||
| 69 | 35 => "35", |
||
| 70 | 40 => "40", |
||
| 71 | 45 => "45", |
||
| 72 | 50 => "50", |
||
| 73 | 55 => "55", |
||
| 74 | ]; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Startup |
||
| 78 | */ |
||
| 79 | protected function startup() |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Before render |
||
| 110 | * Prepare variables for template |
||
| 111 | */ |
||
| 112 | public function beforeRender() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * template |
||
| 152 | * @var string |
||
| 153 | */ |
||
| 154 | protected $template = 'listing'; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * template directory |
||
| 158 | * @var string |
||
| 159 | */ |
||
| 160 | protected $templateDir = ''; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * category ID |
||
| 164 | * @var integer |
||
| 165 | */ |
||
| 166 | protected $itemId = NULL; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * action what to do |
||
| 170 | * @var string |
||
| 171 | */ |
||
| 172 | protected $cms = ''; |
||
| 173 | |||
| 174 | /** |
||
| 175 | * page where to return |
||
| 176 | * @var string |
||
| 177 | */ |
||
| 178 | protected $page = ''; |
||
| 179 | |||
| 180 | /** |
||
| 181 | * heading tetxt |
||
| 182 | * @var string |
||
| 183 | */ |
||
| 184 | protected $heading = ''; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * action what to do next |
||
| 188 | * @var string |
||
| 189 | */ |
||
| 190 | protected $todo = ''; |
||
| 191 | |||
| 192 | /** |
||
| 193 | * data |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | protected $data = array(); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * error handler |
||
| 200 | * @var string |
||
| 201 | */ |
||
| 202 | protected $error = ''; |
||
| 203 | |||
| 204 | /** |
||
| 205 | * database connection |
||
| 206 | * @var string |
||
| 207 | */ |
||
| 208 | protected $database = NULL; |
||
| 209 | |||
| 210 | /** |
||
| 211 | * debug mode |
||
| 212 | * @var boolean |
||
| 213 | */ |
||
| 214 | protected $debugMode = false; |
||
| 215 | |||
| 216 | protected $sunlight; |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Render check box |
||
| 220 | * |
||
| 221 | * @param string name |
||
| 222 | * @param mixed value |
||
| 223 | * @param var variable that match with value |
||
| 224 | * @return string html of chceck box |
||
| 225 | */ |
||
| 226 | protected function renderHtmlCheckBox($name, $value, $checked_variable) |
||
| 237 | |||
| 238 | protected function parseTutorEmail($item) |
||
| 253 | |||
| 254 | // zaheshovane udaje, aby se nedali jen tak ziskat data z databaze |
||
| 255 | protected function formKeyHash($id, $meetingId) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return Model |
||
| 262 | */ |
||
| 263 | protected function getModel() |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param Model $model |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | protected function setModel($model) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return Container |
||
| 280 | */ |
||
| 281 | protected function getContainer() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param Container $container |
||
| 288 | * @return $this |
||
| 289 | */ |
||
| 290 | protected function setContainer($container) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @return Router |
||
| 298 | */ |
||
| 299 | protected function getRouter() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param Router $router |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | protected function setRouter($router) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return Latte |
||
| 316 | */ |
||
| 317 | protected function getLatte() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param Latte $latte |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | protected function setLatte($latte) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | public function getAction($fullyQualified = false) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $action |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function setAction($action) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return SunlightModel |
||
| 352 | */ |
||
| 353 | public function getSunlight() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param SunlightModel $sunlight |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function setSunlight(SunlightModel $sunlight) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return integer |
||
| 374 | */ |
||
| 375 | protected function getMeetingId() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @param integer $meetingId |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | protected function setMeetingId($meetingId) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $guid |
||
| 392 | * @param array $data |
||
| 393 | * @return ActiveRow |
||
| 394 | */ |
||
| 395 | protected function updateByGuid($guid, array $data) |
||
| 399 | |||
| 400 | public function remember($key, $minutes, \Closure $callback) |
||
| 435 | |||
| 436 | protected function getCache() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | protected function getDebugMode() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | protected function getBacklink() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param string $backlink |
||
| 459 | * @return $this |
||
| 460 | */ |
||
| 461 | protected function setBacklink($backlink) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | protected function getBacklinkUrl() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param Nette\Utils\ArrayHash $array |
||
| 485 | * @return self |
||
| 486 | */ |
||
| 487 | protected function setBacklinkFromArray(ArrayHash $array): self |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Flashes success message |
||
| 499 | * |
||
| 500 | * @param string $message Message |
||
| 501 | */ |
||
| 502 | protected function flashSuccess(string $message = '') |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Flashes failure message |
||
| 509 | * |
||
| 510 | * @param string $message Message |
||
| 511 | */ |
||
| 512 | protected function flashFailure(string $message = '') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Flashes error message |
||
| 519 | * |
||
| 520 | * @param string $message Message |
||
| 521 | */ |
||
| 522 | protected function flashError(string $message = '') |
||
| 526 | |||
| 527 | } |
||
| 528 |
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: