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 |
||
|
1 ignored issue
–
show
|
|||
| 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 Page |
||
| 137 | */ |
||
| 138 | protected function get_template () { |
||
| 168 | /** |
||
| 169 | * Processing of template, substituting of content, preparing for the output |
||
| 170 | * |
||
| 171 | * @return Page |
||
| 172 | */ |
||
| 173 | protected function prepare () { |
||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function get_favicon_path () { |
||
| 269 | /** |
||
| 270 | * @param string $property |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | protected function get_property_with_indentation ($property) { |
||
| 277 | /** |
||
| 278 | * Replacing anything in source code of finally generated page |
||
| 279 | * |
||
| 280 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
| 281 | * |
||
| 282 | * @param string|string[] $search |
||
| 283 | * @param string|string[] $replace |
||
| 284 | * |
||
| 285 | * @return Page |
||
| 286 | */ |
||
| 287 | function replace ($search, $replace = '') { |
||
| 295 | /** |
||
| 296 | * Processing of replacing in content |
||
| 297 | * |
||
| 298 | * @param string $content |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | protected function process_replacing ($content) { |
||
| 309 | /** |
||
| 310 | * Adding links |
||
| 311 | * |
||
| 312 | * @param array $data According to h class syntax |
||
| 313 | * |
||
| 314 | * @return Page |
||
| 315 | */ |
||
| 316 | function link ($data) { |
||
| 322 | /** |
||
| 323 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
| 324 | * |
||
| 325 | * @param string $href |
||
| 326 | * @param string $title |
||
| 327 | * |
||
| 328 | * @return Page |
||
| 329 | */ |
||
| 330 | function atom ($href, $title = 'Atom Feed') { |
||
| 340 | /** |
||
| 341 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
| 342 | * |
||
| 343 | * @param string $href |
||
| 344 | * @param string $title |
||
| 345 | * |
||
| 346 | * @return Page |
||
| 347 | */ |
||
| 348 | function rss ($href, $title = 'RSS Feed') { |
||
| 358 | /** |
||
| 359 | * Specify canonical url of current page |
||
| 360 | * |
||
| 361 | * @param string $url |
||
| 362 | * |
||
| 363 | * @return Page |
||
| 364 | */ |
||
| 365 | function canonical_url ($url) { |
||
| 373 | /** |
||
| 374 | * Adding text to the title page |
||
| 375 | * |
||
| 376 | * @param string $title |
||
| 377 | * @param bool $replace Replace whole title by this |
||
| 378 | * |
||
| 379 | * @return Page |
||
| 380 | */ |
||
| 381 | function title ($title, $replace = false) { |
||
| 390 | /** |
||
| 391 | * Display success message |
||
| 392 | * |
||
| 393 | * @param string $success_text |
||
| 394 | * |
||
| 395 | * @return Page |
||
| 396 | */ |
||
| 397 | function success ($success_text) { |
||
| 400 | /** |
||
| 401 | * Display notice message |
||
| 402 | * |
||
| 403 | * @param string $notice_text |
||
| 404 | * |
||
| 405 | * @return Page |
||
| 406 | */ |
||
| 407 | function notice ($notice_text) { |
||
| 410 | /** |
||
| 411 | * Display warning message |
||
| 412 | * |
||
| 413 | * @param string $warning_text |
||
| 414 | * |
||
| 415 | * @return Page |
||
| 416 | */ |
||
| 417 | function warning ($warning_text) { |
||
| 420 | /** |
||
| 421 | * Generic method for 3 methods above |
||
| 422 | * |
||
| 423 | * @param string $message |
||
| 424 | * @param string $class_ending |
||
| 425 | * |
||
| 426 | * @return Page |
||
| 427 | */ |
||
| 428 | protected function top_message ($message, $class_ending) { |
||
| 437 | /** |
||
| 438 | * Error pages processing |
||
| 439 | * |
||
| 440 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
| 441 | * @param bool $json Force JSON return format |
||
| 442 | * @param int $error_code HTTP status code |
||
| 443 | * |
||
| 444 | * @throws ExitException |
||
| 445 | */ |
||
| 446 | function error ($custom_text = null, $json = false, $error_code = 500) { |
||
| 494 | /** |
||
| 495 | * Provides next events: |
||
| 496 | * System/Page/display/before |
||
| 497 | * |
||
| 498 | * System/Page/display/after |
||
| 499 | * |
||
| 500 | * Page generation |
||
| 501 | */ |
||
| 502 | function __finish () { |
||
| 533 | } |
||
| 534 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.