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 |
||
| 35 | class View implements \ArrayAccess, \JsonSerializable |
||
| 36 | { |
||
| 37 | use AccessorTrait; |
||
| 38 | |||
| 39 | const TEMPLATE_TYPE_VIEW = 1; |
||
| 40 | const TEMPLATE_TYPE_LAYOUT = 2; |
||
| 41 | const TEMPLATE_TYPE_PARTIAL = 3; |
||
| 42 | |||
| 43 | const TEMPLATE_PREFIX_VIEW = ''; |
||
| 44 | const TEMPLATE_PREFIX_LAYOUT = '@'; |
||
| 45 | const TEMPLATE_PREFIX_PARTIAL = '_'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Controller |
||
| 49 | */ |
||
| 50 | private $controller; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return Controller |
||
| 54 | */ |
||
| 55 | protected function get_controller() |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var Renderer |
||
| 62 | */ |
||
| 63 | private $renderer; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return Renderer |
||
| 67 | */ |
||
| 68 | protected function get_renderer() |
||
| 72 | |||
| 73 | /** |
||
| 74 | * View's variables. |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | private $variables = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @see $variables |
||
| 82 | * |
||
| 83 | * @return array |
||
| 84 | */ |
||
| 85 | protected function get_variables() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @see $content |
||
| 92 | * |
||
| 93 | * @return mixed |
||
| 94 | */ |
||
| 95 | protected function get_content() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @see $content |
||
| 102 | * |
||
| 103 | * @param mixed $content |
||
| 104 | */ |
||
| 105 | protected function set_content($content) |
||
| 109 | |||
| 110 | private $decorators = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Return the name of the template. |
||
| 114 | * |
||
| 115 | * The template name is resolved as follows: |
||
| 116 | * |
||
| 117 | * - The `template` property of the route. |
||
| 118 | * - The `template` property of the controller. |
||
| 119 | * - The `{$controller->name}/{$controller->action}`, if the controller has an `action` |
||
| 120 | * property. |
||
| 121 | * |
||
| 122 | * @return string|null |
||
| 123 | */ |
||
| 124 | protected function lazy_get_template() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Returns an array of callable used to resolve the {@link $template} property. |
||
| 148 | * |
||
| 149 | * @return callable[] |
||
| 150 | * |
||
| 151 | * @internal |
||
| 152 | */ |
||
| 153 | protected function get_template_resolvers() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Returns the name of the layout. |
||
| 180 | * |
||
| 181 | * The layout name is resolved as follows: |
||
| 182 | * |
||
| 183 | * - The `layout` property of the route. |
||
| 184 | * - The `layout` property of the controller. |
||
| 185 | * - If the identifier of the route starts with "admin:", "admin" is returned. |
||
| 186 | * - If the route pattern is "/" and a "home" layout template is available, "home" is returned. |
||
| 187 | * - If the "@page" template is available, "page" is returned. |
||
| 188 | * - "default" is returned. |
||
| 189 | * |
||
| 190 | * @return string |
||
| 191 | */ |
||
| 192 | protected function lazy_get_layout() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns an array of callable used to resolve the {@link $template} property. |
||
| 231 | * |
||
| 232 | * @return callable[] |
||
| 233 | * |
||
| 234 | * @internal |
||
| 235 | */ |
||
| 236 | protected function get_layout_resolvers() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * An event hook is attached to the `action` event of the controller for late rendering, |
||
| 257 | * which only happens if the response is `null`. |
||
| 258 | * |
||
| 259 | * @param Controller $controller The controller that invoked the view. |
||
| 260 | * @param Renderer $renderer |
||
| 261 | */ |
||
| 262 | public function __construct(Controller $controller, Renderer $renderer) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @inheritdoc |
||
| 277 | * |
||
| 278 | * Returns an array with the following keys: `template`, `layout`, and `variables`. |
||
| 279 | */ |
||
| 280 | public function jsonSerialize() |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @inheritdoc |
||
| 293 | */ |
||
| 294 | public function offsetExists($offset) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @inheritdoc |
||
| 301 | * |
||
| 302 | * @throws OffsetNotDefined if the offset is not defined. |
||
| 303 | */ |
||
| 304 | public function offsetGet($offset) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @inheritdoc |
||
| 316 | */ |
||
| 317 | public function offsetSet($offset, $value) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @inheritdoc |
||
| 324 | */ |
||
| 325 | public function offsetUnset($offset) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Assign multiple variables. |
||
| 332 | * |
||
| 333 | * @param array $variables |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function assign(array $variables) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Resolve a template pathname from its name and type. |
||
| 346 | * |
||
| 347 | * @param string $name Name of the template. |
||
| 348 | * @param string $prefix Template prefix. |
||
| 349 | * @param array $tried Reference to an array where tried paths are collected. |
||
| 350 | * |
||
| 351 | * @return string|false |
||
| 352 | */ |
||
| 353 | protected function resolve_template($name, $prefix, &$tried = []) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Add a template to decorate the content with. |
||
| 376 | * |
||
| 377 | * @param string $template Name of the template. |
||
| 378 | */ |
||
| 379 | public function decorate_with($template) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Decorate the content. |
||
| 386 | * |
||
| 387 | * @param mixed $content The content to decorate. |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | protected function decorate($content) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Render the view. |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function render() |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Renders the view on `Controller::action` event. |
||
| 427 | * |
||
| 428 | * **Note:** The view is not rendered if the event's response is defined, which is the case |
||
| 429 | * when the controller obtained a result after its execution. |
||
| 430 | * |
||
| 431 | * @param Controller\ActionEvent $event |
||
| 432 | */ |
||
| 433 | protected function on_action(Controller\ActionEvent $event) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Ensures the array does not include our instance. |
||
| 452 | * |
||
| 453 | * @param array $array |
||
| 454 | * |
||
| 455 | * @return array |
||
| 456 | */ |
||
| 457 | private function ensure_without_this(array $array) |
||
| 469 | } |
||
| 470 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.