Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Helper 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 Helper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class Helper extends Object { |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Settings for this helper. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | public $settings = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List of helpers used by this helper |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public $helpers = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * A helper lookup table used to lazy load helper objects. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $_helperMap = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The current theme name if any. |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | public $theme = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Request object |
||
| 59 | * |
||
| 60 | * @var CakeRequest |
||
| 61 | */ |
||
| 62 | public $request = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Plugin path |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | public $plugin = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Holds the fields array('field_name' => array('type' => 'string', 'length' => 100), |
||
| 73 | * primaryKey and validates array('field_name') |
||
| 74 | * |
||
| 75 | * @var array |
||
| 76 | */ |
||
| 77 | public $fieldset = array(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Holds tag templates. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $tags = array(); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Holds the content to be cleaned. |
||
| 88 | * |
||
| 89 | * @var mixed |
||
| 90 | */ |
||
| 91 | protected $_tainted = null; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Holds the cleaned content. |
||
| 95 | * |
||
| 96 | * @var mixed |
||
| 97 | */ |
||
| 98 | protected $_cleaned = null; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The View instance this helper is attached to |
||
| 102 | * |
||
| 103 | * @var View |
||
| 104 | */ |
||
| 105 | protected $_View; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * A list of strings that should be treated as suffixes, or |
||
| 109 | * sub inputs for a parent input. This is used for date/time |
||
| 110 | * inputs primarily. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $_fieldSuffixes = array( |
||
| 115 | 'year', 'month', 'day', 'hour', 'min', 'second', 'meridian' |
||
| 116 | ); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The name of the current model entities are in scope of. |
||
| 120 | * |
||
| 121 | * @see Helper::setEntity() |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | protected $_modelScope; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The name of the current model association entities are in scope of. |
||
| 128 | * |
||
| 129 | * @see Helper::setEntity() |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | protected $_association; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The dot separated list of elements the current field entity is for. |
||
| 136 | * |
||
| 137 | * @see Helper::setEntity() |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $_entityPath; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Minimized attributes |
||
| 144 | * |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | protected $_minimizedAttributes = array( |
||
| 148 | 'compact', 'checked', 'declare', 'readonly', 'disabled', 'selected', |
||
| 149 | 'defer', 'ismap', 'nohref', 'noshade', 'nowrap', 'multiple', 'noresize', |
||
| 150 | 'autoplay', 'controls', 'loop', 'muted', 'required', 'novalidate', 'formnovalidate' |
||
| 151 | ); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Format to attribute |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | protected $_attributeFormat = '%s="%s"'; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Format to attribute |
||
| 162 | * |
||
| 163 | * @var string |
||
| 164 | */ |
||
| 165 | protected $_minimizedAttributeFormat = '%s="%s"'; |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Default Constructor |
||
| 169 | * |
||
| 170 | * @param View $View The View this helper is being attached to. |
||
| 171 | * @param array $settings Configuration settings for the helper. |
||
| 172 | */ |
||
| 173 | public function __construct(View $View, $settings = array()) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Provide non fatal errors on missing method calls. |
||
| 186 | * |
||
| 187 | * @param string $method Method to invoke |
||
| 188 | * @param array $params Array of params for the method. |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function __call($method, $params) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Lazy loads helpers. Provides access to deprecated request properties as well. |
||
| 197 | * |
||
| 198 | * @param string $name Name of the property being accessed. |
||
| 199 | * @return mixed Helper or property found at $name |
||
| 200 | * @deprecated Accessing request properties through this method is deprecated and will be removed in 3.0. |
||
| 201 | */ |
||
| 202 | public function __get($name) { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Provides backwards compatibility access for setting values to the request object. |
||
| 225 | * |
||
| 226 | * @param string $name Name of the property being accessed. |
||
| 227 | * @param mixed $value |
||
| 228 | * @return void |
||
| 229 | * @deprecated This method will be removed in 3.0 |
||
| 230 | */ |
||
| 231 | public function __set($name, $value) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Finds URL for specified action. |
||
| 248 | * |
||
| 249 | * Returns a URL pointing at the provided parameters. |
||
| 250 | * |
||
| 251 | * @param string|array $url Either a relative string url like `/products/view/23` or |
||
| 252 | * an array of URL parameters. Using an array for URLs will allow you to leverage |
||
| 253 | * the reverse routing features of CakePHP. |
||
| 254 | * @param boolean $full If true, the full base URL will be prepended to the result |
||
| 255 | * @return string Full translated URL with base path. |
||
| 256 | * @link http://book.cakephp.org/2.0/en/views/helpers.html |
||
| 257 | */ |
||
| 258 | public function url($url = null, $full = false) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Checks if a file exists when theme is used, if no file is found default location is returned |
||
| 264 | * |
||
| 265 | * @param string $file The file to create a webroot path to. |
||
| 266 | * @return string Web accessible path to file. |
||
| 267 | */ |
||
| 268 | public function webroot($file) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Generate URL for given asset file. Depending on options passed provides full URL with domain name. |
||
| 300 | * Also calls Helper::assetTimestamp() to add timestamp to local files |
||
| 301 | * |
||
| 302 | * @param string|array Path string or URL array |
||
| 303 | * @param array $options Options array. Possible keys: |
||
| 304 | * `fullBase` Return full URL with domain name |
||
| 305 | * `pathPrefix` Path prefix for relative URLs |
||
| 306 | * `ext` Asset extension to append |
||
| 307 | * `plugin` False value will prevent parsing path as a plugin |
||
| 308 | * @return string Generated URL |
||
| 309 | */ |
||
| 310 | public function assetUrl($path, $options = array()) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Encodes a URL for use in HTML attributes. |
||
| 346 | * |
||
| 347 | * @param string $url The URL to encode. |
||
| 348 | * @return string The URL encoded for both URL & HTML contexts. |
||
| 349 | */ |
||
| 350 | protected function _encodeUrl($url) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Adds a timestamp to a file based resource based on the value of `Asset.timestamp` in |
||
| 360 | * Configure. If Asset.timestamp is true and debug > 0, or Asset.timestamp === 'force' |
||
| 361 | * a timestamp will be added. |
||
| 362 | * |
||
| 363 | * @param string $path The file path to timestamp, the path must be inside WWW_ROOT |
||
| 364 | * @return string Path with a timestamp added, or not. |
||
| 365 | */ |
||
| 366 | public function assetTimestamp($path) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Used to remove harmful tags from content. Removes a number of well known XSS attacks |
||
| 405 | * from content. However, is not guaranteed to remove all possibilities. Escaping |
||
| 406 | * content is the best way to prevent all possible attacks. |
||
| 407 | * |
||
| 408 | * @param string|array $output Either an array of strings to clean or a single string to clean. |
||
| 409 | * @return string|array cleaned content for output |
||
| 410 | * @deprecated This method will be removed in 3.0 |
||
| 411 | */ |
||
| 412 | public function clean($output) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns a space-delimited string with items of the $options array. If a key |
||
| 430 | * of $options array happens to be one of those listed in `Helper::$_minimizedAttributes` |
||
| 431 | * |
||
| 432 | * And its value is one of: |
||
| 433 | * |
||
| 434 | * - '1' (string) |
||
| 435 | * - 1 (integer) |
||
| 436 | * - true (boolean) |
||
| 437 | * - 'true' (string) |
||
| 438 | * |
||
| 439 | * Then the value will be reset to be identical with key's name. |
||
| 440 | * If the value is not one of these 3, the parameter is not output. |
||
| 441 | * |
||
| 442 | * 'escape' is a special option in that it controls the conversion of |
||
| 443 | * attributes to their html-entity encoded equivalents. Set to false to disable html-encoding. |
||
| 444 | * |
||
| 445 | * If value for any option key is set to `null` or `false`, that option will be excluded from output. |
||
| 446 | * |
||
| 447 | * @param array $options Array of options. |
||
| 448 | * @param array $exclude Array of options to be excluded, the options here will not be part of the return. |
||
| 449 | * @param string $insertBefore String to be inserted before options. |
||
| 450 | * @param string $insertAfter String to be inserted after options. |
||
| 451 | * @return string Composed attributes. |
||
| 452 | * @deprecated This method will be moved to HtmlHelper in 3.0 |
||
| 453 | */ |
||
| 454 | protected function _parseAttributes($options, $exclude = null, $insertBefore = ' ', $insertAfter = null) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Formats an individual attribute, and returns the string value of the composed attribute. |
||
| 480 | * Works with minimized attributes that have the same value as their name such as 'disabled' and 'checked' |
||
| 481 | * |
||
| 482 | * @param string $key The name of the attribute to create |
||
| 483 | * @param string $value The value of the attribute to create. |
||
| 484 | * @param boolean $escape Define if the value must be escaped |
||
| 485 | * @return string The composed attribute. |
||
| 486 | * @deprecated This method will be moved to HtmlHelper in 3.0 |
||
| 487 | */ |
||
| 488 | protected function _formatAttribute($key, $value, $escape = true) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Returns a string to be used as onclick handler for confirm dialogs. |
||
| 508 | * |
||
| 509 | * @param string $message Message to be displayed |
||
| 510 | * @param string $okCode Code to be executed after user chose 'OK' |
||
| 511 | * @param string $cancelCode Code to be executed after user chose 'Cancel' |
||
| 512 | * @param array $options Array of options |
||
| 513 | * @return string onclick JS code |
||
| 514 | */ |
||
| 515 | protected function _confirm($message, $okCode, $cancelCode = '', $options = array()) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Sets this helper's model and field properties to the dot-separated value-pair in $entity. |
||
| 526 | * |
||
| 527 | * @param string $entity A field name, like "ModelName.fieldName" or "ModelName.ID.fieldName" |
||
| 528 | * @param boolean $setScope Sets the view scope to the model specified in $tagValue |
||
| 529 | * @return void |
||
| 530 | */ |
||
| 531 | public function setEntity($entity, $setScope = false) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Returns the entity reference of the current context as an array of identity parts |
||
| 595 | * |
||
| 596 | * @return array An array containing the identity elements of an entity |
||
| 597 | */ |
||
| 598 | public function entity() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Gets the currently-used model of the rendering context. |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function model() { |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Gets the currently-used model field of the rendering context. |
||
| 616 | * Strips off field suffixes such as year, month, day, hour, min, meridian |
||
| 617 | * when the current entity is longer than 2 elements. |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | public function field() { |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Generates a DOM ID for the selected element, if one is not set. |
||
| 633 | * Uses the current View::entity() settings to generate a CamelCased id attribute. |
||
| 634 | * |
||
| 635 | * @param array|string $options Either an array of html attributes to add $id into, or a string |
||
| 636 | * with a view entity path to get a domId for. |
||
| 637 | * @param string $id The name of the 'id' attribute. |
||
| 638 | * @return mixed If $options was an array, an array will be returned with $id set. If a string |
||
| 639 | * was supplied, a string will be returned. |
||
| 640 | */ |
||
| 641 | public function domId($options = null, $id = 'id') { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Gets the input field name for the current tag. Creates input name attributes |
||
| 664 | * using CakePHP's data[Model][field] formatting. |
||
| 665 | * |
||
| 666 | * @param array|string $options If an array, should be an array of attributes that $key needs to be added to. |
||
| 667 | * If a string or null, will be used as the View entity. |
||
| 668 | * @param string $field |
||
| 669 | * @param string $key The name of the attribute to be set, defaults to 'name' |
||
| 670 | * @return mixed If an array was given for $options, an array with $key set will be returned. |
||
| 671 | * If a string was supplied a string will be returned. |
||
| 672 | */ |
||
| 673 | protected function _name($options = array(), $field = null, $key = 'name') { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Gets the data for the current tag |
||
| 706 | * |
||
| 707 | * @param array|string $options If an array, should be an array of attributes that $key needs to be added to. |
||
| 708 | * If a string or null, will be used as the View entity. |
||
| 709 | * @param string $field |
||
| 710 | * @param string $key The name of the attribute to be set, defaults to 'value' |
||
| 711 | * @return mixed If an array was given for $options, an array with $key set will be returned. |
||
| 712 | * If a string was supplied a string will be returned. |
||
| 713 | */ |
||
| 714 | public function value($options = array(), $field = null, $key = 'value') { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Sets the defaults for an input tag. Will set the |
||
| 763 | * name, value, and id attributes for an array of html attributes. |
||
| 764 | * |
||
| 765 | * @param string $field The field name to initialize. |
||
| 766 | * @param array $options Array of options to use while initializing an input field. |
||
| 767 | * @return array Array options for the form input. |
||
| 768 | */ |
||
| 769 | protected function _initInputField($field, $options = array()) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Adds the given class to the element options |
||
| 782 | * |
||
| 783 | * @param array $options Array options/attributes to add a class to |
||
| 784 | * @param string $class The class name being added. |
||
| 785 | * @param string $key the key to use for class. |
||
| 786 | * @return array Array of options with $key set. |
||
| 787 | */ |
||
| 788 | public function addClass($options = array(), $class = null, $key = 'class') { |
||
| 796 | |||
| 797 | /** |
||
| 798 | * Returns a string generated by a helper method |
||
| 799 | * |
||
| 800 | * This method can be overridden in subclasses to do generalized output post-processing |
||
| 801 | * |
||
| 802 | * @param string $str String to be output. |
||
| 803 | * @return string |
||
| 804 | * @deprecated This method will be removed in future versions. |
||
| 805 | */ |
||
| 806 | public function output($str) { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * Before render callback. beforeRender is called before the view file is rendered. |
||
| 812 | * |
||
| 813 | * Overridden in subclasses. |
||
| 814 | * |
||
| 815 | * @param string $viewFile The view file that is going to be rendered |
||
| 816 | * @return void |
||
| 817 | */ |
||
| 818 | public function beforeRender($viewFile) { |
||
| 820 | |||
| 821 | /** |
||
| 822 | * After render callback. afterRender is called after the view file is rendered |
||
| 823 | * but before the layout has been rendered. |
||
| 824 | * |
||
| 825 | * Overridden in subclasses. |
||
| 826 | * |
||
| 827 | * @param string $viewFile The view file that was rendered. |
||
| 828 | * @return void |
||
| 829 | */ |
||
| 830 | public function afterRender($viewFile) { |
||
| 832 | |||
| 833 | /** |
||
| 834 | * Before layout callback. beforeLayout is called before the layout is rendered. |
||
| 835 | * |
||
| 836 | * Overridden in subclasses. |
||
| 837 | * |
||
| 838 | * @param string $layoutFile The layout about to be rendered. |
||
| 839 | * @return void |
||
| 840 | */ |
||
| 841 | public function beforeLayout($layoutFile) { |
||
| 843 | |||
| 844 | /** |
||
| 845 | * After layout callback. afterLayout is called after the layout has rendered. |
||
| 846 | * |
||
| 847 | * Overridden in subclasses. |
||
| 848 | * |
||
| 849 | * @param string $layoutFile The layout file that was rendered. |
||
| 850 | * @return void |
||
| 851 | */ |
||
| 852 | public function afterLayout($layoutFile) { |
||
| 854 | |||
| 855 | /** |
||
| 856 | * Before render file callback. |
||
| 857 | * Called before any view fragment is rendered. |
||
| 858 | * |
||
| 859 | * Overridden in subclasses. |
||
| 860 | * |
||
| 861 | * @param string $viewFile The file about to be rendered. |
||
| 862 | * @return void |
||
| 863 | */ |
||
| 864 | public function beforeRenderFile($viewFile) { |
||
| 866 | |||
| 867 | /** |
||
| 868 | * After render file callback. |
||
| 869 | * Called after any view fragment is rendered. |
||
| 870 | * |
||
| 871 | * Overridden in subclasses. |
||
| 872 | * |
||
| 873 | * @param string $viewFile The file just be rendered. |
||
| 874 | * @param string $content The content that was rendered. |
||
| 875 | * @return void |
||
| 876 | */ |
||
| 877 | public function afterRenderFile($viewFile, $content) { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Transforms a recordset from a hasAndBelongsToMany association to a list of selected |
||
| 882 | * options for a multiple select element |
||
| 883 | * |
||
| 884 | * @param string|array $data |
||
| 885 | * @param string $key |
||
| 886 | * @return array |
||
| 887 | */ |
||
| 888 | protected function _selectedArray($data, $key = 'id') { |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Resets the vars used by Helper::clean() to null |
||
| 911 | * |
||
| 912 | * @return void |
||
| 913 | */ |
||
| 914 | protected function _reset() { |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Removes harmful content from output |
||
| 921 | * |
||
| 922 | * @return void |
||
| 923 | */ |
||
| 924 | protected function _clean() { |
||
| 950 | |||
| 951 | } |
||
| 952 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.