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  | 
            ||
| 33 | class View implements \ArrayAccess, \JsonSerializable  | 
            ||
| 34 | { | 
            ||
| 35 | use PrototypeTrait;  | 
            ||
| 36 | use ViewBindings;  | 
            ||
| 37 | |||
| 38 | const TEMPLATE_TYPE_VIEW = 1;  | 
            ||
| 39 | const TEMPLATE_TYPE_LAYOUT = 2;  | 
            ||
| 40 | const TEMPLATE_TYPE_PARTIAL = 3;  | 
            ||
| 41 | |||
| 42 | const TEMPLATE_PREFIX_VIEW = '';  | 
            ||
| 43 | const TEMPLATE_PREFIX_LAYOUT = '@';  | 
            ||
| 44 | const TEMPLATE_PREFIX_PARTIAL = '_';  | 
            ||
| 45 | |||
| 46 | static private $template_type_name = [  | 
            ||
| 47 | |||
| 48 | self::TEMPLATE_TYPE_VIEW => 'template',  | 
            ||
| 49 | self::TEMPLATE_TYPE_LAYOUT => 'layout',  | 
            ||
| 50 | self::TEMPLATE_TYPE_PARTIAL=> 'partial'  | 
            ||
| 51 | |||
| 52 | ];  | 
            ||
| 53 | |||
| 54 | static private $template_prefix = [  | 
            ||
| 55 | |||
| 56 | self::TEMPLATE_TYPE_VIEW => self::TEMPLATE_PREFIX_VIEW,  | 
            ||
| 57 | self::TEMPLATE_TYPE_LAYOUT => self::TEMPLATE_PREFIX_LAYOUT,  | 
            ||
| 58 | self::TEMPLATE_TYPE_PARTIAL=> self::TEMPLATE_PREFIX_PARTIAL  | 
            ||
| 59 | |||
| 60 | ];  | 
            ||
| 61 | |||
| 62 | /**  | 
            ||
| 63 | * @var Controller  | 
            ||
| 64 | */  | 
            ||
| 65 | private $controller;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @see $controller  | 
            ||
| 69 | *  | 
            ||
| 70 | * @return Controller  | 
            ||
| 71 | */  | 
            ||
| 72 | protected function get_controller()  | 
            ||
| 76 | |||
| 77 | /**  | 
            ||
| 78 | * View's variables.  | 
            ||
| 79 | *  | 
            ||
| 80 | * @var array  | 
            ||
| 81 | */  | 
            ||
| 82 | private $variables = [];  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * @see $variables  | 
            ||
| 86 | *  | 
            ||
| 87 | * @return array  | 
            ||
| 88 | */  | 
            ||
| 89 | protected function get_variables()  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * @see $content  | 
            ||
| 96 | *  | 
            ||
| 97 | * @return mixed  | 
            ||
| 98 | */  | 
            ||
| 99 | protected function get_content()  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * @see $content  | 
            ||
| 106 | *  | 
            ||
| 107 | * @param mixed $content  | 
            ||
| 108 | */  | 
            ||
| 109 | protected function set_content($content)  | 
            ||
| 113 | |||
| 114 | private $decorators = [];  | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * Return the name of the template.  | 
            ||
| 118 | *  | 
            ||
| 119 | * The template name is resolved as follows:  | 
            ||
| 120 | *  | 
            ||
| 121 | * - The `template` property of the route.  | 
            ||
| 122 | * - The `template` property of the controller.  | 
            ||
| 123 | 	 * - The `{$controller->name}/{$controller->action}`, if the controller has an `action` | 
            ||
| 124 | * property.  | 
            ||
| 125 | *  | 
            ||
| 126 | * @return string|null  | 
            ||
| 127 | */  | 
            ||
| 128 | protected function lazy_get_template()  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | 	 * Returns an array of callable used to resolve the {@link $template} property. | 
            ||
| 152 | *  | 
            ||
| 153 | * @return callable[]  | 
            ||
| 154 | *  | 
            ||
| 155 | * @internal  | 
            ||
| 156 | */  | 
            ||
| 157 | protected function get_template_resolvers()  | 
            ||
| 181 | |||
| 182 | /**  | 
            ||
| 183 | * Returns the name of the layout.  | 
            ||
| 184 | *  | 
            ||
| 185 | * The layout name is resolved as follows:  | 
            ||
| 186 | *  | 
            ||
| 187 | * - The `layout` property of the route.  | 
            ||
| 188 | * - The `layout` property of the controller.  | 
            ||
| 189 | * - If the identifier of the route starts with "admin:", "admin" is returned.  | 
            ||
| 190 | * - If the route pattern is "/" and a "home" layout template is available, "home" is returned.  | 
            ||
| 191 | * - If the "@page" template is available, "page" is returned.  | 
            ||
| 192 | * - "default" is returned.  | 
            ||
| 193 | *  | 
            ||
| 194 | * @return string  | 
            ||
| 195 | */  | 
            ||
| 196 | protected function lazy_get_layout()  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | 	 * Returns an array of callable used to resolve the {@link $template} property. | 
            ||
| 235 | *  | 
            ||
| 236 | * @return callable[]  | 
            ||
| 237 | *  | 
            ||
| 238 | * @internal  | 
            ||
| 239 | */  | 
            ||
| 240 | protected function get_layout_resolvers()  | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * An event hook is attached to the `action` event of the controller for late rendering,  | 
            ||
| 261 | * which only happens if the response is `null`.  | 
            ||
| 262 | *  | 
            ||
| 263 | * @param Controller $controller The controller that invoked the view.  | 
            ||
| 264 | */  | 
            ||
| 265 | public function __construct(Controller $controller)  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * @inheritdoc  | 
            ||
| 279 | *  | 
            ||
| 280 | * Returns an array with the following keys: `template`, `layout`, and `variables`.  | 
            ||
| 281 | */  | 
            ||
| 282 | public function jsonSerialize()  | 
            ||
| 292 | |||
| 293 | /**  | 
            ||
| 294 | * @inheritdoc  | 
            ||
| 295 | */  | 
            ||
| 296 | public function offsetExists($offset)  | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * @inheritdoc  | 
            ||
| 303 | *  | 
            ||
| 304 | * @throws OffsetNotDefined if the offset is not defined.  | 
            ||
| 305 | */  | 
            ||
| 306 | public function offsetGet($offset)  | 
            ||
| 315 | |||
| 316 | /**  | 
            ||
| 317 | * @inheritdoc  | 
            ||
| 318 | */  | 
            ||
| 319 | public function offsetSet($offset, $value)  | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * @inheritdoc  | 
            ||
| 326 | */  | 
            ||
| 327 | public function offsetUnset($offset)  | 
            ||
| 331 | |||
| 332 | /**  | 
            ||
| 333 | * Assign multiple variables.  | 
            ||
| 334 | *  | 
            ||
| 335 | * @param array $variables  | 
            ||
| 336 | *  | 
            ||
| 337 | * @return $this  | 
            ||
| 338 | */  | 
            ||
| 339 | public function assign(array $variables)  | 
            ||
| 345 | |||
| 346 | /**  | 
            ||
| 347 | * Resolve a template pathname from its name and type.  | 
            ||
| 348 | *  | 
            ||
| 349 | * @param string $name Name of the template.  | 
            ||
| 350 | * @param string $prefix Template prefix.  | 
            ||
| 351 | * @param array $tried Reference to an array where tried paths are collected.  | 
            ||
| 352 | *  | 
            ||
| 353 | * @return string|false  | 
            ||
| 354 | */  | 
            ||
| 355 | protected function resolve_template($name, $prefix, &$tried = [])  | 
            ||
| 369 | |||
| 370 | /**  | 
            ||
| 371 | * Add a template to decorate the content with.  | 
            ||
| 372 | *  | 
            ||
| 373 | * @param string $template Name of the template.  | 
            ||
| 374 | */  | 
            ||
| 375 | public function decorate_with($template)  | 
            ||
| 379 | |||
| 380 | /**  | 
            ||
| 381 | * Decorate the content.  | 
            ||
| 382 | *  | 
            ||
| 383 | * @param mixed $content The content to decorate.  | 
            ||
| 384 | *  | 
            ||
| 385 | * @return mixed  | 
            ||
| 386 | */  | 
            ||
| 387 | protected function decorate($content)  | 
            ||
| 398 | |||
| 399 | /**  | 
            ||
| 400 | * Render the view.  | 
            ||
| 401 | *  | 
            ||
| 402 | * @return string  | 
            ||
| 403 | */  | 
            ||
| 404 | public function render()  | 
            ||
| 427 | |||
| 428 | /**  | 
            ||
| 429 | * Renders the content using a template.  | 
            ||
| 430 | *  | 
            ||
| 431 | * @param mixed $content The content to render.  | 
            ||
| 432 | * @param string $template Name of the template.  | 
            ||
| 433 | * @param string $type Type of the template.  | 
            ||
| 434 | *  | 
            ||
| 435 | * @return string  | 
            ||
| 436 | */  | 
            ||
| 437 | protected function render_with_template($content, $template, $type)  | 
            ||
| 452 | |||
| 453 | /**  | 
            ||
| 454 | * Prepares engine arguments.  | 
            ||
| 455 | *  | 
            ||
| 456 | * @param mixed $content  | 
            ||
| 457 | * @param string $type  | 
            ||
| 458 | *  | 
            ||
| 459 | * @return array  | 
            ||
| 460 | */  | 
            ||
| 461 | protected function prepare_engine_args($content, $type)  | 
            ||
| 476 | |||
| 477 | /**  | 
            ||
| 478 | * Renders the view on `Controller::action` event.  | 
            ||
| 479 | *  | 
            ||
| 480 | * **Note:** The view is not rendered if the event's response is defined, which is the case  | 
            ||
| 481 | * when the controller obtained a result after its execution.  | 
            ||
| 482 | *  | 
            ||
| 483 | * @param Controller\ActionEvent $event  | 
            ||
| 484 | */  | 
            ||
| 485 | protected function on_action(Controller\ActionEvent $event)  | 
            ||
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * Ensures the array does not include our instance.  | 
            ||
| 504 | *  | 
            ||
| 505 | * @param array $array  | 
            ||
| 506 | *  | 
            ||
| 507 | * @return array  | 
            ||
| 508 | */  | 
            ||
| 509 | private function ensure_without_this(array $array)  | 
            ||
| 521 | }  | 
            ||
| 522 |