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::class], |
||
64 | * 'twig' => ['__class' => \yii\twig\ViewRenderer::class], |
||
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 DynamicContentAwareInterface[] a list of currently active dynamic content class instances. |
||
90 | */ |
||
91 | private $_cacheStack = []; |
||
92 | /** |
||
93 | * @var array a list of placeholders for embedding dynamic contents. |
||
94 | */ |
||
95 | private $_dynamicPlaceholders = []; |
||
96 | /** |
||
97 | * @var array the view files currently being rendered. There may be multiple view files being |
||
98 | * rendered at a moment because one view may be rendered within another. |
||
99 | */ |
||
100 | private $_viewFiles = []; |
||
101 | |||
102 | |||
103 | /** |
||
104 | * Initializes the view component. |
||
105 | */ |
||
106 | 152 | public function init() |
|
118 | |||
119 | /** |
||
120 | * Renders a view. |
||
121 | * |
||
122 | * The view to be rendered can be specified in one of the following formats: |
||
123 | * |
||
124 | * - [path alias](guide:concept-aliases) (e.g. "@app/views/site/index"); |
||
125 | * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. |
||
126 | * The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. |
||
127 | * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash. |
||
128 | * The actual view file will be looked for under the [[Module::viewPath|view path]] of the [[Controller::module|current module]]. |
||
129 | * - relative view (e.g. "index"): the view name does not start with `@` or `/`. The corresponding view file will be |
||
130 | * looked for under the [[ViewContextInterface::getViewPath()|view path]] of the view `$context`. |
||
131 | * If `$context` is not given, it will be looked for under the directory containing the view currently |
||
132 | * being rendered (i.e., this happens when rendering a view within another view). |
||
133 | * |
||
134 | * @param string $view the view name. |
||
135 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
136 | * @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
||
137 | * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
||
138 | * the view file corresponding to a relative view name. |
||
139 | * @return string the rendering result |
||
140 | * @throws ViewNotFoundException if the view file does not exist. |
||
141 | * @throws InvalidCallException if the view cannot be resolved. |
||
142 | * @see renderFile() |
||
143 | */ |
||
144 | 37 | public function render($view, $params = [], $context = null) |
|
149 | |||
150 | /** |
||
151 | * Finds the view file based on the given view name. |
||
152 | * @param string $view the view name or the [path alias](guide:concept-aliases) of the view file. Please refer to [[render()]] |
||
153 | * on how to specify this parameter. |
||
154 | * @param object $context the context to be assigned to the view and can later be accessed via [[context]] |
||
155 | * in the view. If the context implements [[ViewContextInterface]], it may also be used to locate |
||
156 | * the view file corresponding to a relative view name. |
||
157 | * @return string the view file path. Note that the file may not exist. |
||
158 | * @throws InvalidCallException if a relative view name is given while there is no active context to |
||
159 | * determine the corresponding view file. |
||
160 | */ |
||
161 | 37 | protected function findViewFile($view, $context = null) |
|
194 | |||
195 | /** |
||
196 | * Renders a view file. |
||
197 | * |
||
198 | * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long |
||
199 | * as it is available. |
||
200 | * |
||
201 | * The method will call [[FileHelper::localize()]] to localize the view file. |
||
202 | * |
||
203 | * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file. |
||
204 | * Otherwise, it will simply include the view file as a normal PHP file, capture its output and |
||
205 | * return it as a string. |
||
206 | * |
||
207 | * @param string $viewFile the view file. This can be either an absolute file path or an alias of it. |
||
208 | * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
209 | * @param object $context the context that the view should use for rendering the view. If null, |
||
210 | * existing [[context]] will be used. |
||
211 | * @return string the rendering result |
||
212 | * @throws ViewNotFoundException if the view file does not exist |
||
213 | */ |
||
214 | 77 | public function renderFile($viewFile, $params = [], $context = null) |
|
258 | |||
259 | /** |
||
260 | * @return string|bool the view file currently being rendered. False if no view file is being rendered. |
||
261 | */ |
||
262 | public function getViewFile() |
||
266 | |||
267 | /** |
||
268 | * @return string|bool the requested view currently being rendered. False if no view file is being rendered. |
||
269 | * @since 2.0.16 |
||
270 | */ |
||
271 | 6 | protected function getRequestedViewFile() |
|
275 | |||
276 | /** |
||
277 | * This method is invoked right before [[renderFile()]] renders a view file. |
||
278 | * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. |
||
279 | * If you override this method, make sure you call the parent implementation first. |
||
280 | * @param string $viewFile the view file to be rendered. |
||
281 | * @param array $params the parameter array passed to the [[render()]] method. |
||
282 | * @return bool whether to continue rendering the view file. |
||
283 | */ |
||
284 | 76 | public function beforeRender($viewFile, $params) |
|
295 | |||
296 | /** |
||
297 | * This method is invoked right after [[renderFile()]] renders a view file. |
||
298 | * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. |
||
299 | * If you override this method, make sure you call the parent implementation first. |
||
300 | * @param string $viewFile the view file being rendered. |
||
301 | * @param array $params the parameter array passed to the [[render()]] method. |
||
302 | * @param string $output the rendering result of the view file. Updates to this parameter |
||
303 | * will be passed back and returned by [[renderFile()]]. |
||
304 | */ |
||
305 | 76 | public function afterRender($viewFile, $params, &$output) |
|
318 | |||
319 | /** |
||
320 | * Renders a view file as a PHP script. |
||
321 | * |
||
322 | * This method treats the view file as a PHP script and includes the file. |
||
323 | * It extracts the given parameters and makes them available in the view file. |
||
324 | * The method captures the output of the included view file and returns it as a string. |
||
325 | * |
||
326 | * This method should mainly be called by view renderer or [[renderFile()]]. |
||
327 | * |
||
328 | * @param string $_file_ the view file. |
||
329 | * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. |
||
330 | * @return string the rendering result |
||
331 | * @throws \Exception |
||
332 | * @throws \Throwable |
||
333 | */ |
||
334 | 76 | public function renderPhpFile($_file_, $_params_ = []) |
|
352 | |||
353 | /** |
||
354 | * Renders dynamic content returned by the given PHP statements. |
||
355 | * This method is mainly used together with content caching (fragment caching and page caching) |
||
356 | * when some portions of the content (called *dynamic content*) should not be cached. |
||
357 | * The dynamic content must be returned by some PHP statements. You can optionally pass |
||
358 | * additional parameters that will be available as variables in the PHP statement: |
||
359 | * |
||
360 | * ```php |
||
361 | * <?= $this->renderDynamic('return foo($myVar);', [ |
||
362 | * 'myVar' => $model->getMyComplexVar(), |
||
363 | * ]) ?> |
||
364 | * ``` |
||
365 | * @param string $statements the PHP statements for generating the dynamic content. |
||
366 | * @param array $params the parameters (name-value pairs) that will be extracted and made |
||
367 | * available in the $statement context. The parameters will be stored in the cache and be reused |
||
368 | * each time $statement is executed. You should make sure, that these are safely serializable. |
||
369 | * @throws \yii\base\ErrorException if the statement throws an exception in eval() |
||
370 | * @return string the placeholder of the dynamic content, or the dynamic content if there is no |
||
371 | * active content cache currently. |
||
372 | */ |
||
373 | 23 | public function renderDynamic($statements, array $params = []) |
|
389 | |||
390 | /** |
||
391 | * {@inheritdoc} |
||
392 | */ |
||
393 | 1 | public function getDynamicPlaceholders() |
|
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function setDynamicPlaceholders($placeholders) |
||
405 | |||
406 | /** |
||
407 | * {@inheritdoc} |
||
408 | */ |
||
409 | 17 | public function addDynamicPlaceholder($placeholder, $statements) |
|
417 | |||
418 | /** |
||
419 | * Evaluates the given PHP statements. |
||
420 | * This method is mainly used internally to implement dynamic content feature. |
||
421 | * @param string $statements the PHP statements to be evaluated. |
||
422 | * @return mixed the return value of the PHP statements. |
||
423 | */ |
||
424 | 22 | public function evaluateDynamicContent($statements) |
|
428 | |||
429 | /** |
||
430 | * Returns a list of currently active dynamic content class instances. |
||
431 | * @return DynamicContentAwareInterface[] class instances supporting dynamic contents. |
||
432 | * @since 2.0.14 |
||
433 | */ |
||
434 | 16 | public function getDynamicContents() |
|
438 | |||
439 | /** |
||
440 | * Adds a class instance supporting dynamic contents to the end of a list of currently active |
||
441 | * dynamic content class instances. |
||
442 | * @param DynamicContentAwareInterface $instance class instance supporting dynamic contents. |
||
443 | * @since 2.0.14 |
||
444 | */ |
||
445 | 22 | public function pushDynamicContent(DynamicContentAwareInterface $instance) |
|
449 | |||
450 | /** |
||
451 | * Removes a last class instance supporting dynamic contents from a list of currently active |
||
452 | * dynamic content class instances. |
||
453 | * @since 2.0.14 |
||
454 | */ |
||
455 | 22 | public function popDynamicContent() |
|
459 | |||
460 | /** |
||
461 | * Begins recording a block. |
||
462 | * |
||
463 | * This method is a shortcut to beginning [[Block]]. |
||
464 | * @param string $id the block ID. |
||
465 | * @param bool $renderInPlace whether to render the block content in place. |
||
466 | * Defaults to false, meaning the captured block will not be displayed. |
||
467 | * @return Block the Block widget instance |
||
468 | */ |
||
469 | public function beginBlock($id, $renderInPlace = false) |
||
477 | |||
478 | /** |
||
479 | * Ends recording a block. |
||
480 | */ |
||
481 | public function endBlock() |
||
485 | |||
486 | /** |
||
487 | * Begins the rendering of content that is to be decorated by the specified view. |
||
488 | * |
||
489 | * This method can be used to implement nested layout. For example, a layout can be embedded |
||
490 | * in another layout file specified as '@app/views/layouts/base.php' like the following: |
||
491 | * |
||
492 | * ```php |
||
493 | * <?php $this->beginContent('@app/views/layouts/base.php'); ?> |
||
494 | * //...layout content here... |
||
495 | * <?php $this->endContent(); ?> |
||
496 | * ``` |
||
497 | * |
||
498 | * @param string $viewFile the view file that will be used to decorate the content enclosed by this widget. |
||
499 | * This can be specified as either the view file path or [path alias](guide:concept-aliases). |
||
500 | * @param array $params the variables (name => value) to be extracted and made available in the decorative view. |
||
501 | * @return ContentDecorator the ContentDecorator widget instance |
||
502 | * @see ContentDecorator |
||
503 | */ |
||
504 | public function beginContent($viewFile, $params = []) |
||
512 | |||
513 | /** |
||
514 | * Ends the rendering of content. |
||
515 | */ |
||
516 | public function endContent() |
||
520 | |||
521 | /** |
||
522 | * Begins fragment caching. |
||
523 | * |
||
524 | * This method will display cached content if it is available. |
||
525 | * If not, it will start caching and would expect an [[endCache()]] |
||
526 | * call to end the cache and save the content into cache. |
||
527 | * A typical usage of fragment caching is as follows, |
||
528 | * |
||
529 | * ```php |
||
530 | * if ($this->beginCache($id)) { |
||
531 | * // ...generate content here |
||
532 | * $this->endCache(); |
||
533 | * } |
||
534 | * ``` |
||
535 | * |
||
536 | * @param string $id a unique ID identifying the fragment to be cached. |
||
537 | * @param array $properties initial property values for [[FragmentCache]] |
||
538 | * @return bool whether you should generate the content for caching. |
||
539 | * False if the cached version is available. |
||
540 | */ |
||
541 | 11 | public function beginCache($id, $properties = []) |
|
555 | |||
556 | /** |
||
557 | * Ends fragment caching. |
||
558 | */ |
||
559 | 11 | public function endCache() |
|
563 | |||
564 | /** |
||
565 | * Marks the beginning of a page. |
||
566 | */ |
||
567 | 50 | public function beginPage() |
|
574 | |||
575 | /** |
||
576 | * Marks the ending of a page. |
||
577 | */ |
||
578 | public function endPage() |
||
583 | } |
||
584 |
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.