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 |
||
| 37 | class Page { |
||
| 38 | use |
||
| 39 | Singleton, |
||
| 40 | Includes; |
||
| 41 | const INIT_STATE_METHOD = 'init'; |
||
| 42 | /** |
||
| 43 | * Complete page contents |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | public $Content; |
||
| 48 | /** |
||
| 49 | * 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) |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | public $interface; |
||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | public $pre_Html; |
||
| 58 | /** |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | public $Html; |
||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | public $Description; |
||
| 66 | /** |
||
| 67 | * @var string|string[] |
||
| 68 | */ |
||
| 69 | public $Title; |
||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $Head; |
||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $pre_Body; |
||
| 78 | /** |
||
| 79 | * @var string |
||
| 80 | */ |
||
| 81 | public $Left; |
||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | public $Top; |
||
| 86 | /** |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | public $Right; |
||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | public $Bottom; |
||
| 94 | /** |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | public $post_Body; |
||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | public $post_Html; |
||
| 102 | /** |
||
| 103 | * Number of tabs by default for indentation the substitution of values into template |
||
| 104 | * |
||
| 105 | * @var array |
||
| 106 | */ |
||
| 107 | public $level; |
||
| 108 | /** |
||
| 109 | * @var array[] |
||
| 110 | */ |
||
| 111 | protected $link; |
||
| 112 | /** |
||
| 113 | * @var string[] |
||
| 114 | */ |
||
| 115 | protected $search_replace; |
||
| 116 | /** |
||
| 117 | * @var false|string |
||
| 118 | */ |
||
| 119 | protected $canonical_url; |
||
| 120 | protected $theme; |
||
| 121 | protected $finish_called_once; |
||
| 122 | /** |
||
| 123 | * @param string $property |
||
| 124 | * |
||
| 125 | * @return false|null|string |
||
| 126 | */ |
||
| 127 | function __get ($property) { |
||
| 186 | /** |
||
| 187 | * Initialization: setting of title and theme according to system configuration |
||
| 188 | * |
||
| 189 | * @return Page |
||
| 190 | */ |
||
| 191 | 16 | protected function init_config () { |
|
| 197 | /** |
||
| 198 | * Theme changing |
||
| 199 | * |
||
| 200 | * @param string $theme |
||
| 201 | * |
||
| 202 | * @return Page |
||
| 203 | */ |
||
| 204 | 16 | function set_theme ($theme) { |
|
| 208 | /** |
||
| 209 | * Adding of content on the page |
||
| 210 | * |
||
| 211 | * @param string $add |
||
| 212 | * @param bool|int $level |
||
| 213 | * |
||
| 214 | * @return Page |
||
| 215 | */ |
||
| 216 | function content ($add, $level = false) { |
||
| 224 | /** |
||
| 225 | * Sets body with content, that is transformed into JSON format |
||
| 226 | * |
||
| 227 | * @param mixed $add |
||
| 228 | * |
||
| 229 | * @return Page |
||
| 230 | */ |
||
| 231 | function json ($add) { |
||
| 237 | /** |
||
| 238 | * Loading of theme template |
||
| 239 | */ |
||
| 240 | 2 | protected function get_template () { |
|
| 252 | /** |
||
| 253 | * Processing of template, substituting of content, preparing for the output |
||
| 254 | * |
||
| 255 | * @return Page |
||
| 256 | */ |
||
| 257 | 2 | protected function prepare () { |
|
| 347 | /** |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 2 | protected function get_favicon_path () { |
|
| 357 | /** |
||
| 358 | * @param string $property |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | 2 | protected function get_property_with_indentation ($property) { |
|
| 365 | /** |
||
| 366 | * Replacing anything in source code of finally generated page |
||
| 367 | * |
||
| 368 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 369 | * |
||
| 370 | * @param string|string[] $search |
||
| 371 | * @param string|string[] $replace |
||
| 372 | * |
||
| 373 | * @return Page |
||
| 374 | */ |
||
| 375 | function replace ($search, $replace = '') { |
||
| 384 | /** |
||
| 385 | * Processing of replacing in content |
||
| 386 | * |
||
| 387 | * @param string $content |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 6 | protected function process_replacing ($content) { |
|
| 398 | /** |
||
| 399 | * Adding links |
||
| 400 | * |
||
| 401 | * @param array $data According to h class syntax |
||
| 402 | * |
||
| 403 | * @return Page |
||
| 404 | */ |
||
| 405 | function link ($data) { |
||
| 411 | /** |
||
| 412 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 413 | * |
||
| 414 | * @param string $href |
||
| 415 | * @param string $title |
||
| 416 | * |
||
| 417 | * @return Page |
||
| 418 | */ |
||
| 419 | function atom ($href, $title = 'Atom Feed') { |
||
| 429 | /** |
||
| 430 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 431 | * |
||
| 432 | * @param string $href |
||
| 433 | * @param string $title |
||
| 434 | * |
||
| 435 | * @return Page |
||
| 436 | */ |
||
| 437 | function rss ($href, $title = 'RSS Feed') { |
||
| 447 | /** |
||
| 448 | * Specify canonical url of current page |
||
| 449 | * |
||
| 450 | * @param string $url |
||
| 451 | * |
||
| 452 | * @return Page |
||
| 453 | */ |
||
| 454 | function canonical_url ($url) { |
||
| 462 | /** |
||
| 463 | * Adding text to the title page |
||
| 464 | * |
||
| 465 | * @param string $title |
||
| 466 | * @param bool $replace Replace whole title by this |
||
| 467 | * |
||
| 468 | * @return Page |
||
| 469 | */ |
||
| 470 | 2 | function title ($title, $replace = false) { |
|
| 479 | /** |
||
| 480 | * Display success message |
||
| 481 | * |
||
| 482 | * @param string $success_text |
||
| 483 | * |
||
| 484 | * @return Page |
||
| 485 | */ |
||
| 486 | function success ($success_text) { |
||
| 489 | /** |
||
| 490 | * Display notice message |
||
| 491 | * |
||
| 492 | * @param string $notice_text |
||
| 493 | * |
||
| 494 | * @return Page |
||
| 495 | */ |
||
| 496 | function notice ($notice_text) { |
||
| 499 | /** |
||
| 500 | * Display warning message |
||
| 501 | * |
||
| 502 | * @param string $warning_text |
||
| 503 | * |
||
| 504 | * @return Page |
||
| 505 | */ |
||
| 506 | 2 | function warning ($warning_text) { |
|
| 507 | 2 | return $this->top_message($warning_text, 'error'); |
|
| 508 | } |
||
| 509 | /** |
||
| 510 | * Generic method for 3 methods above |
||
| 511 | * |
||
| 512 | * @param string $message |
||
| 513 | * @param string $class_ending |
||
| 514 | * |
||
| 515 | * @return Page |
||
| 516 | */ |
||
| 517 | 2 | protected function top_message ($message, $class_ending) { |
|
| 518 | 2 | $this->Top .= h::div( |
|
| 519 | $message, |
||
| 520 | [ |
||
| 521 | 2 | 'class' => "cs-text-center cs-block-$class_ending cs-text-$class_ending" |
|
| 522 | ] |
||
| 523 | ); |
||
| 524 | 2 | return $this; |
|
| 525 | } |
||
| 526 | /** |
||
| 527 | * Error pages processing |
||
| 528 | * |
||
| 529 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 530 | * @param bool $json Force JSON return format |
||
| 531 | */ |
||
| 532 | 4 | function error ($custom_text = null, $json = false) { |
|
| 559 | /** |
||
| 560 | * @param int $code |
||
| 561 | * @param null|string|string[] $custom_text |
||
| 562 | * |
||
| 563 | * @return string[] |
||
| 564 | */ |
||
| 565 | 4 | protected function error_title_description ($code, $custom_text) { |
|
| 566 | 4 | $title = status_code_string($code); |
|
| 567 | 4 | $description = $custom_text ?: $title; |
|
| 568 | 4 | if (is_array($custom_text)) { |
|
| 569 | 2 | list($title, $description) = $custom_text; |
|
| 570 | } |
||
| 571 | 4 | return [$title, $description]; |
|
| 572 | } |
||
| 573 | /** |
||
| 574 | * @param string $title |
||
| 575 | * @param string $description |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | 4 | protected function error_page ($title, $description) { |
|
| 592 | /** |
||
| 593 | * Provides next events: |
||
| 594 | * System/Page/render/before |
||
| 595 | * |
||
| 596 | * System/Page/render/after |
||
| 597 | * |
||
| 598 | * Page generation |
||
| 599 | */ |
||
| 600 | 6 | function render () { |
|
| 636 | } |
||
| 637 |