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 |
||
44 | class View extends \yii\base\View |
||
45 | { |
||
46 | /** |
||
47 | * @event Event an event that is triggered by [[beginBody()]]. |
||
48 | */ |
||
49 | const EVENT_BEGIN_BODY = 'beginBody'; |
||
50 | /** |
||
51 | * @event Event an event that is triggered by [[endBody()]]. |
||
52 | */ |
||
53 | const EVENT_END_BODY = 'endBody'; |
||
54 | /** |
||
55 | * The location of registered JavaScript code block or files. |
||
56 | * This means the location is in the head section. |
||
57 | */ |
||
58 | const POS_HEAD = 1; |
||
59 | /** |
||
60 | * The location of registered JavaScript code block or files. |
||
61 | * This means the location is at the beginning of the body section. |
||
62 | */ |
||
63 | const POS_BEGIN = 2; |
||
64 | /** |
||
65 | * The location of registered JavaScript code block or files. |
||
66 | * This means the location is at the end of the body section. |
||
67 | */ |
||
68 | const POS_END = 3; |
||
69 | /** |
||
70 | * The location of registered JavaScript code block. |
||
71 | * This means the JavaScript code block will be executed when HTML document composition is ready. |
||
72 | */ |
||
73 | const POS_READY = 4; |
||
74 | /** |
||
75 | * The location of registered JavaScript code block. |
||
76 | * This means the JavaScript code block will be executed when HTML page is completely loaded. |
||
77 | */ |
||
78 | const POS_LOAD = 5; |
||
79 | /** |
||
80 | * This is internally used as the placeholder for receiving the content registered for the head section. |
||
81 | */ |
||
82 | const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>'; |
||
83 | /** |
||
84 | * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. |
||
85 | */ |
||
86 | const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>'; |
||
87 | /** |
||
88 | * This is internally used as the placeholder for receiving the content registered for the end of the body section. |
||
89 | */ |
||
90 | const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; |
||
91 | |||
92 | /** |
||
93 | * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values |
||
94 | * are the registered [[AssetBundle]] objects. |
||
95 | * @see registerAssetBundle() |
||
96 | */ |
||
97 | public $assetBundles = []; |
||
98 | /** |
||
99 | * @var string the page title |
||
100 | */ |
||
101 | public $title; |
||
102 | /** |
||
103 | * @var array the registered meta tags. |
||
104 | * @see registerMetaTag() |
||
105 | */ |
||
106 | public $metaTags = []; |
||
107 | /** |
||
108 | * @var array the registered link tags. |
||
109 | * @see registerLinkTag() |
||
110 | */ |
||
111 | public $linkTags = []; |
||
112 | /** |
||
113 | * @var array the registered CSS code blocks. |
||
114 | * @see registerCss() |
||
115 | */ |
||
116 | public $css = []; |
||
117 | /** |
||
118 | * @var array the registered CSS files. |
||
119 | * @see registerCssFile() |
||
120 | */ |
||
121 | public $cssFiles = []; |
||
122 | /** |
||
123 | * @var array the registered JS code blocks |
||
124 | * @see registerJs() |
||
125 | */ |
||
126 | public $js = []; |
||
127 | /** |
||
128 | * @var array the registered JS files. |
||
129 | * @see registerJsFile() |
||
130 | */ |
||
131 | public $jsFiles = []; |
||
132 | |||
133 | private $_assetManager; |
||
134 | |||
135 | |||
136 | /** |
||
137 | * Marks the position of an HTML head section. |
||
138 | */ |
||
139 | 47 | public function head() |
|
143 | |||
144 | /** |
||
145 | * Marks the beginning of an HTML body section. |
||
146 | */ |
||
147 | 47 | public function beginBody() |
|
152 | |||
153 | /** |
||
154 | * Marks the ending of an HTML body section. |
||
155 | */ |
||
156 | 50 | public function endBody() |
|
157 | { |
||
158 | 50 | $this->trigger(self::EVENT_END_BODY); |
|
159 | 50 | echo self::PH_BODY_END; |
|
160 | |||
161 | 50 | foreach (array_keys($this->assetBundles) as $bundle) { |
|
162 | 10 | $this->registerAssetFiles($bundle); |
|
163 | } |
||
164 | 50 | } |
|
165 | |||
166 | /** |
||
167 | * Marks the ending of an HTML page. |
||
168 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. |
||
169 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||
170 | * will be rendered at the end of the view like normal scripts. |
||
171 | */ |
||
172 | 50 | public function endPage($ajaxMode = false) |
|
173 | { |
||
174 | 50 | $this->trigger(self::EVENT_END_PAGE); |
|
175 | |||
176 | 50 | $content = ob_get_clean(); |
|
177 | |||
178 | 50 | echo strtr($content, [ |
|
179 | 50 | self::PH_HEAD => $this->renderHeadHtml(), |
|
180 | 50 | self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
|
181 | 50 | self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
|
182 | ]); |
||
183 | |||
184 | 50 | $this->clear(); |
|
185 | 50 | } |
|
186 | |||
187 | /** |
||
188 | * Renders a view in response to an AJAX request. |
||
189 | * |
||
190 | * This method is similar to [[render()]] except that it will surround the view being rendered |
||
191 | * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]]. |
||
192 | * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files |
||
193 | * that are registered with the view. |
||
194 | * |
||
195 | * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
||
196 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
197 | * @param object $context the context that the view should use for rendering the view. If null, |
||
198 | * existing [[context]] will be used. |
||
199 | * @return string the rendering result |
||
200 | * @see render() |
||
201 | */ |
||
202 | public function renderAjax($view, $params = [], $context = null) |
||
218 | |||
219 | /** |
||
220 | * Registers the asset manager being used by this view object. |
||
221 | * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component. |
||
222 | */ |
||
223 | 30 | public function getAssetManager() |
|
227 | |||
228 | /** |
||
229 | * Sets the asset manager. |
||
230 | * @param \yii\web\AssetManager $value the asset manager |
||
231 | */ |
||
232 | 67 | public function setAssetManager($value) |
|
236 | |||
237 | /** |
||
238 | * Clears up the registered meta tags, link tags, css/js scripts and files. |
||
239 | */ |
||
240 | 50 | public function clear() |
|
250 | |||
251 | /** |
||
252 | * Registers all files provided by an asset bundle including depending bundles files. |
||
253 | * Removes a bundle from [[assetBundles]] once files are registered. |
||
254 | * @param string $name name of the bundle to register |
||
255 | */ |
||
256 | 10 | protected function registerAssetFiles($name) |
|
257 | { |
||
258 | 10 | if (!isset($this->assetBundles[$name])) { |
|
259 | 8 | return; |
|
260 | } |
||
261 | 10 | $bundle = $this->assetBundles[$name]; |
|
262 | 10 | if ($bundle) { |
|
263 | 10 | foreach ($bundle->depends as $dep) { |
|
264 | 8 | $this->registerAssetFiles($dep); |
|
265 | } |
||
266 | 10 | $bundle->registerAssetFiles($this); |
|
267 | } |
||
268 | 10 | unset($this->assetBundles[$name]); |
|
269 | 10 | } |
|
270 | |||
271 | /** |
||
272 | * Registers the named asset bundle. |
||
273 | * All dependent asset bundles will be registered. |
||
274 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||
275 | * @param int|null $position if set, this forces a minimum position for javascript files. |
||
276 | * This will adjust depending assets javascript file position or fail if requirement can not be met. |
||
277 | * If this is null, asset bundles position settings will not be changed. |
||
278 | * See [[registerJsFile]] for more details on javascript position. |
||
279 | * @return AssetBundle the registered asset bundle instance |
||
280 | * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected |
||
281 | */ |
||
282 | 29 | public function registerAssetBundle($name, $position = null) |
|
283 | { |
||
284 | 29 | if (!isset($this->assetBundles[$name])) { |
|
285 | 28 | $am = $this->getAssetManager(); |
|
286 | 28 | $bundle = $am->getBundle($name); |
|
287 | 28 | $this->assetBundles[$name] = false; |
|
288 | // register dependencies |
||
289 | 28 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|
290 | 28 | foreach ($bundle->depends as $dep) { |
|
291 | 16 | $this->registerAssetBundle($dep, $pos); |
|
292 | } |
||
293 | 27 | $this->assetBundles[$name] = $bundle; |
|
294 | 11 | } elseif ($this->assetBundles[$name] === false) { |
|
295 | 1 | throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); |
|
296 | } else { |
||
297 | 10 | $bundle = $this->assetBundles[$name]; |
|
298 | } |
||
299 | |||
300 | 28 | if ($position !== null) { |
|
301 | 12 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|
302 | 12 | if ($pos === null) { |
|
303 | 12 | $bundle->jsOptions['position'] = $pos = $position; |
|
304 | 6 | } elseif ($pos > $position) { |
|
305 | 6 | throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); |
|
306 | } |
||
307 | // update position for all dependencies |
||
308 | 12 | foreach ($bundle->depends as $dep) { |
|
309 | 6 | $this->registerAssetBundle($dep, $pos); |
|
310 | } |
||
311 | } |
||
312 | |||
313 | 28 | return $bundle; |
|
314 | } |
||
315 | |||
316 | /** |
||
317 | * Registers a meta tag. |
||
318 | * |
||
319 | * For example, a description meta tag can be added like the following: |
||
320 | * |
||
321 | * ```php |
||
322 | * $view->registerMetaTag([ |
||
323 | * 'name' => 'description', |
||
324 | * 'content' => 'This website is about funny raccoons.' |
||
325 | * ]); |
||
326 | * ``` |
||
327 | * |
||
328 | * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
||
329 | * |
||
330 | * @param array $options the HTML attributes for the meta tag. |
||
331 | * @param string $key the key that identifies the meta tag. If two meta tags are registered |
||
332 | * with the same key, the latter will overwrite the former. If this is null, the new meta tag |
||
333 | * will be appended to the existing ones. |
||
334 | */ |
||
335 | public function registerMetaTag($options, $key = null) |
||
343 | |||
344 | /** |
||
345 | * Registers a link tag. |
||
346 | * |
||
347 | * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) |
||
348 | * can be added like the following: |
||
349 | * |
||
350 | * ```php |
||
351 | * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
||
352 | * ``` |
||
353 | * |
||
354 | * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
||
355 | * |
||
356 | * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which |
||
357 | * has more options for this kind of link tag. |
||
358 | * |
||
359 | * @param array $options the HTML attributes for the link tag. |
||
360 | * @param string $key the key that identifies the link tag. If two link tags are registered |
||
361 | * with the same key, the latter will overwrite the former. If this is null, the new link tag |
||
362 | * will be appended to the existing ones. |
||
363 | */ |
||
364 | public function registerLinkTag($options, $key = null) |
||
372 | |||
373 | /** |
||
374 | * Registers CSRF meta tags. |
||
375 | * They are rendered dynamically to retrieve a new CSRF token for each request. |
||
376 | * |
||
377 | * ```php |
||
378 | * $view->registerCsrfMetaTags(); |
||
379 | * ``` |
||
380 | * |
||
381 | * The above code will result in `<meta name="csrf-param" content="[yii\web\Request::$csrfParam]">` |
||
382 | * and `<meta name="csrf-token" content="tTNpWKpdy-bx8ZmIq9R72...K1y8IP3XGkzZA==">` added to the page. |
||
383 | * |
||
384 | * Note: Hidden CSRF input of ActiveForm will be automatically refreshed by calling `window.yii.refreshCsrfToken()` |
||
385 | * from `yii.js`. |
||
386 | * |
||
387 | * @since 2.0.13 |
||
388 | */ |
||
389 | 1 | public function registerCsrfMetaTags() |
|
390 | { |
||
391 | 1 | $this->metaTags['csrf_meta_tags'] = $this->renderDynamic('return yii\helpers\Html::csrfMetaTags();'); |
|
392 | 1 | } |
|
393 | |||
394 | /** |
||
395 | * Registers a CSS code block. |
||
396 | * @param string $css the content of the CSS code block to be registered |
||
397 | * @param array $options the HTML attributes for the `<style>`-tag. |
||
398 | * @param string $key the key that identifies the CSS code block. If null, it will use |
||
399 | * $css as the key. If two CSS code blocks are registered with the same key, the latter |
||
400 | * will overwrite the former. |
||
401 | */ |
||
402 | public function registerCss($css, $options = [], $key = null) |
||
407 | |||
408 | /** |
||
409 | * Registers a CSS file. |
||
410 | * |
||
411 | * This method should be used for simple registration of CSS files. If you want to use features of |
||
412 | * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]] |
||
413 | * and [[registerAssetBundle()]] instead. |
||
414 | * |
||
415 | * @param string $url the CSS file to be registered. |
||
416 | * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for |
||
417 | * the supported options. The following options are specially handled and are not treated as HTML attributes: |
||
418 | * |
||
419 | * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
||
420 | * |
||
421 | * @param string $key the key that identifies the CSS script file. If null, it will use |
||
422 | * $url as the key. If two CSS files are registered with the same key, the latter |
||
423 | * will overwrite the former. |
||
424 | */ |
||
425 | 19 | public function registerCssFile($url, $options = [], $key = null) |
|
445 | |||
446 | /** |
||
447 | * Registers a JS code block. |
||
448 | * @param string $js the JS code block to be registered |
||
449 | * @param int $position the position at which the JS script tag should be inserted |
||
450 | * in a page. The possible values are: |
||
451 | * |
||
452 | * - [[POS_HEAD]]: in the head section |
||
453 | * - [[POS_BEGIN]]: at the beginning of the body section |
||
454 | * - [[POS_END]]: at the end of the body section. This is the default value. |
||
455 | * - [[POS_LOAD]]: executed when HTML page is completely loaded. |
||
456 | * - [[POS_READY]]: executed when HTML document composition is ready. |
||
457 | * |
||
458 | * @param string $key the key that identifies the JS code block. If null, it will use |
||
459 | * $js as the key. If two JS code blocks are registered with the same key, the latter |
||
460 | * will overwrite the former. |
||
461 | */ |
||
462 | 10 | public function registerJs($js, $position = self::POS_END, $key = null) |
|
467 | |||
468 | /** |
||
469 | * Registers a JS file. |
||
470 | * |
||
471 | * This method should be used for simple registration of JS files. If you want to use features of |
||
472 | * [[AssetManager]] like appending timestamps to the URL and file publishing options, use [[AssetBundle]] |
||
473 | * and [[registerAssetBundle()]] instead. |
||
474 | * |
||
475 | * @param string $url the JS file to be registered. |
||
476 | * @param array $options the HTML attributes for the script tag. The following options are specially handled |
||
477 | * and are not treated as HTML attributes: |
||
478 | * |
||
479 | * - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
||
480 | * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
||
481 | * * [[POS_HEAD]]: in the head section |
||
482 | * * [[POS_BEGIN]]: at the beginning of the body section |
||
483 | * * [[POS_END]]: at the end of the body section. This is the default value. |
||
484 | * |
||
485 | * Please refer to [[Html::jsFile()]] for other supported options. |
||
486 | * |
||
487 | * @param string $key the key that identifies the JS script file. If null, it will use |
||
488 | * $url as the key. If two JS files are registered with the same key at the same position, the latter |
||
489 | * will overwrite the former. Note that position option takes precedence, thus files registered with the same key, |
||
490 | * but different position option will not override each other. |
||
491 | */ |
||
492 | 21 | public function registerJsFile($url, $options = [], $key = null) |
|
513 | |||
514 | /** |
||
515 | * Registers a JS code block defining a variable. The name of variable will be |
||
516 | * used as key, preventing duplicated variable names. |
||
517 | * |
||
518 | * @param string $name Name of the variable |
||
519 | * @param array|string $value Value of the variable |
||
520 | * @param int $position the position in a page at which the JavaScript variable should be inserted. |
||
521 | * The possible values are: |
||
522 | * |
||
523 | * - [[POS_HEAD]]: in the head section. This is the default value. |
||
524 | * - [[POS_BEGIN]]: at the beginning of the body section. |
||
525 | * - [[POS_END]]: at the end of the body section. |
||
526 | * - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
||
527 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
528 | * - [[POS_READY]]: enclosed within jQuery(document).ready(). |
||
529 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
530 | * |
||
531 | * @since 2.0.14 |
||
532 | */ |
||
533 | 1 | public function registerJsVar($name, $value, $position = self::POS_HEAD) |
|
538 | |||
539 | /** |
||
540 | * Renders the content to be inserted in the head section. |
||
541 | * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
||
542 | * @return string the rendered content |
||
543 | */ |
||
544 | 50 | protected function renderHeadHtml() |
|
569 | |||
570 | /** |
||
571 | * Renders the content to be inserted at the beginning of the body section. |
||
572 | * The content is rendered using the registered JS code blocks and files. |
||
573 | * @return string the rendered content |
||
574 | */ |
||
575 | 50 | protected function renderBodyBeginHtml() |
|
587 | |||
588 | /** |
||
589 | * Renders the content to be inserted at the end of the body section. |
||
590 | * The content is rendered using the registered JS code blocks and files. |
||
591 | * @param bool $ajaxMode whether the view is rendering in AJAX mode. |
||
592 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||
593 | * will be rendered at the end of the view like normal scripts. |
||
594 | * @return string the rendered content |
||
595 | */ |
||
596 | 50 | protected function renderBodyEndHtml($ajaxMode) |
|
634 | } |
||
635 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.