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:
| 1 | <?php |
||
| 31 | class HtmlHelper extends CakeHtmlHelper |
||
| 32 | { |
||
| 33 | |||
| 34 | use HelperTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * List of helpers used by this helper. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | public $helpers = [ |
||
| 42 | 'Core.Less', |
||
| 43 | 'Url' => ['className' => 'Core.Url'], |
||
| 44 | ]; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * HtmlHelper constructor. |
||
| 48 | * |
||
| 49 | * @param View $View |
||
| 50 | * @param array $config |
||
| 51 | */ |
||
| 52 | View Code Duplication | public function __construct(View $View, array $config = []) |
|
|
|
|||
| 53 | { |
||
| 54 | parent::__construct($View, $config); |
||
| 55 | $this->_configWrite('btnPref', Configure::read('Cms.btnPref')); |
||
| 56 | $this->_configWrite('iconPref', Configure::read('Cms.iconPref')); |
||
| 57 | $this->_configWrite('classPrefix', Configure::read('Cms.classPrefix')); |
||
| 58 | $this->_configWrite('templates.icon', '<i class="{{class}}"{{attrs}}></i>'); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create icon element. |
||
| 63 | * |
||
| 64 | * @param string $icon |
||
| 65 | * @param array $options |
||
| 66 | * @return null|string |
||
| 67 | */ |
||
| 68 | public function icon($icon = 'home', array $options = []) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Creates a CSS stylesheets from less. |
||
| 90 | * |
||
| 91 | * @param string|array $path |
||
| 92 | * @param array $options |
||
| 93 | * @return null|string |
||
| 94 | */ |
||
| 95 | public function less($path, array $options = []) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Create an html link. |
||
| 120 | * |
||
| 121 | * @param string $title |
||
| 122 | * @param null|string|array $url |
||
| 123 | * @param array $options |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function link($title, $url = null, array $options = []) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Check if need escape link title. |
||
| 165 | * |
||
| 166 | * @param string $title |
||
| 167 | * @param bool $isClear |
||
| 168 | * @param array $options |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | protected function _isEscapeTitle($title, $isClear, array $options = []) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Setup default title attr. |
||
| 178 | * |
||
| 179 | * @param string $title |
||
| 180 | * @param array $options |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function _setTitleAttr($title, array $options = []) |
||
| 191 | } |
||
| 192 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.