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 | protected $finish_called_once; |
||
122 | /** |
||
123 | * @param string $property |
||
124 | * |
||
125 | * @return false|null|string |
||
126 | */ |
||
127 | function __get ($property) { |
||
186 | /** |
||
187 | * Adding of content on the page |
||
188 | * |
||
189 | * @param string $add |
||
190 | * @param bool|int $level |
||
191 | * |
||
192 | * @return Page |
||
193 | */ |
||
194 | function content ($add, $level = false) { |
||
202 | /** |
||
203 | * Sets body with content, that is transformed into JSON format |
||
204 | * |
||
205 | * @param mixed $add |
||
206 | * |
||
207 | * @return Page |
||
208 | */ |
||
209 | function json ($add) { |
||
215 | /** |
||
216 | * Loading of theme template |
||
217 | */ |
||
218 | protected function get_template () { |
||
230 | /** |
||
231 | * Processing of template, substituting of content, preparing for the output |
||
232 | * |
||
233 | * @return Page |
||
234 | */ |
||
235 | protected function prepare () { |
||
326 | /** |
||
327 | * @return string |
||
328 | */ |
||
329 | protected function get_favicon_path () { |
||
336 | /** |
||
337 | * @param string $property |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | protected function get_property_with_indentation ($property) { |
||
344 | /** |
||
345 | * Replacing anything in source code of finally generated page |
||
346 | * |
||
347 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
348 | * |
||
349 | * @param string|string[] $search |
||
350 | * @param string|string[] $replace |
||
351 | * |
||
352 | * @return Page |
||
353 | */ |
||
354 | function replace ($search, $replace = '') { |
||
363 | /** |
||
364 | * Processing of replacing in content |
||
365 | * |
||
366 | * @param string $content |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | protected function process_replacing ($content) { |
||
377 | /** |
||
378 | * Adding links |
||
379 | * |
||
380 | * @param array $data According to h class syntax |
||
381 | * |
||
382 | * @return Page |
||
383 | */ |
||
384 | function link ($data) { |
||
390 | /** |
||
391 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
392 | * |
||
393 | * @param string $href |
||
394 | * @param string $title |
||
395 | * |
||
396 | * @return Page |
||
397 | */ |
||
398 | function atom ($href, $title = 'Atom Feed') { |
||
408 | /** |
||
409 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
410 | * |
||
411 | * @param string $href |
||
412 | * @param string $title |
||
413 | * |
||
414 | * @return Page |
||
415 | */ |
||
416 | function rss ($href, $title = 'RSS Feed') { |
||
426 | /** |
||
427 | * Specify canonical url of current page |
||
428 | * |
||
429 | * @param string $url |
||
430 | * |
||
431 | * @return Page |
||
432 | */ |
||
433 | function canonical_url ($url) { |
||
441 | /** |
||
442 | * Adding text to the title page |
||
443 | * |
||
444 | * @param string $title |
||
445 | * @param bool $replace Replace whole title by this |
||
446 | * |
||
447 | * @return Page |
||
448 | */ |
||
449 | function title ($title, $replace = false) { |
||
458 | /** |
||
459 | * Display success message |
||
460 | * |
||
461 | * @param string $success_text |
||
462 | * |
||
463 | * @return Page |
||
464 | */ |
||
465 | function success ($success_text) { |
||
468 | /** |
||
469 | * Display notice message |
||
470 | * |
||
471 | * @param string $notice_text |
||
472 | * |
||
473 | * @return Page |
||
474 | */ |
||
475 | function notice ($notice_text) { |
||
478 | /** |
||
479 | * Display warning message |
||
480 | * |
||
481 | * @param string $warning_text |
||
482 | * |
||
483 | * @return Page |
||
484 | */ |
||
485 | function warning ($warning_text) { |
||
488 | /** |
||
489 | * Generic method for 3 methods above |
||
490 | * |
||
491 | * @param string $message |
||
492 | * @param string $class_ending |
||
493 | * |
||
494 | * @return Page |
||
495 | */ |
||
496 | protected function top_message ($message, $class_ending) { |
||
505 | /** |
||
506 | * Error pages processing |
||
507 | * |
||
508 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
509 | * @param bool $json Force JSON return format |
||
510 | */ |
||
511 | function error ($custom_text = null, $json = false) { |
||
538 | /** |
||
539 | * @param int $code |
||
540 | * @param null|string|string[] $custom_text |
||
541 | * |
||
542 | * @return string[] |
||
543 | */ |
||
544 | protected function error_title_description ($code, $custom_text) { |
||
552 | /** |
||
553 | * @param string $title |
||
554 | * @param string $description |
||
555 | * |
||
556 | * @return string |
||
557 | */ |
||
558 | protected function error_page ($title, $description) { |
||
571 | /** |
||
572 | * Provides next events: |
||
573 | * System/Page/render/before |
||
574 | * |
||
575 | * System/Page/render/after |
||
576 | * |
||
577 | * Page generation |
||
578 | */ |
||
579 | function render () { |
||
615 | } |
||
616 |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.