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 |
||
42 | class View extends \yii\base\View |
||
43 | { |
||
44 | /** |
||
45 | * @event Event an event that is triggered by [[beginBody()]]. |
||
46 | */ |
||
47 | const EVENT_BEGIN_BODY = 'beginBody'; |
||
48 | /** |
||
49 | * @event Event an event that is triggered by [[endBody()]]. |
||
50 | */ |
||
51 | const EVENT_END_BODY = 'endBody'; |
||
52 | /** |
||
53 | * The location of registered JavaScript code block or files. |
||
54 | * This means the location is in the head section. |
||
55 | */ |
||
56 | const POS_HEAD = 1; |
||
57 | /** |
||
58 | * The location of registered JavaScript code block or files. |
||
59 | * This means the location is at the beginning of the body section. |
||
60 | */ |
||
61 | const POS_BEGIN = 2; |
||
62 | /** |
||
63 | * The location of registered JavaScript code block or files. |
||
64 | * This means the location is at the end of the body section. |
||
65 | */ |
||
66 | const POS_END = 3; |
||
67 | /** |
||
68 | * The location of registered JavaScript code block. |
||
69 | * This means the JavaScript code block will be enclosed within `jQuery(document).ready()`. |
||
70 | */ |
||
71 | const POS_READY = 4; |
||
72 | /** |
||
73 | * The location of registered JavaScript code block. |
||
74 | * This means the JavaScript code block will be enclosed within `jQuery(window).load()`. |
||
75 | */ |
||
76 | const POS_LOAD = 5; |
||
77 | /** |
||
78 | * This is internally used as the placeholder for receiving the content registered for the head section. |
||
79 | */ |
||
80 | const PH_HEAD = '<![CDATA[YII-BLOCK-HEAD]]>'; |
||
81 | /** |
||
82 | * This is internally used as the placeholder for receiving the content registered for the beginning of the body section. |
||
83 | */ |
||
84 | const PH_BODY_BEGIN = '<![CDATA[YII-BLOCK-BODY-BEGIN]]>'; |
||
85 | /** |
||
86 | * This is internally used as the placeholder for receiving the content registered for the end of the body section. |
||
87 | */ |
||
88 | const PH_BODY_END = '<![CDATA[YII-BLOCK-BODY-END]]>'; |
||
89 | |||
90 | /** |
||
91 | * @var AssetBundle[] list of the registered asset bundles. The keys are the bundle names, and the values |
||
92 | * are the registered [[AssetBundle]] objects. |
||
93 | * @see registerAssetBundle() |
||
94 | */ |
||
95 | public $assetBundles = []; |
||
96 | /** |
||
97 | * @var string the page title |
||
98 | */ |
||
99 | public $title; |
||
100 | /** |
||
101 | * @var array the registered meta tags. |
||
102 | * @see registerMetaTag() |
||
103 | */ |
||
104 | public $metaTags; |
||
105 | /** |
||
106 | * @var array the registered link tags. |
||
107 | * @see registerLinkTag() |
||
108 | */ |
||
109 | public $linkTags; |
||
110 | /** |
||
111 | * @var array the registered CSS code blocks. |
||
112 | * @see registerCss() |
||
113 | */ |
||
114 | public $css; |
||
115 | /** |
||
116 | * @var array the registered CSS files. |
||
117 | * @see registerCssFile() |
||
118 | */ |
||
119 | public $cssFiles; |
||
120 | /** |
||
121 | * @var array the registered JS code blocks |
||
122 | * @see registerJs() |
||
123 | */ |
||
124 | public $js; |
||
125 | /** |
||
126 | * @var array the registered JS files. |
||
127 | * @see registerJsFile() |
||
128 | */ |
||
129 | public $jsFiles; |
||
130 | |||
131 | private $_assetManager; |
||
132 | |||
133 | |||
134 | /** |
||
135 | * Marks the position of an HTML head section. |
||
136 | */ |
||
137 | 10 | public function head() |
|
141 | |||
142 | /** |
||
143 | * Marks the beginning of an HTML body section. |
||
144 | */ |
||
145 | 10 | public function beginBody() |
|
146 | { |
||
147 | 10 | echo self::PH_BODY_BEGIN; |
|
148 | 10 | $this->trigger(self::EVENT_BEGIN_BODY); |
|
149 | 10 | } |
|
150 | |||
151 | /** |
||
152 | * Marks the ending of an HTML body section. |
||
153 | */ |
||
154 | 10 | public function endBody() |
|
155 | { |
||
156 | 10 | $this->trigger(self::EVENT_END_BODY); |
|
157 | 10 | echo self::PH_BODY_END; |
|
158 | |||
159 | 10 | foreach (array_keys($this->assetBundles) as $bundle) { |
|
160 | 10 | $this->registerAssetFiles($bundle); |
|
161 | 10 | } |
|
162 | 10 | } |
|
163 | |||
164 | /** |
||
165 | * Marks the ending of an HTML page. |
||
166 | * @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
||
167 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||
168 | * will be rendered at the end of the view like normal scripts. |
||
169 | */ |
||
170 | 10 | public function endPage($ajaxMode = false) |
|
171 | { |
||
172 | 10 | $this->trigger(self::EVENT_END_PAGE); |
|
173 | |||
174 | 10 | $content = ob_get_clean(); |
|
175 | |||
176 | 10 | echo strtr($content, [ |
|
177 | 10 | self::PH_HEAD => $this->renderHeadHtml(), |
|
178 | 10 | self::PH_BODY_BEGIN => $this->renderBodyBeginHtml(), |
|
179 | 10 | self::PH_BODY_END => $this->renderBodyEndHtml($ajaxMode), |
|
180 | 10 | ]); |
|
181 | |||
182 | 10 | $this->clear(); |
|
183 | 10 | } |
|
184 | |||
185 | /** |
||
186 | * Renders a view in response to an AJAX request. |
||
187 | * |
||
188 | * This method is similar to [[render()]] except that it will surround the view being rendered |
||
189 | * with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]]. |
||
190 | * By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files |
||
191 | * that are registered with the view. |
||
192 | * |
||
193 | * @param string $view the view name. Please refer to [[render()]] on how to specify this parameter. |
||
194 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
195 | * @param object $context the context that the view should use for rendering the view. If null, |
||
196 | * existing [[context]] will be used. |
||
197 | * @return string the rendering result |
||
198 | * @see render() |
||
199 | */ |
||
200 | public function renderAjax($view, $params = [], $context = null) |
||
201 | { |
||
202 | $viewFile = $this->findViewFile($view, $context); |
||
203 | |||
204 | ob_start(); |
||
205 | ob_implicit_flush(false); |
||
206 | |||
207 | $this->beginPage(); |
||
208 | $this->head(); |
||
209 | $this->beginBody(); |
||
210 | echo $this->renderFile($viewFile, $params, $context); |
||
|
|||
211 | $this->endBody(); |
||
212 | $this->endPage(true); |
||
213 | |||
214 | return ob_get_clean(); |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * Registers the asset manager being used by this view object. |
||
219 | * @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component. |
||
220 | */ |
||
221 | 22 | public function getAssetManager() |
|
225 | |||
226 | /** |
||
227 | * Sets the asset manager. |
||
228 | * @param \yii\web\AssetManager $value the asset manager |
||
229 | */ |
||
230 | 39 | public function setAssetManager($value) |
|
234 | |||
235 | /** |
||
236 | * Clears up the registered meta tags, link tags, css/js scripts and files. |
||
237 | */ |
||
238 | 10 | public function clear() |
|
239 | { |
||
240 | 10 | $this->metaTags = null; |
|
241 | 10 | $this->linkTags = null; |
|
242 | 10 | $this->css = null; |
|
243 | 10 | $this->cssFiles = null; |
|
244 | 10 | $this->js = null; |
|
245 | 10 | $this->jsFiles = null; |
|
246 | 10 | $this->assetBundles = []; |
|
247 | 10 | } |
|
248 | |||
249 | /** |
||
250 | * Registers all files provided by an asset bundle including depending bundles files. |
||
251 | * Removes a bundle from [[assetBundles]] once files are registered. |
||
252 | * @param string $name name of the bundle to register |
||
253 | */ |
||
254 | 10 | protected function registerAssetFiles($name) |
|
268 | |||
269 | /** |
||
270 | * Registers the named asset bundle. |
||
271 | * All dependent asset bundles will be registered. |
||
272 | * @param string $name the class name of the asset bundle (without the leading backslash) |
||
273 | * @param integer|null $position if set, this forces a minimum position for javascript files. |
||
274 | * This will adjust depending assets javascript file position or fail if requirement can not be met. |
||
275 | * If this is null, asset bundles position settings will not be changed. |
||
276 | * See [[registerJsFile]] for more details on javascript position. |
||
277 | * @return AssetBundle the registered asset bundle instance |
||
278 | * @throws InvalidConfigException if the asset bundle does not exist or a circular dependency is detected |
||
279 | */ |
||
280 | 22 | public function registerAssetBundle($name, $position = null) |
|
281 | { |
||
282 | 22 | if (!isset($this->assetBundles[$name])) { |
|
283 | 21 | $am = $this->getAssetManager(); |
|
284 | 21 | $bundle = $am->getBundle($name); |
|
285 | 21 | $this->assetBundles[$name] = false; |
|
286 | // register dependencies |
||
287 | 21 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|
288 | 21 | foreach ($bundle->depends as $dep) { |
|
289 | 15 | $this->registerAssetBundle($dep, $pos); |
|
290 | 20 | } |
|
291 | 20 | $this->assetBundles[$name] = $bundle; |
|
292 | 22 | } elseif ($this->assetBundles[$name] === false) { |
|
293 | 1 | throw new InvalidConfigException("A circular dependency is detected for bundle '$name'."); |
|
294 | } else { |
||
295 | 10 | $bundle = $this->assetBundles[$name]; |
|
296 | } |
||
297 | |||
298 | 21 | if ($position !== null) { |
|
299 | 12 | $pos = isset($bundle->jsOptions['position']) ? $bundle->jsOptions['position'] : null; |
|
300 | 12 | if ($pos === null) { |
|
301 | 12 | $bundle->jsOptions['position'] = $pos = $position; |
|
302 | 12 | } elseif ($pos > $position) { |
|
303 | 6 | throw new InvalidConfigException("An asset bundle that depends on '$name' has a higher javascript file position configured than '$name'."); |
|
304 | } |
||
305 | // update position for all dependencies |
||
306 | 12 | foreach ($bundle->depends as $dep) { |
|
307 | 6 | $this->registerAssetBundle($dep, $pos); |
|
308 | 12 | } |
|
309 | 12 | } |
|
310 | |||
311 | 21 | return $bundle; |
|
312 | } |
||
313 | |||
314 | /** |
||
315 | * Registers a meta tag. |
||
316 | * |
||
317 | * For example, a description meta tag can be added like the following: |
||
318 | * |
||
319 | * ```php |
||
320 | * $view->registerMetaTag([ |
||
321 | * 'name' => 'description', |
||
322 | * 'content' => 'This website is about funny raccoons.' |
||
323 | * ]); |
||
324 | * ``` |
||
325 | * |
||
326 | * will result in the meta tag `<meta name="description" content="This website is about funny raccoons.">`. |
||
327 | * |
||
328 | * @param array $options the HTML attributes for the meta tag. |
||
329 | * @param string $key the key that identifies the meta tag. If two meta tags are registered |
||
330 | * with the same key, the latter will overwrite the former. If this is null, the new meta tag |
||
331 | * will be appended to the existing ones. |
||
332 | */ |
||
333 | public function registerMetaTag($options, $key = null) |
||
334 | { |
||
335 | if ($key === null) { |
||
336 | $this->metaTags[] = Html::tag('meta', '', $options); |
||
337 | } else { |
||
338 | $this->metaTags[$key] = Html::tag('meta', '', $options); |
||
339 | } |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Registers a link tag. |
||
344 | * |
||
345 | * For example, a link tag for a custom [favicon](http://www.w3.org/2005/10/howto-favicon) |
||
346 | * can be added like the following: |
||
347 | * |
||
348 | * ```php |
||
349 | * $view->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => '/myicon.png']); |
||
350 | * ``` |
||
351 | * |
||
352 | * which will result in the following HTML: `<link rel="icon" type="image/png" href="/myicon.png">`. |
||
353 | * |
||
354 | * **Note:** To register link tags for CSS stylesheets, use [[registerCssFile()]] instead, which |
||
355 | * has more options for this kind of link tag. |
||
356 | * |
||
357 | * @param array $options the HTML attributes for the link tag. |
||
358 | * @param string $key the key that identifies the link tag. If two link tags are registered |
||
359 | * with the same key, the latter will overwrite the former. If this is null, the new link tag |
||
360 | * will be appended to the existing ones. |
||
361 | */ |
||
362 | public function registerLinkTag($options, $key = null) |
||
363 | { |
||
364 | if ($key === null) { |
||
365 | $this->linkTags[] = Html::tag('link', '', $options); |
||
366 | } else { |
||
367 | $this->linkTags[$key] = Html::tag('link', '', $options); |
||
368 | } |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Registers a CSS code block. |
||
373 | * @param string $css the content of the CSS code block to be registered |
||
374 | * @param array $options the HTML attributes for the `<style>`-tag. |
||
375 | * @param string $key the key that identifies the CSS code block. If null, it will use |
||
376 | * $css as the key. If two CSS code blocks are registered with the same key, the latter |
||
377 | * will overwrite the former. |
||
378 | */ |
||
379 | public function registerCss($css, $options = [], $key = null) |
||
380 | { |
||
381 | $key = $key ?: md5($css); |
||
382 | $this->css[$key] = Html::style($css, $options); |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * Registers a CSS file. |
||
387 | * @param string $url the CSS file to be registered. |
||
388 | * @param array $options the HTML attributes for the link tag. Please refer to [[Html::cssFile()]] for |
||
389 | * the supported options. The following options are specially handled and are not treated as HTML attributes: |
||
390 | * |
||
391 | * - `depends`: array, specifies the names of the asset bundles that this CSS file depends on. |
||
392 | * |
||
393 | * @param string $key the key that identifies the CSS script file. If null, it will use |
||
394 | * $url as the key. If two CSS files are registered with the same key, the latter |
||
395 | * will overwrite the former. |
||
396 | */ |
||
397 | 8 | public function registerCssFile($url, $options = [], $key = null) |
|
398 | { |
||
399 | 8 | $url = Yii::getAlias($url); |
|
400 | 8 | $key = $key ?: $url; |
|
401 | 8 | $depends = ArrayHelper::remove($options, 'depends', []); |
|
402 | |||
403 | 8 | if (empty($depends)) { |
|
404 | 8 | $this->cssFiles[$key] = Html::cssFile($url, $options); |
|
405 | 8 | } else { |
|
406 | $this->getAssetManager()->bundles[$key] = new AssetBundle([ |
||
407 | 'baseUrl' => '', |
||
408 | 'css' => [strncmp($url, '//', 2) === 0 ? $url : ltrim($url, '/')], |
||
409 | 'cssOptions' => $options, |
||
410 | 'depends' => (array) $depends, |
||
411 | ]); |
||
412 | $this->registerAssetBundle($key); |
||
413 | } |
||
414 | 8 | } |
|
415 | |||
416 | /** |
||
417 | * Registers a JS code block. |
||
418 | * @param string $js the JS code block to be registered |
||
419 | * @param integer $position the position at which the JS script tag should be inserted |
||
420 | * in a page. The possible values are: |
||
421 | * |
||
422 | * - [[POS_HEAD]]: in the head section |
||
423 | * - [[POS_BEGIN]]: at the beginning of the body section |
||
424 | * - [[POS_END]]: at the end of the body section |
||
425 | * - [[POS_LOAD]]: enclosed within jQuery(window).load(). |
||
426 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
427 | * - [[POS_READY]]: enclosed within jQuery(document).ready(). This is the default value. |
||
428 | * Note that by using this position, the method will automatically register the jQuery js file. |
||
429 | * |
||
430 | * @param string $key the key that identifies the JS code block. If null, it will use |
||
431 | * $js as the key. If two JS code blocks are registered with the same key, the latter |
||
432 | * will overwrite the former. |
||
433 | */ |
||
434 | public function registerJs($js, $position = self::POS_READY, $key = null) |
||
442 | |||
443 | /** |
||
444 | * Registers a JS file. |
||
445 | * @param string $url the JS file to be registered. |
||
446 | * @param array $options the HTML attributes for the script tag. The following options are specially handled |
||
447 | * and are not treated as HTML attributes: |
||
448 | * |
||
449 | * - `depends`: array, specifies the names of the asset bundles that this JS file depends on. |
||
450 | * - `position`: specifies where the JS script tag should be inserted in a page. The possible values are: |
||
451 | * * [[POS_HEAD]]: in the head section |
||
452 | * * [[POS_BEGIN]]: at the beginning of the body section |
||
453 | * * [[POS_END]]: at the end of the body section. This is the default value. |
||
454 | * |
||
455 | * Please refer to [[Html::jsFile()]] for other supported options. |
||
456 | * |
||
457 | * @param string $key the key that identifies the JS script file. If null, it will use |
||
458 | * $url as the key. If two JS files are registered with the same key, the latter |
||
459 | * will overwrite the former. |
||
460 | */ |
||
461 | 10 | public function registerJsFile($url, $options = [], $key = null) |
|
480 | |||
481 | /** |
||
482 | * Renders the content to be inserted in the head section. |
||
483 | * The content is rendered using the registered meta tags, link tags, CSS/JS code blocks and files. |
||
484 | * @return string the rendered content |
||
485 | */ |
||
486 | 10 | protected function renderHeadHtml() |
|
511 | |||
512 | /** |
||
513 | * Renders the content to be inserted at the beginning of the body section. |
||
514 | * The content is rendered using the registered JS code blocks and files. |
||
515 | * @return string the rendered content |
||
516 | */ |
||
517 | 10 | protected function renderBodyBeginHtml() |
|
529 | |||
530 | /** |
||
531 | * Renders the content to be inserted at the end of the body section. |
||
532 | * The content is rendered using the registered JS code blocks and files. |
||
533 | * @param boolean $ajaxMode whether the view is rendering in AJAX mode. |
||
534 | * If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions |
||
535 | * will be rendered at the end of the view like normal scripts. |
||
536 | * @return string the rendered content |
||
537 | */ |
||
538 | 10 | protected function renderBodyEndHtml($ajaxMode) |
|
576 | } |
||
577 |
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.