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 | public $Content; |
||
| 19 | public $interface = true; |
||
| 20 | public $pre_Html = ''; |
||
| 21 | public $Html = ''; |
||
| 22 | public $Description = ''; |
||
| 23 | /** |
||
| 24 | * @var string|string[] |
||
| 25 | */ |
||
| 26 | public $Title = []; |
||
| 27 | public $Head = ''; |
||
| 28 | public $pre_Body = ''; |
||
| 29 | public $Left = ''; |
||
| 30 | public $Top = ''; |
||
| 31 | public $Right = ''; |
||
| 32 | public $Bottom = ''; |
||
| 33 | public $post_Body = ''; |
||
| 34 | public $post_Html = ''; |
||
| 35 | /** |
||
| 36 | * Number of tabs by default for indentation the substitution of values into template |
||
| 37 | * |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | public $level = [ |
||
| 41 | 'Head' => 0, |
||
| 42 | 'pre_Body' => 1, |
||
| 43 | 'Left' => 3, |
||
| 44 | 'Top' => 3, |
||
| 45 | 'Content' => 4, |
||
| 46 | 'Bottom' => 3, |
||
| 47 | 'Right' => 3, |
||
| 48 | 'post_Body' => 1 |
||
| 49 | ]; |
||
| 50 | /** |
||
| 51 | * @var array[] |
||
| 52 | */ |
||
| 53 | protected $link = []; |
||
| 54 | /** |
||
| 55 | * @var string[] |
||
| 56 | */ |
||
| 57 | protected $search_replace = []; |
||
| 58 | /** |
||
| 59 | * @var false|string |
||
| 60 | */ |
||
| 61 | protected $canonical_url = false; |
||
| 62 | protected $theme; |
||
| 63 | protected $finish_called_once = false; |
||
| 64 | /** |
||
| 65 | * @param string $property |
||
| 66 | * |
||
| 67 | * @return false|null|string |
||
| 68 | */ |
||
| 69 | function __get ($property) { |
||
| 98 | /** |
||
| 99 | * Initialization: setting of title and theme according to system configuration |
||
| 100 | * |
||
| 101 | * @return Page |
||
| 102 | */ |
||
| 103 | protected function init () { |
||
| 109 | /** |
||
| 110 | * Theme changing |
||
| 111 | * |
||
| 112 | * @param string $theme |
||
| 113 | * |
||
| 114 | * @return Page |
||
| 115 | */ |
||
| 116 | function set_theme ($theme) { |
||
| 120 | /** |
||
| 121 | * Adding of content on the page |
||
| 122 | * |
||
| 123 | * @param string $add |
||
| 124 | * @param bool|int $level |
||
| 125 | * |
||
| 126 | * @return Page |
||
| 127 | */ |
||
| 128 | function content ($add, $level = false) { |
||
| 136 | /** |
||
| 137 | * Sets body with content, that is transformed into JSON format |
||
| 138 | * |
||
| 139 | * @param mixed $add |
||
| 140 | * |
||
| 141 | * @return Page |
||
| 142 | */ |
||
| 143 | function json ($add) { |
||
| 149 | /** |
||
| 150 | * Loading of theme template |
||
| 151 | */ |
||
| 152 | protected function get_template () { |
||
| 165 | /** |
||
| 166 | * Processing of template, substituting of content, preparing for the output |
||
| 167 | * |
||
| 168 | * @return Page |
||
| 169 | */ |
||
| 170 | protected function prepare () { |
||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | protected function get_favicon_path () { |
||
| 270 | /** |
||
| 271 | * @param string $property |
||
| 272 | * |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | protected function get_property_with_indentation ($property) { |
||
| 278 | /** |
||
| 279 | * Replacing anything in source code of finally generated page |
||
| 280 | * |
||
| 281 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 282 | * |
||
| 283 | * @param string|string[] $search |
||
| 284 | * @param string|string[] $replace |
||
| 285 | * |
||
| 286 | * @return Page |
||
| 287 | */ |
||
| 288 | function replace ($search, $replace = '') { |
||
| 297 | /** |
||
| 298 | * Processing of replacing in content |
||
| 299 | * |
||
| 300 | * @param string $content |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | protected function process_replacing ($content) { |
||
| 311 | /** |
||
| 312 | * Adding links |
||
| 313 | * |
||
| 314 | * @param array $data According to h class syntax |
||
| 315 | * |
||
| 316 | * @return Page |
||
| 317 | */ |
||
| 318 | function link ($data) { |
||
| 324 | /** |
||
| 325 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 326 | * |
||
| 327 | * @param string $href |
||
| 328 | * @param string $title |
||
| 329 | * |
||
| 330 | * @return Page |
||
| 331 | */ |
||
| 332 | function atom ($href, $title = 'Atom Feed') { |
||
| 342 | /** |
||
| 343 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 344 | * |
||
| 345 | * @param string $href |
||
| 346 | * @param string $title |
||
| 347 | * |
||
| 348 | * @return Page |
||
| 349 | */ |
||
| 350 | function rss ($href, $title = 'RSS Feed') { |
||
| 360 | /** |
||
| 361 | * Specify canonical url of current page |
||
| 362 | * |
||
| 363 | * @param string $url |
||
| 364 | * |
||
| 365 | * @return Page |
||
| 366 | */ |
||
| 367 | function canonical_url ($url) { |
||
| 375 | /** |
||
| 376 | * Adding text to the title page |
||
| 377 | * |
||
| 378 | * @param string $title |
||
| 379 | * @param bool $replace Replace whole title by this |
||
| 380 | * |
||
| 381 | * @return Page |
||
| 382 | */ |
||
| 383 | function title ($title, $replace = false) { |
||
| 392 | /** |
||
| 393 | * Display success message |
||
| 394 | * |
||
| 395 | * @param string $success_text |
||
| 396 | * |
||
| 397 | * @return Page |
||
| 398 | */ |
||
| 399 | function success ($success_text) { |
||
| 402 | /** |
||
| 403 | * Display notice message |
||
| 404 | * |
||
| 405 | * @param string $notice_text |
||
| 406 | * |
||
| 407 | * @return Page |
||
| 408 | */ |
||
| 409 | function notice ($notice_text) { |
||
| 412 | /** |
||
| 413 | * Display warning message |
||
| 414 | * |
||
| 415 | * @param string $warning_text |
||
| 416 | * |
||
| 417 | * @return Page |
||
| 418 | */ |
||
| 419 | function warning ($warning_text) { |
||
| 422 | /** |
||
| 423 | * Generic method for 3 methods above |
||
| 424 | * |
||
| 425 | * @param string $message |
||
| 426 | * @param string $class_ending |
||
| 427 | * |
||
| 428 | * @return Page |
||
| 429 | */ |
||
| 430 | protected function top_message ($message, $class_ending) { |
||
| 439 | /** |
||
| 440 | * Error pages processing |
||
| 441 | * |
||
| 442 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 443 | * @param bool $json Force JSON return format |
||
| 444 | */ |
||
| 445 | function error ($custom_text = null, $json = false) { |
||
| 487 | /** |
||
| 488 | * @todo refactor this into something meaningful, should not be this way |
||
| 489 | * |
||
| 490 | * Provides next events: |
||
| 491 | * System/Page/display/before |
||
| 492 | * |
||
| 493 | * System/Page/display/after |
||
| 494 | * |
||
| 495 | * Page generation |
||
| 496 | */ |
||
| 497 | function __finish () { |
||
| 533 | } |
||
| 534 |