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 | const INIT_STATE_METHOD = 'init'; |
||
22 | /** |
||
23 | * Complete page contents |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | public $Content; |
||
28 | /** |
||
29 | * 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) |
||
30 | * |
||
31 | * @var bool |
||
32 | */ |
||
33 | public $interface; |
||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | public $pre_Html; |
||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | public $Html; |
||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | public $Description; |
||
46 | /** |
||
47 | * @var string|string[] |
||
48 | */ |
||
49 | public $Title; |
||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | public $Head; |
||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | public $pre_Body; |
||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | public $Left; |
||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | public $Top; |
||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | public $Right; |
||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | public $Bottom; |
||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | public $post_Body; |
||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | public $post_Html; |
||
82 | /** |
||
83 | * Number of tabs by default for indentation the substitution of values into template |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | public $level; |
||
88 | /** |
||
89 | * @var array[] |
||
90 | */ |
||
91 | protected $link; |
||
92 | /** |
||
93 | * @var string[] |
||
94 | */ |
||
95 | protected $search_replace; |
||
96 | /** |
||
97 | * @var false|string |
||
98 | */ |
||
99 | protected $canonical_url; |
||
100 | protected $theme; |
||
101 | protected $finish_called_once; |
||
102 | /** |
||
103 | * @param string $property |
||
104 | * |
||
105 | * @return false|null|string |
||
106 | */ |
||
107 | function __get ($property) { |
||
166 | /** |
||
167 | * Initialization: setting of title and theme according to system configuration |
||
168 | * |
||
169 | * @todo Probably, make public and add resetting here in order to avoid complete object recreation |
||
170 | * |
||
171 | * @return Page |
||
172 | */ |
||
173 | protected function init_config () { |
||
179 | /** |
||
180 | * Theme changing |
||
181 | * |
||
182 | * @param string $theme |
||
183 | * |
||
184 | * @return Page |
||
185 | */ |
||
186 | function set_theme ($theme) { |
||
190 | /** |
||
191 | * Adding of content on the page |
||
192 | * |
||
193 | * @param string $add |
||
194 | * @param bool|int $level |
||
195 | * |
||
196 | * @return Page |
||
197 | */ |
||
198 | function content ($add, $level = false) { |
||
206 | /** |
||
207 | * Sets body with content, that is transformed into JSON format |
||
208 | * |
||
209 | * @param mixed $add |
||
210 | * |
||
211 | * @return Page |
||
212 | */ |
||
213 | function json ($add) { |
||
219 | /** |
||
220 | * Loading of theme template |
||
221 | */ |
||
222 | protected function get_template () { |
||
235 | /** |
||
236 | * Processing of template, substituting of content, preparing for the output |
||
237 | * |
||
238 | * @return Page |
||
239 | */ |
||
240 | protected function prepare () { |
||
330 | /** |
||
331 | * @return string |
||
332 | */ |
||
333 | protected function get_favicon_path () { |
||
340 | /** |
||
341 | * @param string $property |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | protected function get_property_with_indentation ($property) { |
||
348 | /** |
||
349 | * Replacing anything in source code of finally generated page |
||
350 | * |
||
351 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
352 | * |
||
353 | * @param string|string[] $search |
||
354 | * @param string|string[] $replace |
||
355 | * |
||
356 | * @return Page |
||
357 | */ |
||
358 | function replace ($search, $replace = '') { |
||
367 | /** |
||
368 | * Processing of replacing in content |
||
369 | * |
||
370 | * @param string $content |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function process_replacing ($content) { |
||
381 | /** |
||
382 | * Adding links |
||
383 | * |
||
384 | * @param array $data According to h class syntax |
||
385 | * |
||
386 | * @return Page |
||
387 | */ |
||
388 | function link ($data) { |
||
394 | /** |
||
395 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
396 | * |
||
397 | * @param string $href |
||
398 | * @param string $title |
||
399 | * |
||
400 | * @return Page |
||
401 | */ |
||
402 | function atom ($href, $title = 'Atom Feed') { |
||
412 | /** |
||
413 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
414 | * |
||
415 | * @param string $href |
||
416 | * @param string $title |
||
417 | * |
||
418 | * @return Page |
||
419 | */ |
||
420 | function rss ($href, $title = 'RSS Feed') { |
||
430 | /** |
||
431 | * Specify canonical url of current page |
||
432 | * |
||
433 | * @param string $url |
||
434 | * |
||
435 | * @return Page |
||
436 | */ |
||
437 | function canonical_url ($url) { |
||
445 | /** |
||
446 | * Adding text to the title page |
||
447 | * |
||
448 | * @param string $title |
||
449 | * @param bool $replace Replace whole title by this |
||
450 | * |
||
451 | * @return Page |
||
452 | */ |
||
453 | function title ($title, $replace = false) { |
||
462 | /** |
||
463 | * Display success message |
||
464 | * |
||
465 | * @param string $success_text |
||
466 | * |
||
467 | * @return Page |
||
468 | */ |
||
469 | function success ($success_text) { |
||
472 | /** |
||
473 | * Display notice message |
||
474 | * |
||
475 | * @param string $notice_text |
||
476 | * |
||
477 | * @return Page |
||
478 | */ |
||
479 | function notice ($notice_text) { |
||
482 | /** |
||
483 | * Display warning message |
||
484 | * |
||
485 | * @param string $warning_text |
||
486 | * |
||
487 | * @return Page |
||
488 | */ |
||
489 | function warning ($warning_text) { |
||
492 | /** |
||
493 | * Generic method for 3 methods above |
||
494 | * |
||
495 | * @param string $message |
||
496 | * @param string $class_ending |
||
497 | * |
||
498 | * @return Page |
||
499 | */ |
||
500 | protected function top_message ($message, $class_ending) { |
||
509 | /** |
||
510 | * Error pages processing |
||
511 | * |
||
512 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
513 | * @param bool $json Force JSON return format |
||
514 | */ |
||
515 | function error ($custom_text = null, $json = false) { |
||
556 | /** |
||
557 | * Provides next events: |
||
558 | * System/Page/render/before |
||
559 | * |
||
560 | * System/Page/render/after |
||
561 | * |
||
562 | * Page generation |
||
563 | */ |
||
564 | function render () { |
||
600 | } |
||
601 |