Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class View extends \yii\web\View |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @event \yii\base\Event an event that is triggered by [[endAjax()]]. |
||
| 25 | */ |
||
| 26 | const EVENT_END_AJAX_DEBUG = 'endAjax'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The location of registered JavaScript code block or files. |
||
| 30 | * This means the location is in the head section. |
||
| 31 | */ |
||
| 32 | const POS_JS_BLOCK = 6; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * This is internally used as the placeholder for receiving the content registered for the js block section. |
||
| 36 | */ |
||
| 37 | const PH_JS_BLOCK = '<![CDATA[APP-JS-BLOCK]]>'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var array Body html tag parameters |
||
| 41 | */ |
||
| 42 | public $bodyOptions = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array general options for this view |
||
| 46 | */ |
||
| 47 | public $Opts = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var boolean allow idle timeout of user session |
||
| 51 | */ |
||
| 52 | public $enableIdleTimeout = true; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string simple single page style sheet |
||
| 56 | */ |
||
| 57 | public $pageStyle = ''; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array list of style sheets to be added to the view |
||
| 61 | */ |
||
| 62 | public $pageStyles = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var array list of style sheets to be added to the view after all other styles |
||
| 66 | */ |
||
| 67 | public $pageStylesFinal = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string simple single page javascript file to be added to the view |
||
| 71 | */ |
||
| 72 | public $pagePlugin = ''; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array list of javascript files to be added to the view |
||
| 76 | */ |
||
| 77 | public $pagePlugins = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string simple single javascript function to call on jQuery page ready |
||
| 81 | */ |
||
| 82 | public $pageReadyJS = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var array list of javascript functions to call on jQuery page ready |
||
| 86 | */ |
||
| 87 | public $pageReadyJSs = []; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var array list of javascript functions to call on jQuery page ready (before pageReadyJSs) |
||
| 91 | */ |
||
| 92 | public $pageReadyPriorityJSs = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var array list of javascript functions to call on jQuery page ready (after other page ready js) |
||
| 96 | */ |
||
| 97 | public $pageReadyFinalJSs = []; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var string simple single javascript function to call on jQuery page loaded |
||
| 101 | */ |
||
| 102 | public $pageLoadJS = ''; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var array list of javascript functions to call on jQuery page loaded |
||
| 106 | */ |
||
| 107 | public $pageLoadJSs = []; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array list of javascript functions to call on jQuery page ready (before pageLoadJSs) |
||
| 111 | */ |
||
| 112 | public $pageLoadPriorityJSs = []; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array list of javascript functions to call on jQuery page load (after other page load js) |
||
| 116 | */ |
||
| 117 | public $pageLoadFinalJSs = []; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @var array the registered base tag |
||
| 121 | * @see registerBaseTag() |
||
| 122 | */ |
||
| 123 | public $baseTag; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var string Code for active menu item |
||
| 127 | */ |
||
| 128 | public $activeMenuCode = ''; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add in some controller parameters that we want to make available to all |
||
| 132 | * views without needing to pass them explicitly |
||
| 133 | * |
||
| 134 | * (non-PHPdoc) |
||
| 135 | * @see \yii\base\View::renderFile($viewFile, $params, $context) |
||
| 136 | */ |
||
| 137 | public function renderFile($viewFile, $params = [], $context = null) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Renders a view in response to an AJAX request. |
||
| 152 | * |
||
| 153 | * Identical to \yii\web\View for renderAjax but clears out unwanted jsFiles from being repeated within the ajax request |
||
| 154 | * |
||
| 155 | * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
||
| 156 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
| 157 | * @param object $context the context that the view should use for rendering the view. If null, |
||
| 158 | * existing [[context]] will be used. |
||
| 159 | * @return string the rendering result |
||
| 160 | * @see render() |
||
| 161 | */ |
||
| 162 | public function renderAjaxLocal($view, $params = [], $context = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Renders the content to be inserted in the head section. |
||
| 185 | * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
||
| 186 | * @return string the rendered content |
||
| 187 | */ |
||
| 188 | protected function renderHeadHtml() |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Marks the ending of an HTML page. |
||
| 247 | * @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
||
| 248 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||
| 249 | * will be rendered at the end of the view like normal scripts. |
||
| 250 | */ |
||
| 251 | public function endPage($ajaxMode = false) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Marks the position of the JS Block section. |
||
| 267 | */ |
||
| 268 | public function jsBlock() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Renders the content to be inserted within the default js block |
||
| 275 | * The content is rendered using the registered JS code blocks and files. |
||
| 276 | * @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
||
| 277 | * @return string the rendered content |
||
| 278 | */ |
||
| 279 | protected function renderJsBlockHtml($ajaxMode = false) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Add a page style for rendering later |
||
| 379 | * |
||
| 380 | * @param string $style |
||
| 381 | * @param boolean $start [optional] should style be added to start of existing page styles |
||
| 382 | */ |
||
| 383 | public function addPageStyle($style, $start = false) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Add a page style for rendering later (after all other styles) |
||
| 394 | * |
||
| 395 | * @param string $style |
||
| 396 | * @param boolean $start [optional] add to start of existing array |
||
| 397 | */ |
||
| 398 | public function addFinalPageStyle($style, $start = false) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Add a page javascript plugin for rendering later |
||
| 409 | * |
||
| 410 | * @param string $style |
||
| 411 | * @param boolean $start [optional] add to start of existing array |
||
| 412 | */ |
||
| 413 | public function addPagePlugin($plugin, $start = false) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Add js function to execute on jQuery page ready |
||
| 424 | * |
||
| 425 | * @param string $js |
||
| 426 | * @param boolean $start [optional] add to start of existing array |
||
| 427 | */ |
||
| 428 | public function addPageReadyJS($js, $start = false) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Add priority js function to execute on jQuery page ready |
||
| 439 | * |
||
| 440 | * @param string $js |
||
| 441 | * @param boolean $start [optional] add to start of existing array |
||
| 442 | */ |
||
| 443 | public function addPageReadyPriorityJS($js, $start = false) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Add priority js function to execute on jQuery page ready |
||
| 454 | * |
||
| 455 | * @param string $js |
||
| 456 | * @param boolean $start [optional] add to start of existing array |
||
| 457 | */ |
||
| 458 | public function addPageReadyFinalJS($js, $start = false) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Add js function to execute on jQuery page loaded |
||
| 469 | * |
||
| 470 | * @param string $js |
||
| 471 | * @param boolean $start [optional] add to start of existing array |
||
| 472 | */ |
||
| 473 | public function addPageLoadJS($js, $start = false) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Add priority js function to execute on jQuery page loaded |
||
| 484 | * |
||
| 485 | * @param string $js |
||
| 486 | * @param boolean $start [optional] add to start of existing array |
||
| 487 | */ |
||
| 488 | public function addPageLoadPriorityJS($js, $start = false) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Add priority js function to execute on jQuery page loaded |
||
| 499 | * |
||
| 500 | * @param string $js |
||
| 501 | * @param boolean $start [optional] add to start of existing array |
||
| 502 | */ |
||
| 503 | public function addPageLoadFinalJS($js, $start = false) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Render stylesheet tags for the current view |
||
| 514 | */ |
||
| 515 | public function renderPageStyles() |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Render plugin javascript script tags for the current view |
||
| 533 | */ |
||
| 534 | public function renderPagePlugins() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Render jQuery page ready js function calls |
||
| 549 | * |
||
| 550 | * @param boolean $ajax is function being called as a result of rendering an ajax page |
||
| 551 | */ |
||
| 552 | public function renderPageReadyJS($ajax = false) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Render jQuery page loaded js function calls |
||
| 585 | * |
||
| 586 | * @param boolean $ajax is function being called as a result of rendering an ajax page |
||
| 587 | */ |
||
| 588 | public function renderPageLoadJS($ajax = false) |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Called at the end of an ajax request by Controller::endAjaxResponse() |
||
| 621 | */ |
||
| 622 | public function endAjax() |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Registers a base tag. |
||
| 632 | * @param string $url |
||
| 633 | * @param array $options the HTML attributes for the base tag. |
||
| 634 | */ |
||
| 635 | public function registerBaseTag($url, $options = []) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * (non-PHPdoc) |
||
| 643 | * @see \yii\web\View::registerLinkTag() |
||
| 644 | */ |
||
| 645 | public function registerLinkTag($options, $key = null) |
||
| 652 | |||
| 653 | /** |
||
| 654 | * (non-PHPdoc) |
||
| 655 | * @see \yii\web\View::clear() |
||
| 656 | */ |
||
| 657 | public function clear() |
||
| 675 | |||
| 676 | } |
||
| 677 |