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 |
||
29 | class View extends Component implements DynamicContentAwareInterface |
||
30 | { |
||
31 | /** |
||
32 | * @event Event an event that is triggered by [[beginPage()]]. |
||
33 | */ |
||
34 | const EVENT_BEGIN_PAGE = 'beginPage'; |
||
35 | /** |
||
36 | * @event Event an event that is triggered by [[endPage()]]. |
||
37 | */ |
||
38 | const EVENT_END_PAGE = 'endPage'; |
||
39 | /** |
||
40 | * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file. |
||
41 | */ |
||
42 | const EVENT_BEFORE_RENDER = 'beforeRender'; |
||
43 | /** |
||
44 | * @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file. |
||
45 | */ |
||
46 | const EVENT_AFTER_RENDER = 'afterRender'; |
||
47 | |||
48 | /** |
||
49 | * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked. |
||
50 | */ |
||
51 | public $context; |
||
52 | /** |
||
53 | * @var mixed custom parameters that are shared among view templates. |
||
54 | */ |
||
55 | public $params = []; |
||
56 | /** |
||
57 | * @var array a list of available renderers indexed by their corresponding supported file extensions. |
||
58 | * Each renderer may be a view renderer object or the configuration for creating the renderer object. |
||
59 | * For example, the following configuration enables both Smarty and Twig view renderers: |
||
60 | * |
||
61 | * ```php |
||
62 | * [ |
||
63 | * 'tpl' => ['class' => 'yii\smarty\ViewRenderer'], |
||
64 | * 'twig' => ['class' => 'yii\twig\ViewRenderer'], |
||
65 | * ] |
||
66 | * ``` |
||
67 | * |
||
68 | * If no renderer is available for the given view file, the view file will be treated as a normal PHP |
||
69 | * and rendered via [[renderPhpFile()]]. |
||
70 | */ |
||
71 | public $renderers; |
||
72 | /** |
||
73 | * @var string the default view file extension. This will be appended to view file names if they don't have file extensions. |
||
74 | */ |
||
75 | public $defaultExtension = 'php'; |
||
76 | /** |
||
77 | * @var Theme|array|string the theme object or the configuration for creating the theme object. |
||
78 | * If not set, it means theming is not enabled. |
||
79 | */ |
||
80 | public $theme; |
||
81 | /** |
||
82 | * @var array a list of named output blocks. The keys are the block names and the values |
||
83 | * are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]] |
||
84 | * to capture small fragments of a view. They can be later accessed somewhere else |
||
85 | * through this property. |
||
86 | */ |
||
87 | public $blocks; |
||
88 | /** |
||
89 | * @var array|DynamicContentAwareInterface[] a list of currently active dynamic content class instances. |
||
90 | * This property is used internally to implement the dynamic content caching feature. Do not modify it directly. |
||
91 | * @internal |
||
92 | * @deprecated Since 2.0.14. Do not use this property directly. Use methods [[getDynamicContents()]], |
||
93 | * [[pushDynamicContent()]], [[popDynamicContent()]] instead. |
||
94 | */ |
||
95 | public $cacheStack = []; |
||
96 | /** |
||
97 | * @var array a list of placeholders for embedding dynamic contents. This property |
||
98 | * is used internally to implement the content caching feature. Do not modify it directly. |
||
99 | * @internal |
||
100 | * @deprecated Since 2.0.14. Do not use this property directly. Use methods [[getDynamicPlaceholders()]], |
||
101 | * [[setDynamicPlaceholders()]], [[addDynamicPlaceholder()]] instead. |
||
102 | */ |
||
103 | public $dynamicPlaceholders = []; |
||
104 | |||
105 | /** |
||
106 | * @var array the view files currently being rendered. There may be multiple view files being |
||
107 | * rendered at a moment because one view may be rendered within another. |
||
108 | */ |
||
109 | private $_viewFiles = []; |
||
110 | |||
111 | |||
112 | /** |
||
113 | * Initializes the view component. |
||
114 | */ |
||
115 | 152 | public function init() |
|
116 | { |
||
117 | 152 | parent::init(); |
|
118 | 152 | if (is_array($this->theme)) { |
|
119 | if (!isset($this->theme['class'])) { |
||
120 | $this->theme['class'] = 'yii\base\Theme'; |
||
121 | } |
||
122 | $this->theme = Yii::createObject($this->theme); |
||
123 | 152 | } elseif (is_string($this->theme)) { |
|
124 | $this->theme = Yii::createObject($this->theme); |
||
125 | } |
||
126 | 152 | } |
|
127 | |||
128 | /** |
||
129 | * Renders a view. |
||
130 | * |
||
131 | * The view to be rendered can be specified in one of the following formats: |
||
132 | * |
||
133 | * - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
||
134 | * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
||
135 | * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
||
136 | * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash. |
||
137 | * The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]]. |
||
138 | * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be |
||
139 | * looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`. |
||
140 | * If `$context` is not given, it will be looked for under the directory containing the view currently |
||
141 | * being rendered (i.e., this happens when rendering a view within another view). |
||
142 | * |
||
143 | * @param string $view the view name. |
||
144 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
145 | * @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
||
146 | * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
||
147 | * the view file corresponding to a relative view name. |
||
148 | * @return string the rendering result |
||
149 | * @throws ViewNotFoundException if the view file does not exist. |
||
150 | * @throws InvalidCallException if the view cannot be resolved. |
||
151 | * @see renderFile() |
||
152 | */ |
||
153 | 36 | public function render($view, $params = [], $context = null) |
|
158 | |||
159 | /** |
||
160 | * Finds the view file based on the given view name. |
||
161 | * @param string $view the view name or the [path alias](guide:concept-aliases) of the view file. Please refer to [[render()]] |
||
162 | * on how to specify this parameter. |
||
163 | * @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
||
164 | * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
||
165 | * the view file corresponding to a relative view name. |
||
166 | * @return string the view file path. Note that the file may not exist. |
||
167 | * @throws InvalidCallException if a relative view name is given while there is no active context to |
||
168 | * determine the corresponding view file. |
||
169 | */ |
||
170 | 36 | protected function findViewFile($view, $context = null) |
|
203 | |||
204 | /** |
||
205 | * Renders a view file. |
||
206 | * |
||
207 | * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long |
||
208 | * as it is available. |
||
209 | * |
||
210 | * The method will call [[FileHelper::localize()]] to localize the view file. |
||
211 | * |
||
212 | * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file. |
||
213 | * Otherwise, it will simply include the view file as a normal PHP file, capture its output and |
||
214 | * return it as a string. |
||
215 | * |
||
216 | * @param string $viewFile the view file. This can be either an absolute file path or an alias of it. |
||
217 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
218 | * @param object $context the context that the view should use for rendering the view. If null, |
||
219 | * existing [[context]] will be used. |
||
220 | * @return string the rendering result |
||
221 | * @throws ViewNotFoundException if the view file does not exist |
||
222 | */ |
||
223 | 76 | public function renderFile($viewFile, $params = [], $context = null) |
|
267 | |||
268 | /** |
||
269 | * @return string|bool the view file currently being rendered. False if no view file is being rendered. |
||
270 | */ |
||
271 | public function getViewFile() |
||
275 | |||
276 | /** |
||
277 | * @return string|bool the requested view currently being rendered. False if no view file is being rendered. |
||
278 | * @since 2.0.16 |
||
279 | */ |
||
280 | 6 | protected function getRequestedViewFile() |
|
284 | |||
285 | /** |
||
286 | * This method is invoked right before [[renderFile()]] renders a view file. |
||
287 | * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. |
||
288 | * If you override this method, make sure you call the parent implementation first. |
||
289 | * @param string $viewFile the view file to be rendered. |
||
290 | * @param array $params the parameter array passed to the [[render()]] method. |
||
291 | * @return bool whether to continue rendering the view file. |
||
292 | */ |
||
293 | 75 | public function beforeRender($viewFile, $params) |
|
303 | |||
304 | /** |
||
305 | * This method is invoked right after [[renderFile()]] renders a view file. |
||
306 | * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. |
||
307 | * If you override this method, make sure you call the parent implementation first. |
||
308 | * @param string $viewFile the view file being rendered. |
||
309 | * @param array $params the parameter array passed to the [[render()]] method. |
||
310 | * @param string $output the rendering result of the view file. Updates to this parameter |
||
311 | * will be passed back and returned by [[renderFile()]]. |
||
312 | */ |
||
313 | 75 | public function afterRender($viewFile, $params, &$output) |
|
325 | |||
326 | /** |
||
327 | * Renders a view file as a PHP script. |
||
328 | * |
||
329 | * This method treats the view file as a PHP script and includes the file. |
||
330 | * It extracts the given parameters and makes them available in the view file. |
||
331 | * The method captures the output of the included view file and returns it as a string. |
||
332 | * |
||
333 | * This method should mainly be called by view renderer or [[renderFile()]]. |
||
334 | * |
||
335 | * @param string $_file_ the view file. |
||
336 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
337 | * @return string the rendering result |
||
338 | * @throws \Exception |
||
339 | * @throws \Throwable |
||
340 | */ |
||
341 | 75 | public function renderPhpFile($_file_, $_params_ = []) |
|
366 | |||
367 | /** |
||
368 | * Renders dynamic content returned by the given PHP statements. |
||
369 | * This method is mainly used together with content caching (fragment caching and page caching) |
||
370 | * when some portions of the content (called *dynamic content*) should not be cached. |
||
371 | * The dynamic content must be returned by some PHP statements. |
||
372 | * @param string $statements the PHP statements for generating the dynamic content. |
||
373 | * @return string the placeholder of the dynamic content, or the dynamic content if there is no |
||
374 | * active content cache currently. |
||
375 | */ |
||
376 | 18 | public function renderDynamic($statements) |
|
388 | |||
389 | /** |
||
390 | * {@inheritdoc} |
||
391 | */ |
||
392 | public function getDynamicPlaceholders() |
||
396 | |||
397 | /** |
||
398 | * {@inheritdoc} |
||
399 | */ |
||
400 | public function setDynamicPlaceholders($placeholders) |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | */ |
||
408 | 16 | public function addDynamicPlaceholder($placeholder, $statements) |
|
420 | |||
421 | /** |
||
422 | * Evaluates the given PHP statements. |
||
423 | * This method is mainly used internally to implement dynamic content feature. |
||
424 | * @param string $statements the PHP statements to be evaluated. |
||
425 | * @return mixed the return value of the PHP statements. |
||
426 | */ |
||
427 | 18 | public function evaluateDynamicContent($statements) |
|
431 | |||
432 | /** |
||
433 | * Returns a list of currently active dynamic content class instances. |
||
434 | * @return DynamicContentAwareInterface[] class instances supporting dynamic contents. |
||
435 | * @since 2.0.14 |
||
436 | */ |
||
437 | 16 | public function getDynamicContents() |
|
441 | |||
442 | /** |
||
443 | * Adds a class instance supporting dynamic contents to the end of a list of currently active |
||
444 | * dynamic content class instances. |
||
445 | * @param DynamicContentAwareInterface $instance class instance supporting dynamic contents. |
||
446 | * @since 2.0.14 |
||
447 | */ |
||
448 | 21 | public function pushDynamicContent(DynamicContentAwareInterface $instance) |
|
452 | |||
453 | /** |
||
454 | * Removes a last class instance supporting dynamic contents from a list of currently active |
||
455 | * dynamic content class instances. |
||
456 | * @since 2.0.14 |
||
457 | */ |
||
458 | 21 | public function popDynamicContent() |
|
462 | |||
463 | /** |
||
464 | * Begins recording a block. |
||
465 | * |
||
466 | * This method is a shortcut to beginning [[Block]]. |
||
467 | * @param string $id the block ID. |
||
468 | * @param bool $renderInPlace whether to render the block content in place. |
||
469 | * Defaults to false, meaning the captured block will not be displayed. |
||
470 | * @return Block the Block widget instance |
||
471 | */ |
||
472 | public function beginBlock($id, $renderInPlace = false) |
||
480 | |||
481 | /** |
||
482 | * Ends recording a block. |
||
483 | */ |
||
484 | public function endBlock() |
||
488 | |||
489 | /** |
||
490 | * Begins the rendering of content that is to be decorated by the specified view. |
||
491 | * |
||
492 | * This method can be used to implement nested layout. For example, a layout can be embedded |
||
493 | * in another layout file specified as '@app/views/layouts/base.php' like the following: |
||
494 | * |
||
495 | * ```php |
||
496 | * <?php $this->beginContent('@app/views/layouts/base.php'); ?> |
||
497 | * //...layout content here... |
||
498 | * <?php $this->endContent(); ?> |
||
499 | * ``` |
||
500 | * |
||
501 | * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget. |
||
502 | * This can be specified as either the view file path or [path alias](guide:concept-aliases). |
||
503 | * @param array $params the variables (name => value) to be extracted and made available in the decorative view. |
||
504 | * @return ContentDecorator the ContentDecorator widget instance |
||
505 | * @see ContentDecorator |
||
506 | */ |
||
507 | public function beginContent($viewFile, $params = []) |
||
515 | |||
516 | /** |
||
517 | * Ends the rendering of content. |
||
518 | */ |
||
519 | public function endContent() |
||
523 | |||
524 | /** |
||
525 | * Begins fragment caching. |
||
526 | * |
||
527 | * This method will display cached content if it is available. |
||
528 | * If not, it will start caching and would expect an [[endCache()]] |
||
529 | * call to end the cache and save the content into cache. |
||
530 | * A typical usage of fragment caching is as follows, |
||
531 | * |
||
532 | * ```php |
||
533 | * if ($this->beginCache($id)) { |
||
534 | * // ...generate content here |
||
535 | * $this->endCache(); |
||
536 | * } |
||
537 | * ``` |
||
538 | * |
||
539 | * @param string $id a unique ID identifying the fragment to be cached. |
||
540 | * @param array $properties initial property values for [[FragmentCache]] |
||
541 | * @return bool whether you should generate the content for caching. |
||
542 | * False if the cached version is available. |
||
543 | */ |
||
544 | 10 | public function beginCache($id, $properties = []) |
|
558 | |||
559 | /** |
||
560 | * Ends fragment caching. |
||
561 | */ |
||
562 | 10 | public function endCache() |
|
566 | |||
567 | /** |
||
568 | * Marks the beginning of a page. |
||
569 | */ |
||
570 | 50 | public function beginPage() |
|
577 | |||
578 | /** |
||
579 | * Marks the ending of a page. |
||
580 | */ |
||
581 | public function endPage() |
||
586 | } |
||
587 |
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.