Complex classes like Page 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 Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Page { |
||
| 15 | use |
||
| 16 | Singleton, |
||
| 17 | Includes; |
||
| 18 | const INIT_STATE_METHOD = 'init'; |
||
| 19 | /** |
||
| 20 | * Complete page contents |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $Content; |
||
| 25 | /** |
||
| 26 | * If `false` - only page content will be shown, without the rest of interface (useful for AJAX request, though for API it is set to `false` automatically) |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | public $interface; |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | public $pre_Html; |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | public $Html; |
||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public $Description; |
||
| 43 | /** |
||
| 44 | * @var string|string[] |
||
| 45 | */ |
||
| 46 | public $Title; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | public $Head; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $pre_Body; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $Left; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $Top; |
||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $Right; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $Bottom; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $post_Body; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $post_Html; |
||
| 79 | /** |
||
| 80 | * Number of tabs by default for indentation the substitution of values into template |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $level; |
||
| 85 | /** |
||
| 86 | * @var array[] |
||
| 87 | */ |
||
| 88 | protected $link; |
||
| 89 | /** |
||
| 90 | * @var string[] |
||
| 91 | */ |
||
| 92 | protected $search_replace; |
||
| 93 | /** |
||
| 94 | * @var false|string |
||
| 95 | */ |
||
| 96 | protected $canonical_url; |
||
| 97 | protected $theme; |
||
| 98 | protected $finish_called_once; |
||
| 99 | /** |
||
| 100 | * @param string $property |
||
| 101 | * |
||
| 102 | * @return false|null|string |
||
| 103 | */ |
||
| 104 | function __get ($property) { |
||
| 163 | /** |
||
| 164 | * Initialization: setting of title and theme according to system configuration |
||
| 165 | * |
||
| 166 | * @todo Probably, make public and add resetting here in order to avoid complete object recreation |
||
| 167 | * |
||
| 168 | * @return Page |
||
| 169 | */ |
||
| 170 | protected function init_config () { |
||
| 176 | /** |
||
| 177 | * Theme changing |
||
| 178 | * |
||
| 179 | * @param string $theme |
||
| 180 | * |
||
| 181 | * @return Page |
||
| 182 | */ |
||
| 183 | function set_theme ($theme) { |
||
| 187 | /** |
||
| 188 | * Adding of content on the page |
||
| 189 | * |
||
| 190 | * @param string $add |
||
| 191 | * @param bool|int $level |
||
| 192 | * |
||
| 193 | * @return Page |
||
| 194 | */ |
||
| 195 | function content ($add, $level = false) { |
||
| 203 | /** |
||
| 204 | * Sets body with content, that is transformed into JSON format |
||
| 205 | * |
||
| 206 | * @param mixed $add |
||
| 207 | * |
||
| 208 | * @return Page |
||
| 209 | */ |
||
| 210 | function json ($add) { |
||
| 216 | /** |
||
| 217 | * Loading of theme template |
||
| 218 | */ |
||
| 219 | protected function get_template () { |
||
| 232 | /** |
||
| 233 | * Processing of template, substituting of content, preparing for the output |
||
| 234 | * |
||
| 235 | * @return Page |
||
| 236 | */ |
||
| 237 | protected function prepare () { |
||
| 327 | /** |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | protected function get_favicon_path () { |
||
| 337 | /** |
||
| 338 | * @param string $property |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | protected function get_property_with_indentation ($property) { |
||
| 345 | /** |
||
| 346 | * Replacing anything in source code of finally generated page |
||
| 347 | * |
||
| 348 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 349 | * |
||
| 350 | * @param string|string[] $search |
||
| 351 | * @param string|string[] $replace |
||
| 352 | * |
||
| 353 | * @return Page |
||
| 354 | */ |
||
| 355 | function replace ($search, $replace = '') { |
||
| 364 | /** |
||
| 365 | * Processing of replacing in content |
||
| 366 | * |
||
| 367 | * @param string $content |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | protected function process_replacing ($content) { |
||
| 378 | /** |
||
| 379 | * Adding links |
||
| 380 | * |
||
| 381 | * @param array $data According to h class syntax |
||
| 382 | * |
||
| 383 | * @return Page |
||
| 384 | */ |
||
| 385 | function link ($data) { |
||
| 391 | /** |
||
| 392 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 393 | * |
||
| 394 | * @param string $href |
||
| 395 | * @param string $title |
||
| 396 | * |
||
| 397 | * @return Page |
||
| 398 | */ |
||
| 399 | function atom ($href, $title = 'Atom Feed') { |
||
| 409 | /** |
||
| 410 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 411 | * |
||
| 412 | * @param string $href |
||
| 413 | * @param string $title |
||
| 414 | * |
||
| 415 | * @return Page |
||
| 416 | */ |
||
| 417 | function rss ($href, $title = 'RSS Feed') { |
||
| 427 | /** |
||
| 428 | * Specify canonical url of current page |
||
| 429 | * |
||
| 430 | * @param string $url |
||
| 431 | * |
||
| 432 | * @return Page |
||
| 433 | */ |
||
| 434 | function canonical_url ($url) { |
||
| 442 | /** |
||
| 443 | * Adding text to the title page |
||
| 444 | * |
||
| 445 | * @param string $title |
||
| 446 | * @param bool $replace Replace whole title by this |
||
| 447 | * |
||
| 448 | * @return Page |
||
| 449 | */ |
||
| 450 | function title ($title, $replace = false) { |
||
| 459 | /** |
||
| 460 | * Display success message |
||
| 461 | * |
||
| 462 | * @param string $success_text |
||
| 463 | * |
||
| 464 | * @return Page |
||
| 465 | */ |
||
| 466 | function success ($success_text) { |
||
| 469 | /** |
||
| 470 | * Display notice message |
||
| 471 | * |
||
| 472 | * @param string $notice_text |
||
| 473 | * |
||
| 474 | * @return Page |
||
| 475 | */ |
||
| 476 | function notice ($notice_text) { |
||
| 479 | /** |
||
| 480 | * Display warning message |
||
| 481 | * |
||
| 482 | * @param string $warning_text |
||
| 483 | * |
||
| 484 | * @return Page |
||
| 485 | */ |
||
| 486 | function warning ($warning_text) { |
||
| 489 | /** |
||
| 490 | * Generic method for 3 methods above |
||
| 491 | * |
||
| 492 | * @param string $message |
||
| 493 | * @param string $class_ending |
||
| 494 | * |
||
| 495 | * @return Page |
||
| 496 | */ |
||
| 497 | protected function top_message ($message, $class_ending) { |
||
| 506 | /** |
||
| 507 | * Error pages processing |
||
| 508 | * |
||
| 509 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 510 | * @param bool $json Force JSON return format |
||
| 511 | */ |
||
| 512 | function error ($custom_text = null, $json = false) { |
||
| 553 | /** |
||
| 554 | * @deprecated Use `cs\Page::render()` instead |
||
| 555 | */ |
||
| 556 | function __finish () { |
||
| 559 | /** |
||
| 560 | * Provides next events: |
||
| 561 | * System/Page/render/before |
||
| 562 | * |
||
| 563 | * System/Page/render/after |
||
| 564 | * |
||
| 565 | * Page generation |
||
| 566 | */ |
||
| 567 | function render () { |
||
| 605 | } |
||
| 606 |