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 |
||
14 | class Page { |
||
15 | use |
||
16 | Singleton, |
||
17 | Includes; |
||
18 | public $Content; |
||
19 | public $interface = true; |
||
20 | public $pre_Html = ''; |
||
21 | public $Html = ''; |
||
22 | public $Description = ''; |
||
23 | /** |
||
24 | * @var string|string[] |
||
25 | */ |
||
26 | public $Title = []; |
||
27 | public $Head = ''; |
||
28 | public $pre_Body = ''; |
||
29 | public $Left = ''; |
||
30 | public $Top = ''; |
||
31 | public $Right = ''; |
||
32 | public $Bottom = ''; |
||
33 | public $post_Body = ''; |
||
34 | public $post_Html = ''; |
||
35 | /** |
||
36 | * Number of tabs by default for indentation the substitution of values into template |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | public $level = [ |
||
41 | 'Head' => 0, |
||
42 | 'pre_Body' => 1, |
||
43 | 'Left' => 3, |
||
44 | 'Top' => 3, |
||
45 | 'Content' => 4, |
||
46 | 'Bottom' => 3, |
||
47 | 'Right' => 3, |
||
48 | 'post_Body' => 1 |
||
49 | ]; |
||
50 | /** |
||
51 | * @var array[] |
||
52 | */ |
||
53 | protected $link = []; |
||
54 | /** |
||
55 | * @var string[] |
||
56 | */ |
||
57 | protected $search_replace = []; |
||
58 | /** |
||
59 | * @var false|string |
||
60 | */ |
||
61 | protected $canonical_url = false; |
||
62 | protected $theme; |
||
63 | protected $error_showed = false; |
||
64 | protected $finish_called_once = false; |
||
65 | /** |
||
66 | * @param string $property |
||
67 | * |
||
68 | * @return false|null|string |
||
69 | */ |
||
70 | function __get ($property) { |
||
99 | /** |
||
100 | * Initialization: setting of title and theme according to system configuration |
||
101 | * |
||
102 | * @return Page |
||
103 | */ |
||
104 | protected function init () { |
||
110 | /** |
||
111 | * Theme changing |
||
112 | * |
||
113 | * @param string $theme |
||
114 | * |
||
115 | * @return Page |
||
116 | */ |
||
117 | function set_theme ($theme) { |
||
121 | /** |
||
122 | * Adding of content on the page |
||
123 | * |
||
124 | * @param string $add |
||
125 | * @param bool|int $level |
||
126 | * |
||
127 | * @return Page |
||
128 | */ |
||
129 | function content ($add, $level = false) { |
||
137 | /** |
||
138 | * Sets body with content, that is transformed into JSON format |
||
139 | * |
||
140 | * @param mixed $add |
||
141 | * |
||
142 | * @return Page |
||
143 | */ |
||
144 | function json ($add) { |
||
150 | /** |
||
151 | * Loading of theme template |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | protected function get_template () { |
||
187 | /** |
||
188 | * Processing of template, substituting of content, preparing for the output |
||
189 | * |
||
190 | * @return Page |
||
191 | */ |
||
192 | protected function prepare () { |
||
284 | /** |
||
285 | * @return string |
||
286 | */ |
||
287 | protected function get_favicon_path () { |
||
294 | /** |
||
295 | * @param string $property |
||
296 | * |
||
297 | * @return string |
||
298 | */ |
||
299 | protected function get_property_with_indentation ($property) { |
||
302 | /** |
||
303 | * Replacing anything in source code of finally generated page |
||
304 | * |
||
305 | * Parameters may be both simply strings for str_replace() and regular expressions for preg_replace() |
||
306 | * |
||
307 | * @param string|string[] $search |
||
308 | * @param string|string[] $replace |
||
309 | * |
||
310 | * @return Page |
||
311 | */ |
||
312 | function replace ($search, $replace = '') { |
||
320 | /** |
||
321 | * Processing of replacing in content |
||
322 | * |
||
323 | * @param string $content |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function process_replacing ($content) { |
||
334 | /** |
||
335 | * Adding links |
||
336 | * |
||
337 | * @param array $data According to h class syntax |
||
338 | * |
||
339 | * @return Page |
||
340 | */ |
||
341 | function link ($data) { |
||
347 | /** |
||
348 | * Simple wrapper of $Page->link() for inserting Atom feed on page |
||
349 | * |
||
350 | * @param string $href |
||
351 | * @param string $title |
||
352 | * |
||
353 | * @return Page |
||
354 | */ |
||
355 | function atom ($href, $title = 'Atom Feed') { |
||
365 | /** |
||
366 | * Simple wrapper of $Page->link() for inserting RSS feed on page |
||
367 | * |
||
368 | * @param string $href |
||
369 | * @param string $title |
||
370 | * |
||
371 | * @return Page |
||
372 | */ |
||
373 | function rss ($href, $title = 'RSS Feed') { |
||
383 | /** |
||
384 | * Specify canonical url of current page |
||
385 | * |
||
386 | * @param string $url |
||
387 | * |
||
388 | * @return Page |
||
389 | */ |
||
390 | function canonical_url ($url) { |
||
398 | /** |
||
399 | * Adding text to the title page |
||
400 | * |
||
401 | * @param string $title |
||
402 | * @param bool $replace Replace whole title by this |
||
403 | * |
||
404 | * @return Page |
||
405 | */ |
||
406 | function title ($title, $replace = false) { |
||
415 | /** |
||
416 | * Display success message |
||
417 | * |
||
418 | * @param string $success_text |
||
419 | * |
||
420 | * @return Page |
||
421 | */ |
||
422 | function success ($success_text) { |
||
425 | /** |
||
426 | * Display notice message |
||
427 | * |
||
428 | * @param string $notice_text |
||
429 | * |
||
430 | * @return Page |
||
431 | */ |
||
432 | function notice ($notice_text) { |
||
435 | /** |
||
436 | * Display warning message |
||
437 | * |
||
438 | * @param string $warning_text |
||
439 | * |
||
440 | * @return Page |
||
441 | */ |
||
442 | function warning ($warning_text) { |
||
445 | /** |
||
446 | * Generic method for 3 methods above |
||
447 | * |
||
448 | * @param string $message |
||
449 | * @param string $class_ending |
||
450 | * |
||
451 | * @return Page |
||
452 | */ |
||
453 | protected function top_message ($message, $class_ending) { |
||
462 | /** |
||
463 | * Error pages processing |
||
464 | * |
||
465 | * @param null|string|string[] $custom_text Custom error text instead of text like "404 Not Found" or array with two elements: [error, error_description] |
||
466 | * @param bool $json Force JSON return format |
||
467 | * @param int $error_code HTTP status code |
||
468 | * |
||
469 | * @throws ExitException |
||
470 | */ |
||
471 | function error ($custom_text = null, $json = false, $error_code = 500) { |
||
519 | /** |
||
520 | * Provides next events: |
||
521 | * System/Page/display/before |
||
522 | * |
||
523 | * System/Page/display/after |
||
524 | * |
||
525 | * Page generation |
||
526 | */ |
||
527 | function __finish () { |
||
558 | } |
||
559 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.