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 |
||
| 17 | class Page { |
||
| 18 | use |
||
| 19 | Singleton, |
||
| 20 | Includes; |
||
| 21 | public $Content; |
||
| 22 | public $interface = true; |
||
| 23 | public $pre_Html = ''; |
||
| 24 | public $Html = ''; |
||
| 25 | public $Description = ''; |
||
| 26 | /** |
||
| 27 | * @var string|string[] |
||
| 28 | */ |
||
| 29 | public $Title = []; |
||
| 30 | public $Head = ''; |
||
| 31 | public $pre_Body = ''; |
||
| 32 | public $Left = ''; |
||
| 33 | public $Top = ''; |
||
| 34 | public $Right = ''; |
||
| 35 | public $Bottom = ''; |
||
| 36 | public $post_Body = ''; |
||
| 37 | public $post_Html = ''; |
||
| 38 | /** |
||
| 39 | * Number of tabs by default for indentation the substitution of values into template |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public $level = [ |
||
| 44 | 'Head' => 0, |
||
| 45 | 'pre_Body' => 1, |
||
| 46 | 'Left' => 3, |
||
| 47 | 'Top' => 3, |
||
| 48 | 'Content' => 4, |
||
| 49 | 'Bottom' => 3, |
||
| 50 | 'Right' => 3, |
||
| 51 | 'post_Body' => 1 |
||
| 52 | ]; |
||
| 53 | /** |
||
| 54 | * @var array[] |
||
| 55 | */ |
||
| 56 | protected $link = []; |
||
| 57 | /** |
||
| 58 | * @var string[] |
||
| 59 | */ |
||
| 60 | protected $search_replace = []; |
||
| 61 | /** |
||
| 62 | * @var false|string |
||
| 63 | */ |
||
| 64 | protected $canonical_url = false; |
||
| 65 | protected $theme; |
||
| 66 | protected $error_showed = false; |
||
| 67 | protected $finish_called_once = false; |
||
| 68 | /** |
||
| 69 | * @param string $property |
||
| 70 | * |
||
| 71 | * @return false|null|string |
||
| 72 | */ |
||
| 73 | function __get ($property) { |
||
| 80 | /** |
||
| 81 | * Initialization: setting of title and theme according to specified parameters |
||
| 82 | * |
||
| 83 | * @param string $title |
||
| 84 | * @param string $theme |
||
| 85 | * |
||
| 86 | * @return Page |
||
| 87 | */ |
||
| 88 | function init ($title, $theme) { |
||
| 93 | /** |
||
| 94 | * Theme changing |
||
| 95 | * |
||
| 96 | * @param string $theme |
||
| 97 | * |
||
| 98 | * @return Page |
||
| 99 | */ |
||
| 100 | function set_theme ($theme) { |
||
| 104 | /** |
||
| 105 | * Adding of content on the page |
||
| 106 | * |
||
| 107 | * @param string $add |
||
| 108 | * @param bool|int $level |
||
| 109 | * |
||
| 110 | * @return Page |
||
| 111 | */ |
||
| 112 | function content ($add, $level = false) { |
||
| 120 | /** |
||
| 121 | * Sets body with content, that is transformed into JSON format |
||
| 122 | * |
||
| 123 | * @param mixed $add |
||
| 124 | * |
||
| 125 | * @return Page |
||
| 126 | */ |
||
| 127 | function json ($add) { |
||
| 133 | /** |
||
| 134 | * Loading of theme template |
||
| 135 | * |
||
| 136 | * @return bool |
||
| 137 | */ |
||
| 138 | protected function get_template () { |
||
| 170 | /** |
||
| 171 | * Processing of template, substituting of content, preparing for the output |
||
| 172 | * |
||
| 173 | * @return Page |
||
|
|
|||
| 174 | */ |
||
| 175 | protected function prepare () { |
||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | protected function get_favicon_path () { |
||
| 277 | /** |
||
| 278 | * @param string $property |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | protected function get_property_with_indentation ($property) { |
||
| 285 | /** |
||
| 286 | * Replacing anything in source code of finally generated page |
||
| 287 | * |
||
| 288 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 289 | * |
||
| 290 | * @param string|string[] $search |
||
| 291 | * @param string|string[] $replace |
||
| 292 | * |
||
| 293 | * @return Page |
||
| 294 | */ |
||
| 295 | function replace ($search, $replace = '') { |
||
| 303 | /** |
||
| 304 | * Processing of replacing in content |
||
| 305 | * |
||
| 306 | * @param string $content |
||
| 307 | * |
||
| 308 | * @return string |
||
| 309 | */ |
||
| 310 | protected function process_replacing ($content) { |
||
| 317 | /** |
||
| 318 | * Adding links |
||
| 319 | * |
||
| 320 | * @param array $data According to h class syntax |
||
| 321 | * |
||
| 322 | * @return Page |
||
| 323 | */ |
||
| 324 | function link ($data) { |
||
| 330 | /** |
||
| 331 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 332 | * |
||
| 333 | * @param string $href |
||
| 334 | * @param string $title |
||
| 335 | * |
||
| 336 | * @return Page |
||
| 337 | */ |
||
| 338 | function atom ($href, $title = 'Atom Feed') { |
||
| 348 | /** |
||
| 349 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 350 | * |
||
| 351 | * @param string $href |
||
| 352 | * @param string $title |
||
| 353 | * |
||
| 354 | * @return Page |
||
| 355 | */ |
||
| 356 | function rss ($href, $title = 'RSS Feed') { |
||
| 366 | /** |
||
| 367 | * Specify canonical url of current page |
||
| 368 | * |
||
| 369 | * @param string $url |
||
| 370 | * |
||
| 371 | * @return Page |
||
| 372 | */ |
||
| 373 | function canonical_url ($url) { |
||
| 381 | /** |
||
| 382 | * Adding text to the title page |
||
| 383 | * |
||
| 384 | * @param string $title |
||
| 385 | * @param bool $replace Replace whole title by this |
||
| 386 | * |
||
| 387 | * @return Page |
||
| 388 | */ |
||
| 389 | function title ($title, $replace = false) { |
||
| 398 | /** |
||
| 399 | * Display success message |
||
| 400 | * |
||
| 401 | * @param string $success_text |
||
| 402 | * |
||
| 403 | * @return Page |
||
| 404 | */ |
||
| 405 | function success ($success_text) { |
||
| 408 | /** |
||
| 409 | * Display notice message |
||
| 410 | * |
||
| 411 | * @param string $notice_text |
||
| 412 | * |
||
| 413 | * @return Page |
||
| 414 | */ |
||
| 415 | function notice ($notice_text) { |
||
| 418 | /** |
||
| 419 | * Display warning message |
||
| 420 | * |
||
| 421 | * @param string $warning_text |
||
| 422 | * |
||
| 423 | * @return Page |
||
| 424 | */ |
||
| 425 | function warning ($warning_text) { |
||
| 428 | /** |
||
| 429 | * Generic method for 3 methods above |
||
| 430 | * |
||
| 431 | * @param string $message |
||
| 432 | * @param string $class_ending |
||
| 433 | * |
||
| 434 | * @return Page |
||
| 435 | */ |
||
| 436 | protected function top_message ($message, $class_ending) { |
||
| 445 | /** |
||
| 446 | * Error pages processing |
||
| 447 | * |
||
| 448 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 449 | * @param bool $json Force JSON return format |
||
| 450 | * @param int $error_code HTTP status code |
||
| 451 | * |
||
| 452 | * @throws ExitException |
||
| 453 | */ |
||
| 454 | function error ($custom_text = null, $json = false, $error_code = 500) { |
||
| 502 | /** |
||
| 503 | * Provides next events: |
||
| 504 | * System/Page/display/before |
||
| 505 | * |
||
| 506 | * System/Page/display/after |
||
| 507 | * |
||
| 508 | * Page generation |
||
| 509 | */ |
||
| 510 | function __finish () { |
||
| 541 | } |
||
| 542 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.