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 | /** |
||
122 | * @param string $property |
||
123 | * |
||
124 | * @return false|null|string |
||
125 | */ |
||
126 | public function __get ($property) { |
||
184 | /** |
||
185 | * Adding of content on the page |
||
186 | * |
||
187 | * @param string $add |
||
188 | * @param bool|int $level |
||
189 | * |
||
190 | * @return Page |
||
191 | */ |
||
192 | public function content ($add, $level = false) { |
||
200 | /** |
||
201 | * Sets body with content, that is transformed into JSON format |
||
202 | * |
||
203 | * @param mixed $add |
||
204 | * |
||
205 | * @return Page |
||
206 | */ |
||
207 | public function json ($add) { |
||
213 | /** |
||
214 | * Loading of theme template |
||
215 | */ |
||
216 | 2 | protected function get_template () { |
|
228 | /** |
||
229 | * Processing of template, substituting of content, preparing for the output |
||
230 | * |
||
231 | * @return Page |
||
232 | */ |
||
233 | 2 | protected function prepare () { |
|
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | 2 | protected function get_favicon_path () { |
|
334 | /** |
||
335 | * @param string $property |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | 2 | protected function get_property_with_indentation ($property) { |
|
342 | /** |
||
343 | * Replacing anything in source code of finally generated page |
||
344 | * |
||
345 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
346 | * |
||
347 | * @param string|string[] $search |
||
348 | * @param string|string[] $replace |
||
349 | * |
||
350 | * @return Page |
||
351 | */ |
||
352 | public function replace ($search, $replace = '') { |
||
361 | /** |
||
362 | * Processing of replacing in content |
||
363 | * |
||
364 | * @param string $content |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | 6 | protected function process_replacing ($content) { |
|
375 | /** |
||
376 | * Adding links |
||
377 | * |
||
378 | * @param array $data According to h class syntax |
||
379 | * |
||
380 | * @return Page |
||
381 | */ |
||
382 | public function link ($data) { |
||
388 | /** |
||
389 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
390 | * |
||
391 | * @param string $href |
||
392 | * @param string $title |
||
393 | * |
||
394 | * @return Page |
||
395 | */ |
||
396 | public function atom ($href, $title = 'Atom Feed') { |
||
406 | /** |
||
407 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
408 | * |
||
409 | * @param string $href |
||
410 | * @param string $title |
||
411 | * |
||
412 | * @return Page |
||
413 | */ |
||
414 | public function rss ($href, $title = 'RSS Feed') { |
||
424 | /** |
||
425 | * Specify canonical url of current page |
||
426 | * |
||
427 | * @param string $url |
||
428 | * |
||
429 | * @return Page |
||
430 | */ |
||
431 | public function canonical_url ($url) { |
||
439 | /** |
||
440 | * Adding text to the title page |
||
441 | * |
||
442 | * @param string $title |
||
443 | * @param bool $replace Replace whole title by this |
||
444 | * |
||
445 | * @return Page |
||
446 | */ |
||
447 | 2 | public function title ($title, $replace = false) { |
|
456 | /** |
||
457 | * Display success message |
||
458 | * |
||
459 | * @param string $success_text |
||
460 | * |
||
461 | * @return Page |
||
462 | */ |
||
463 | public function success ($success_text) { |
||
466 | /** |
||
467 | * Display notice message |
||
468 | * |
||
469 | * @param string $notice_text |
||
470 | * |
||
471 | * @return Page |
||
472 | */ |
||
473 | public function notice ($notice_text) { |
||
476 | /** |
||
477 | * Display warning message |
||
478 | * |
||
479 | * @param string $warning_text |
||
480 | * |
||
481 | * @return Page |
||
482 | */ |
||
483 | 6 | public function warning ($warning_text) { |
|
486 | /** |
||
487 | * Generic method for 3 methods above |
||
488 | * |
||
489 | * @param string $message |
||
490 | * @param string $class_ending |
||
491 | * |
||
492 | * @return Page |
||
493 | */ |
||
494 | 6 | protected function top_message ($message, $class_ending) { |
|
503 | /** |
||
504 | * Error pages processing |
||
505 | * |
||
506 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
507 | * @param bool $json Force JSON return format |
||
508 | */ |
||
509 | 4 | public function error ($custom_text = null, $json = false) { |
|
536 | /** |
||
537 | * @param int $code |
||
538 | * @param null|string|string[] $custom_text |
||
539 | * |
||
540 | * @return string[] |
||
541 | */ |
||
542 | 4 | protected function error_title_description ($code, $custom_text) { |
|
550 | /** |
||
551 | * @param string $title |
||
552 | * @param string $description |
||
553 | * |
||
554 | * @return string |
||
555 | */ |
||
556 | 4 | protected function error_page ($title, $description) { |
|
569 | /** |
||
570 | * Provides next events: |
||
571 | * System/Page/render/before |
||
572 | * |
||
573 | * System/Page/render/after |
||
574 | * |
||
575 | * Page generation |
||
576 | */ |
||
577 | 6 | public function render () { |
|
606 | } |
||
607 |