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) { |
||
| 133 | 36 | protected function init () { |
|
| 134 | 36 | $this->Content = ''; |
|
| 135 | 36 | $this->interface = true; |
|
| 136 | 36 | $this->pre_Html = ''; |
|
| 137 | 36 | $this->Html = ''; |
|
| 138 | 36 | $this->Description = ''; |
|
| 139 | 36 | $this->Title = []; |
|
| 140 | 36 | $this->Head = ''; |
|
| 141 | 36 | $this->pre_Body = ''; |
|
| 142 | 36 | $this->Left = ''; |
|
| 143 | 36 | $this->Top = ''; |
|
| 144 | 36 | $this->Right = ''; |
|
| 145 | 36 | $this->Bottom = ''; |
|
| 146 | 36 | $this->post_Body = ''; |
|
| 147 | 36 | $this->post_Html = ''; |
|
| 148 | 36 | $this->level = [ |
|
| 149 | 'Head' => 0, |
||
| 150 | 'pre_Body' => 1, |
||
| 151 | 'Left' => 3, |
||
| 152 | 'Top' => 3, |
||
| 153 | 'Content' => 4, |
||
| 154 | 'Bottom' => 3, |
||
| 155 | 'Right' => 3, |
||
| 156 | 'post_Body' => 1 |
||
| 157 | ]; |
||
| 158 | 36 | $this->link = []; |
|
| 159 | 36 | $this->search_replace = []; |
|
| 160 | 36 | $this->canonical_url = false; |
|
| 161 | 36 | $this->theme = null; |
|
| 162 | 36 | $this->init_includes(); |
|
| 163 | 36 | $Config = Config::instance(true); |
|
| 164 | /** |
||
| 165 | * We need Config for initialization |
||
| 166 | */ |
||
| 167 | 36 | if (!$Config) { |
|
| 168 | 2 | Event::instance()->once( |
|
| 169 | 2 | 'System/Config/init/after', |
|
| 170 | function () { |
||
| 171 | $this->theme = Config::instance()->core['theme']; |
||
| 172 | 2 | } |
|
| 173 | ); |
||
| 174 | } else { |
||
| 175 | 34 | $this->theme = Config::instance()->core['theme']; |
|
| 176 | } |
||
| 177 | 36 | Event::instance()->on( |
|
| 178 | 36 | 'System/Config/changed', |
|
| 179 | 36 | function () { |
|
| 180 | $this->theme = Config::instance()->core['theme']; |
||
| 181 | 36 | } |
|
| 182 | ); |
||
| 183 | 36 | } |
|
| 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) { |
||
| 193 | if ($level !== false) { |
||
| 194 | $this->Content .= h::level($add, $level); |
||
| 195 | } else { |
||
| 196 | $this->Content .= $add; |
||
| 197 | } |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | /** |
||
| 201 | * Sets body with content, that is transformed into JSON format |
||
| 202 | * |
||
| 203 | * @param mixed $content |
||
| 204 | * |
||
| 205 | * @return Page |
||
| 206 | */ |
||
| 207 | 4 | public function json ($content) { |
|
| 208 | 4 | Response::instance()->header('content-type', 'application/json; charset=utf-8'); |
|
| 209 | 4 | $this->interface = false; |
|
| 210 | 4 | $this->Content = _json_encode($content); |
|
| 211 | 4 | return $this; |
|
| 212 | } |
||
| 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 () { |
|
| 234 | 2 | $Config = Config::instance(true); |
|
| 235 | /** |
||
| 236 | * Loading of template |
||
| 237 | */ |
||
| 238 | 2 | $this->get_template(); |
|
| 239 | /** |
||
| 240 | * Forming page title |
||
| 241 | */ |
||
| 242 | 2 | $this->Title = array_filter($this->Title, 'trim'); |
|
| 243 | 2 | array_unshift($this->Title, $Config->core['site_name']); |
|
| 244 | 2 | $this->Title = $Config->core['title_reverse'] ? array_reverse($this->Title) : $this->Title; |
|
| 245 | 2 | $this->Title = implode($Config->core['title_delimiter'] ?: '|', $this->Title); |
|
| 246 | /** |
||
| 247 | * Addition of CSS, JavaScript and Web Components includes |
||
| 248 | */ |
||
| 249 | 2 | $this->add_includes_on_page(); |
|
| 250 | /** |
||
| 251 | * Forming <head> content |
||
| 252 | */ |
||
| 253 | 2 | $this->Head = |
|
| 254 | 2 | h::title($this->Title). |
|
| 255 | 2 | h::meta( |
|
| 256 | [ |
||
| 257 | 'charset' => 'utf-8' |
||
| 258 | 2 | ] |
|
| 259 | ). |
||
| 260 | 2 | h::meta( |
|
| 261 | 2 | $this->Description ? [ |
|
| 262 | 'name' => 'description', |
||
| 263 | 'content' => $this->Description |
||
| 264 | 2 | ] : false |
|
| 265 | ). |
||
| 266 | 2 | h::meta( |
|
| 267 | [ |
||
| 268 | 2 | 'name' => 'generator', |
|
| 269 | 'content' => 'CleverStyle Framework by Mokrynskyi Nazar' |
||
| 270 | ] |
||
| 271 | ). |
||
| 272 | 2 | h::base( |
|
| 273 | 2 | $Config ? [ |
|
| 274 | 2 | 'href' => $Config->base_url().'/' |
|
| 275 | 2 | ] : false |
|
| 276 | ). |
||
| 277 | 2 | $this->Head. |
|
| 278 | 2 | h::link( |
|
| 279 | [ |
||
| 280 | 2 | 'rel' => 'shortcut icon', |
|
| 281 | 2 | 'href' => $this->get_favicon_path() |
|
| 282 | ] |
||
| 283 | ). |
||
| 284 | 2 | h::link(array_values($this->link) ?: false); |
|
| 285 | /** |
||
| 286 | * Generation of Open Graph protocol information |
||
| 287 | */ |
||
| 288 | 2 | Meta::instance()->render(); |
|
| 289 | /** |
||
| 290 | * Substitution of information into template |
||
| 291 | */ |
||
| 292 | 2 | $this->Html = str_replace( |
|
| 293 | [ |
||
| 294 | 2 | '<!--pre_Html-->', |
|
| 295 | '<!--head-->', |
||
| 296 | '<!--pre_Body-->', |
||
| 297 | '<!--left_blocks-->', |
||
| 298 | '<!--top_blocks-->', |
||
| 299 | '<!--content-->', |
||
| 300 | '<!--bottom_blocks-->', |
||
| 301 | '<!--right_blocks-->', |
||
| 302 | '<!--post_Body-->', |
||
| 303 | '<!--post_Html-->' |
||
| 304 | ], |
||
| 305 | _rtrim( |
||
| 306 | [ |
||
| 307 | 2 | $this->pre_Html, |
|
| 308 | 2 | $this->get_property_with_indentation('Head'), |
|
| 309 | 2 | $this->get_property_with_indentation('pre_Body'), |
|
| 310 | 2 | $this->get_property_with_indentation('Left'), |
|
| 311 | 2 | $this->get_property_with_indentation('Top'), |
|
| 312 | 2 | $this->get_property_with_indentation('Content'), |
|
| 313 | 2 | $this->get_property_with_indentation('Bottom'), |
|
| 314 | 2 | $this->get_property_with_indentation('Right'), |
|
| 315 | 2 | $this->get_property_with_indentation('post_Body'), |
|
| 316 | 2 | $this->post_Html |
|
| 317 | ], |
||
| 318 | 2 | "\t" |
|
| 319 | ), |
||
| 320 | 2 | $this->Html |
|
| 321 | ); |
||
| 322 | 2 | return $this; |
|
| 323 | } |
||
| 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 |