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 |
||
| 56 | class View |
||
| 57 | { |
||
| 58 | /** |
||
| 59 | * The business object that will be rendered. |
||
| 60 | * |
||
| 61 | * @var \Alpha\Model\ActiveRecord |
||
| 62 | * |
||
| 63 | * @since 1.0 |
||
| 64 | */ |
||
| 65 | protected $record; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The rendering provider that will be used to render the active record. |
||
| 69 | * |
||
| 70 | * @var \Alpha\View\Renderer\RendererProviderInterface |
||
| 71 | * |
||
| 72 | * @since 1.2 |
||
| 73 | */ |
||
| 74 | private static $provider; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Trace logger. |
||
| 78 | * |
||
| 79 | * @var Logger |
||
| 80 | * |
||
| 81 | * @since 1.0 |
||
| 82 | */ |
||
| 83 | private static $logger = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Constructor for the View. As this is protected, use the View::getInstance method from a public scope. |
||
| 87 | * |
||
| 88 | * @param ActiveRecord $record The main business object that this view is going to render |
||
| 89 | * @param string $acceptHeader Optionally pass the HTTP Accept header to select the correct renderer provider. |
||
| 90 | * |
||
| 91 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 92 | * |
||
| 93 | * @since 1.0 |
||
| 94 | */ |
||
| 95 | protected function __construct($record, $acceptHeader = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Static method which returns a View object or a custom child view for the Record specified |
||
| 116 | * if one exists. |
||
| 117 | * |
||
| 118 | * @param ActiveRecord $record The main business object that this view is going to render |
||
| 119 | * @param bool $returnParent Flag to enforce the return of this object instead of a child (defaults to false) |
||
| 120 | * @param string $acceptHeader Optionally pass the HTTP Accept header to select the correct renderer provider. |
||
| 121 | * |
||
| 122 | * @return View Returns a View object, or a child view object if one exists for this record |
||
| 123 | * |
||
| 124 | * @since 1.0 |
||
| 125 | */ |
||
| 126 | public static function getInstance($record, $returnParent = false, $acceptHeader = null) |
||
| 127 | { |
||
| 128 | if (self::$logger == null) { |
||
| 129 | self::$logger = new Logger('View'); |
||
| 130 | } |
||
| 131 | self::$logger->debug('>>getInstance(Record=['.var_export($record, true).'], returnParent=['.$returnParent.'], acceptHeader=['.$acceptHeader.'])'); |
||
| 132 | |||
| 133 | $class = new ReflectionClass($record); |
||
| 134 | $childView = $class->getShortname(); |
||
| 135 | $childView = $childView.'View'; |
||
| 136 | |||
| 137 | // Check to see if a custom view exists for this record, and if it does return that view instead |
||
| 138 | if (!$returnParent) { |
||
| 139 | $className = '\Alpha\View\\'.$childView; |
||
| 140 | |||
| 141 | if (class_exists($className)) { |
||
| 142 | self::$logger->debug('<<getInstance [new '.$className.'('.get_class($record).')]'); |
||
| 143 | |||
| 144 | $instance = new $className($record, $acceptHeader); |
||
| 145 | |||
| 146 | return $instance; |
||
| 147 | } |
||
| 148 | |||
| 149 | $className = '\View\\'.$childView; |
||
| 150 | |||
| 151 | if (class_exists('\View\\'.$childView)) { |
||
| 152 | self::$logger->debug('<<getInstance [new '.$className.'('.get_class($record).')]'); |
||
| 153 | |||
| 154 | $instance = new $className($record, $acceptHeader); |
||
| 155 | |||
| 156 | return $instance; |
||
| 157 | } |
||
| 158 | |||
| 159 | self::$logger->debug('<<getInstance [new View('.get_class($record).', '.$acceptHeader.')]'); |
||
| 160 | |||
| 161 | return new self($record, $acceptHeader); |
||
| 162 | } else { |
||
| 163 | self::$logger->debug('<<getInstance [new View('.get_class($record).', '.$acceptHeader.')]'); |
||
| 164 | |||
| 165 | return new self($record, $acceptHeader); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Simple setter for the view business object. |
||
| 171 | * |
||
| 172 | * @param \Alpha\Model\ActiveRecord $record |
||
| 173 | * |
||
| 174 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 175 | * |
||
| 176 | * @since 1.0 |
||
| 177 | */ |
||
| 178 | public function setRecord($record) |
||
| 179 | { |
||
| 180 | self::$logger->debug('>>setRecord(Record=['.var_export($record, true).'])'); |
||
| 181 | |||
| 182 | if ($record instanceof \Alpha\Model\ActiveRecord) { |
||
| 183 | $this->record = $record; |
||
| 184 | } else { |
||
| 185 | throw new IllegalArguementException('The Record provided ['.get_class($record).'] is not defined anywhere!'); |
||
| 186 | } |
||
| 187 | |||
| 188 | self::$logger->debug('<<setRecord'); |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets the Record attached to this view (if any). |
||
| 193 | * |
||
| 194 | * @return \Alpha\Model\ActiveRecord |
||
| 195 | * |
||
| 196 | * @since 1.0 |
||
| 197 | */ |
||
| 198 | public function getRecord() |
||
| 199 | { |
||
| 200 | return $this->record; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Renders the default create view. |
||
| 205 | * |
||
| 206 | * @param array $fields Hash array of fields to pass to the template |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | * |
||
| 210 | * @since 1.0 |
||
| 211 | */ |
||
| 212 | public function createView($fields = array()) |
||
| 213 | { |
||
| 214 | self::$logger->debug('>>createView(fields=['.var_export($fields, true).'])'); |
||
| 215 | |||
| 216 | if (method_exists($this, 'before_createView_callback')) { |
||
| 217 | $this->{'before_createView_callback'}(); |
||
| 218 | } |
||
| 219 | |||
| 220 | $body = self::$provider->createView($fields); |
||
| 221 | |||
| 222 | if (method_exists($this, 'after_createView_callback')) { |
||
| 223 | $this->{'after_createView_callback'}(); |
||
| 224 | } |
||
| 225 | |||
| 226 | self::$logger->debug('<<createView'); |
||
| 227 | |||
| 228 | return $body; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Renders a form to enable object editing. |
||
| 233 | * |
||
| 234 | * @param array $fields Hash array of fields to pass to the template |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | * |
||
| 238 | * @since 1.0 |
||
| 239 | */ |
||
| 240 | public function editView($fields = array()) |
||
| 241 | { |
||
| 242 | self::$logger->debug('>>editView(fields=['.var_export($fields, true).'])'); |
||
| 243 | |||
| 244 | if (method_exists($this, 'before_editView_callback')) { |
||
| 245 | $this->{'before_editView_callback'}(); |
||
| 246 | } |
||
| 247 | |||
| 248 | $body = self::$provider->editView($fields); |
||
| 249 | |||
| 250 | if (method_exists($this, 'after_editView_callback')) { |
||
| 251 | $this->{'after_editView_callback'}(); |
||
| 252 | } |
||
| 253 | |||
| 254 | self::$logger->debug('<<editView'); |
||
| 255 | |||
| 256 | return $body; |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Renders the list view. |
||
| 261 | * |
||
| 262 | * @param array $fields Hash array of fields to pass to the template |
||
| 263 | * |
||
| 264 | * @return string |
||
| 265 | * |
||
| 266 | * @since 1.0 |
||
| 267 | */ |
||
| 268 | public function listView($fields = array()) |
||
| 269 | { |
||
| 270 | self::$logger->debug('>>listView(fields=['.var_export($fields, true).'])'); |
||
| 271 | |||
| 272 | if (method_exists($this, 'before_listView_callback')) { |
||
| 273 | $this->{'before_listView_callback'}(); |
||
| 274 | } |
||
| 275 | |||
| 276 | $body = self::$provider->listView($fields); |
||
| 277 | |||
| 278 | if (method_exists($this, 'after_listView_callback')) { |
||
| 279 | $this->{'after_listView_callback'}(); |
||
| 280 | } |
||
| 281 | |||
| 282 | self::$logger->debug('<<listView'); |
||
| 283 | |||
| 284 | return $body; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Renders a detailed view of the object (read-only). |
||
| 289 | * |
||
| 290 | * @param array $fields Hash array of fields to pass to the template |
||
| 291 | * |
||
| 292 | * @return string |
||
| 293 | * |
||
| 294 | * @since 1.0 |
||
| 295 | */ |
||
| 296 | public function detailedView($fields = array()) |
||
| 297 | { |
||
| 298 | self::$logger->debug('>>detailedView(fields=['.var_export($fields, true).'])'); |
||
| 299 | |||
| 300 | if (method_exists($this, 'before_detailedView_callback')) { |
||
| 301 | $this->{'before_detailedView_callback'}(); |
||
| 302 | } |
||
| 303 | |||
| 304 | $body = self::$provider->detailedView($fields); |
||
| 305 | |||
| 306 | if (method_exists($this, 'after_detailedView_callback')) { |
||
| 307 | $this->{'after_detailedView_callback'}(); |
||
| 308 | } |
||
| 309 | |||
| 310 | self::$logger->debug('<<detailedView'); |
||
| 311 | |||
| 312 | return $body; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Renders the admin view for the business object screen. |
||
| 317 | * |
||
| 318 | * @param array $fields Hash array of fields to pass to the template |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | * |
||
| 322 | * @since 1.0 |
||
| 323 | */ |
||
| 324 | public function adminView($fields = array()) |
||
| 325 | { |
||
| 326 | self::$logger->debug('>>adminView(fields=['.var_export($fields, true).'])'); |
||
| 327 | |||
| 328 | if (method_exists($this, 'before_adminView_callback')) { |
||
| 329 | $this->{'before_adminView_callback'}(); |
||
| 330 | } |
||
| 331 | |||
| 332 | $body = self::$provider->adminView($fields); |
||
| 333 | |||
| 334 | if (method_exists($this, 'after_adminView_callback')) { |
||
| 335 | $this->{'after_adminView_callback'}(); |
||
| 336 | } |
||
| 337 | |||
| 338 | self::$logger->debug('<<adminView'); |
||
| 339 | |||
| 340 | return $body; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Method to render the page header content. |
||
| 345 | * |
||
| 346 | * @param \Alpha\Controller\Controller $controller |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | * |
||
| 350 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 351 | * |
||
| 352 | * @since 1.0 |
||
| 353 | */ |
||
| 354 | public static function displayPageHead($controller) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Method to render the page footer content. |
||
| 385 | * |
||
| 386 | * @param \Alpha\Controller\Controller $controller |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | * |
||
| 390 | * @since 1.0 |
||
| 391 | */ |
||
| 392 | public static function displayPageFoot($controller) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Method for rendering the pagination links. |
||
| 426 | * |
||
| 427 | * @param \Alpha\Controller\Controller $controller |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | * |
||
| 431 | * @since 3.0 |
||
| 432 | */ |
||
| 433 | public static function displayPageLinks($controller) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Renders the content for an update (e.g. successful save) message. |
||
| 513 | * |
||
| 514 | * @param string $message |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | * |
||
| 518 | * @since 1.0 |
||
| 519 | */ |
||
| 520 | public static function displayUpdateMessage($message) |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Renders the content for an error (e.g. save failed) message. |
||
| 543 | * |
||
| 544 | * @param string $message |
||
| 545 | * |
||
| 546 | * @return string |
||
| 547 | * |
||
| 548 | * @since 1.0 |
||
| 549 | */ |
||
| 550 | public static function displayErrorMessage($message) |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Renders an error page with the supplied error code (typlically a HTTP code) and a message. |
||
| 573 | * |
||
| 574 | * @param string $code |
||
| 575 | * @param string $message |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | * |
||
| 579 | * @since 1.0 |
||
| 580 | */ |
||
| 581 | public static function renderErrorPage($code, $message) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Method to render a hidden HTML form for posting the ID of an object to be deleted. |
||
| 604 | * |
||
| 605 | * @param string $URI The URI that the form will point to |
||
| 606 | * |
||
| 607 | * @return string |
||
| 608 | * |
||
| 609 | * @since 1.0 |
||
| 610 | */ |
||
| 611 | public static function renderDeleteForm($URI) |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Method to render a HTML form with two hidden, hashed (MD5) form fields to be used as |
||
| 634 | * a check to ensure that a post to the controller is being sent from the same server |
||
| 635 | * as hosting it. |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | * |
||
| 639 | * @since 1.0 |
||
| 640 | */ |
||
| 641 | public static function renderSecurityFields() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Method to render the default Integer HTML. |
||
| 664 | * |
||
| 665 | * @param string $name The field name |
||
| 666 | * @param string $label The label to apply to the field |
||
| 667 | * @param string $mode The field mode (create/edit/view) |
||
| 668 | * @param string $value The field value (optional) |
||
| 669 | * |
||
| 670 | * @return string |
||
| 671 | * |
||
| 672 | * @since 1.0 |
||
| 673 | */ |
||
| 674 | public function renderIntegerField($name, $label, $mode, $value = '') |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Method to render the default Double HTML. |
||
| 687 | * |
||
| 688 | * @param string $name The field name |
||
| 689 | * @param string $label The label to apply to the field |
||
| 690 | * @param string $mode The field mode (create/edit/view) |
||
| 691 | * @param string $value The field value (optional) |
||
| 692 | * |
||
| 693 | * @return string |
||
| 694 | * |
||
| 695 | * @since 1.0 |
||
| 696 | */ |
||
| 697 | public function renderDoubleField($name, $label, $mode, $value = '') |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Method to render the default Boolean HTML. |
||
| 710 | * |
||
| 711 | * @param string $name The field name |
||
| 712 | * @param string $label The label to apply to the field |
||
| 713 | * @param string $mode The field mode (create/edit/view) |
||
| 714 | * @param string $value The field value (optional) |
||
| 715 | * |
||
| 716 | * @return string |
||
| 717 | * |
||
| 718 | * @since 1.0 |
||
| 719 | */ |
||
| 720 | public function renderBooleanField($name, $label, $mode, $value = '') |
||
| 730 | |||
| 731 | /** |
||
| 732 | * Method to render the default Enum HTML. |
||
| 733 | * |
||
| 734 | * @param string $name The field name |
||
| 735 | * @param string $label The label to apply to the field |
||
| 736 | * @param string $mode The field mode (create/edit/view) |
||
| 737 | * @param array $options The Enum options |
||
| 738 | * @param string $value The field value (optional) |
||
| 739 | * |
||
| 740 | * @return string |
||
| 741 | * |
||
| 742 | * @since 1.0 |
||
| 743 | */ |
||
| 744 | public function renderEnumField($name, $label, $mode, $options, $value = '') |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Method to render the default DEnum HTML. |
||
| 757 | * |
||
| 758 | * @param string $name The field name |
||
| 759 | * @param string $label The label to apply to the field |
||
| 760 | * @param string $mode The field mode (create/edit/view) |
||
| 761 | * @param array $options The DEnum options |
||
| 762 | * @param string $value The field value (optional) |
||
| 763 | * |
||
| 764 | * @return string |
||
| 765 | * |
||
| 766 | * @since 1.0 |
||
| 767 | */ |
||
| 768 | public function renderDEnumField($name, $label, $mode, $options, $value = '') |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Method to render the default field HTML when type is not known. |
||
| 781 | * |
||
| 782 | * @param string $name The field name |
||
| 783 | * @param string $label The label to apply to the field |
||
| 784 | * @param string $mode The field mode (create/edit/view) |
||
| 785 | * @param string $value The field value (optional) |
||
| 786 | * |
||
| 787 | * @return string |
||
| 788 | * |
||
| 789 | * @since 1.0 |
||
| 790 | */ |
||
| 791 | public function renderDefaultField($name, $label, $mode, $value = '') |
||
| 801 | |||
| 802 | /** |
||
| 803 | * render the default Text HTML. |
||
| 804 | * |
||
| 805 | * @param string $name The field name |
||
| 806 | * @param string $label The label to apply to the field |
||
| 807 | * @param string $mode The field mode (create/edit/view) |
||
| 808 | * @param string $value The field value (optional) |
||
| 809 | * |
||
| 810 | * @return string |
||
| 811 | * |
||
| 812 | * @since 1.0 |
||
| 813 | */ |
||
| 814 | public function renderTextField($name, $label, $mode, $value = '') |
||
| 824 | |||
| 825 | /** |
||
| 826 | * render the default Relation HTML. |
||
| 827 | * |
||
| 828 | * @param string $name The field name |
||
| 829 | * @param string $label The label to apply to the field |
||
| 830 | * @param string $mode The field mode (create/edit/view) |
||
| 831 | * @param string $value The field value (optional) |
||
| 832 | * @param bool $expanded Render the related fields in expanded format or not (optional) |
||
| 833 | * @param bool $buttons Render buttons for expanding/contacting the related fields (optional) |
||
| 834 | * |
||
| 835 | * @return string |
||
| 836 | * |
||
| 837 | * @since 1.0 |
||
| 838 | */ |
||
| 839 | public function renderRelationField($name, $label, $mode, $value = '', $expanded = false, $buttons = true) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Renders all fields for the current Record in edit/create/view mode. |
||
| 852 | * |
||
| 853 | * @param string $mode (view|edit|create) |
||
| 854 | * @param array $filterFields Optional list of field names to exclude from rendering |
||
| 855 | * @param array $readOnlyFields Optional list of fields to render in a readonly fashion when rendering in create or edit mode |
||
| 856 | * |
||
| 857 | * @return string |
||
| 858 | * |
||
| 859 | * @since 1.0 |
||
| 860 | */ |
||
| 861 | public function renderAllFields($mode, $filterFields = array(), $readOnlyFields = array()) |
||
| 871 | |||
| 872 | /** |
||
| 873 | * Loads a template for the Record specified if one exists. Lower level custom templates |
||
| 874 | * take precedence. |
||
| 875 | * |
||
| 876 | * @param \Alpha\Model\ActiveRecord $record |
||
| 877 | * @param string $mode |
||
| 878 | * @param array $fields |
||
| 879 | * |
||
| 880 | * @return string |
||
| 881 | * |
||
| 882 | * @since 1.0 |
||
| 883 | * |
||
| 884 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 885 | */ |
||
| 886 | public static function loadTemplate($record, $mode, $fields = array()) |
||
| 970 | |||
| 971 | /** |
||
| 972 | * Loads a template fragment from the Renderer/[type]/Fragments/[filename.ext] location. |
||
| 973 | * |
||
| 974 | * @param string $type Currently only html supported, later json and xml. |
||
| 975 | * @param string $fileName The name of the fragment file |
||
| 976 | * @param array $fields A hash array of field values to pass to the template fragment. |
||
| 977 | * |
||
| 978 | * @return string |
||
| 979 | * |
||
| 980 | * @since 1.2 |
||
| 981 | * |
||
| 982 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 983 | */ |
||
| 984 | public static function loadTemplateFragment($type, $fileName, $fields = array()) |
||
| 1032 | |||
| 1033 | /** |
||
| 1034 | * Enables you to set an explicit type of RendererProviderInterface implementation to use for rendering the records |
||
| 1035 | * attached to this view. |
||
| 1036 | * |
||
| 1037 | * @param string $ProviderClassName The name of the RendererProviderInterface implementation to use in this view object |
||
| 1038 | * @param string $acceptHeader Optional pass the HTTP Accept header to select the correct renderer provider. |
||
| 1039 | * |
||
| 1040 | * @since 1.2 |
||
| 1041 | * |
||
| 1042 | * @throws \Alpha\Exception\IllegalArguementException |
||
| 1043 | */ |
||
| 1044 | public static function setProvider($ProviderClassName, $acceptHeader = null) |
||
| 1068 | |||
| 1069 | /** |
||
| 1070 | * Get the current view renderer provider. |
||
| 1071 | * |
||
| 1072 | * @return \Alpha\View\Renderer\RendererProviderInterface |
||
| 1073 | * |
||
| 1074 | * @since 2.0 |
||
| 1075 | */ |
||
| 1076 | public static function getProvider() |
||
| 1088 | } |
||
| 1089 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: