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 |
||
| 34 | class Page { |
||
| 35 | use |
||
| 36 | Singleton, |
||
| 37 | Includes; |
||
| 38 | const INIT_STATE_METHOD = 'init'; |
||
| 39 | /** |
||
| 40 | * Complete page contents |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | public $Content; |
||
| 45 | /** |
||
| 46 | * 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) |
||
| 47 | * |
||
| 48 | * @var bool |
||
| 49 | */ |
||
| 50 | public $interface; |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | public $pre_Html; |
||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | */ |
||
| 58 | public $Html; |
||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $Description; |
||
| 63 | /** |
||
| 64 | * @var string|string[] |
||
| 65 | */ |
||
| 66 | public $Title; |
||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $Head; |
||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | public $pre_Body; |
||
| 75 | /** |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $Left; |
||
| 79 | /** |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | public $Top; |
||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | public $Right; |
||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | public $Bottom; |
||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | public $post_Body; |
||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | public $post_Html; |
||
| 99 | /** |
||
| 100 | * Number of tabs by default for indentation the substitution of values into template |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | public $level; |
||
| 105 | /** |
||
| 106 | * @var array[] |
||
| 107 | */ |
||
| 108 | protected $link; |
||
| 109 | /** |
||
| 110 | * @var string[] |
||
| 111 | */ |
||
| 112 | protected $search_replace; |
||
| 113 | /** |
||
| 114 | * @var false|string |
||
| 115 | */ |
||
| 116 | protected $canonical_url; |
||
| 117 | protected $theme; |
||
| 118 | /** |
||
| 119 | * @param string $property |
||
| 120 | * |
||
| 121 | * @return false|null|string |
||
| 122 | */ |
||
| 123 | public function __get ($property) { |
||
| 181 | /** |
||
| 182 | * Adding of content on the page |
||
| 183 | * |
||
| 184 | * @param string $add |
||
| 185 | * @param bool|int $level |
||
| 186 | * |
||
| 187 | * @return Page |
||
| 188 | */ |
||
| 189 | public function content ($add, $level = false) { |
||
| 197 | /** |
||
| 198 | * Sets body with content, that is transformed into JSON format |
||
| 199 | * |
||
| 200 | * @param mixed $content |
||
| 201 | * |
||
| 202 | * @return Page |
||
| 203 | */ |
||
| 204 | 20 | public function json ($content) { |
|
| 210 | /** |
||
| 211 | * Loading of theme template |
||
| 212 | */ |
||
| 213 | 2 | protected function get_template () { |
|
| 225 | /** |
||
| 226 | * Processing of template, substituting of content, preparing for the output |
||
| 227 | * |
||
| 228 | * @return Page |
||
| 229 | */ |
||
| 230 | 2 | protected function prepare () { |
|
| 321 | /** |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 2 | protected function get_favicon_path () { |
|
| 331 | /** |
||
| 332 | * @param string $property |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | 2 | protected function get_property_with_indentation ($property) { |
|
| 339 | /** |
||
| 340 | * Replacing anything in source code of finally generated page |
||
| 341 | * |
||
| 342 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 343 | * |
||
| 344 | * @param string|string[] $search |
||
| 345 | * @param string|string[] $replace |
||
| 346 | * |
||
| 347 | * @return Page |
||
| 348 | */ |
||
| 349 | public function replace ($search, $replace = '') { |
||
| 358 | /** |
||
| 359 | * Processing of replacing in content |
||
| 360 | * |
||
| 361 | * @param string $content |
||
| 362 | * |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | 22 | protected function process_replacing ($content) { |
|
| 372 | /** |
||
| 373 | * Adding links |
||
| 374 | * |
||
| 375 | * @param array $data According to h class syntax |
||
| 376 | * |
||
| 377 | * @return Page |
||
| 378 | */ |
||
| 379 | public function link ($data) { |
||
| 385 | /** |
||
| 386 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 387 | * |
||
| 388 | * @param string $href |
||
| 389 | * @param string $title |
||
| 390 | * |
||
| 391 | * @return Page |
||
| 392 | */ |
||
| 393 | public function atom ($href, $title = 'Atom Feed') { |
||
| 403 | /** |
||
| 404 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 405 | * |
||
| 406 | * @param string $href |
||
| 407 | * @param string $title |
||
| 408 | * |
||
| 409 | * @return Page |
||
| 410 | */ |
||
| 411 | public function rss ($href, $title = 'RSS Feed') { |
||
| 421 | /** |
||
| 422 | * Specify canonical url of current page |
||
| 423 | * |
||
| 424 | * @param string $url |
||
| 425 | * |
||
| 426 | * @return Page |
||
| 427 | */ |
||
| 428 | public function canonical_url ($url) { |
||
| 436 | /** |
||
| 437 | * Adding text to the title page |
||
| 438 | * |
||
| 439 | * @param string $title |
||
| 440 | * @param bool $replace Replace whole title by this |
||
| 441 | * |
||
| 442 | * @return Page |
||
| 443 | */ |
||
| 444 | 2 | public function title ($title, $replace = false) { |
|
| 453 | /** |
||
| 454 | * Display success message |
||
| 455 | * |
||
| 456 | * @param string $success_text |
||
| 457 | * |
||
| 458 | * @return Page |
||
| 459 | */ |
||
| 460 | public function success ($success_text) { |
||
| 463 | /** |
||
| 464 | * Display notice message |
||
| 465 | * |
||
| 466 | * @param string $notice_text |
||
| 467 | * |
||
| 468 | * @return Page |
||
| 469 | */ |
||
| 470 | public function notice ($notice_text) { |
||
| 473 | /** |
||
| 474 | * Display warning message |
||
| 475 | * |
||
| 476 | * @param string $warning_text |
||
| 477 | * |
||
| 478 | * @return Page |
||
| 479 | */ |
||
| 480 | 2 | public function warning ($warning_text) { |
|
| 483 | /** |
||
| 484 | * Generic method for 3 methods above |
||
| 485 | * |
||
| 486 | * @param string $message |
||
| 487 | * @param string $class_ending |
||
| 488 | * |
||
| 489 | * @return Page |
||
| 490 | */ |
||
| 491 | 2 | protected function top_message ($message, $class_ending) { |
|
| 500 | /** |
||
| 501 | * Error pages processing |
||
| 502 | * |
||
| 503 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 504 | * @param bool $json Force JSON return format |
||
| 505 | */ |
||
| 506 | 14 | public function error ($custom_text = null, $json = false) { |
|
| 533 | /** |
||
| 534 | * @param int $code |
||
| 535 | * @param null|string|string[] $custom_text |
||
| 536 | * |
||
| 537 | * @return string[] |
||
| 538 | */ |
||
| 539 | 14 | protected function error_title_description ($code, $custom_text) { |
|
| 547 | /** |
||
| 548 | * @param string $title |
||
| 549 | * @param string $description |
||
| 550 | * |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | 4 | protected function error_page ($title, $description) { |
|
| 566 | /** |
||
| 567 | * Provides next events: |
||
| 568 | * System/Page/render/before |
||
| 569 | * |
||
| 570 | * System/Page/render/after |
||
| 571 | * |
||
| 572 | * Page generation |
||
| 573 | */ |
||
| 574 | 22 | public function render () { |
|
| 602 | } |
||
| 603 |