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 |
||
| 35 | class HtmlHelper extends CakeHtmlHelper |
||
| 36 | { |
||
| 37 | |||
| 38 | use HelperTrait, IncludeTrait; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * List of helpers used by this helper. |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | public $helpers = [ |
||
| 46 | 'Core.Less', |
||
| 47 | 'Core.Document', |
||
| 48 | 'Url' => ['className' => 'Core.Url'], |
||
| 49 | ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Hold assets data. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $_assets = [ |
||
| 57 | 'styles' => [], |
||
| 58 | 'scripts' => [], |
||
| 59 | ]; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * HtmlHelper constructor. |
||
| 63 | * |
||
| 64 | * @param View $View |
||
| 65 | * @param array $config |
||
| 66 | */ |
||
| 67 | View Code Duplication | public function __construct(View $View, array $config = []) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Creates a link element for CSS stylesheets. |
||
| 78 | * |
||
| 79 | * @param array|string $path |
||
| 80 | * @param array $options |
||
| 81 | * @return null|string |
||
| 82 | */ |
||
| 83 | public function css($path, array $options = []) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get sort assets included list. |
||
| 91 | * |
||
| 92 | * @param string $key |
||
| 93 | * @return array|null |
||
| 94 | */ |
||
| 95 | public function getAssets($key) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Create icon element. |
||
| 107 | * |
||
| 108 | * @param string $icon |
||
| 109 | * @param array $options |
||
| 110 | * @return null|string |
||
| 111 | */ |
||
| 112 | public function icon($icon = 'home', array $options = []) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Creates a CSS stylesheets from less. |
||
| 134 | * |
||
| 135 | * @param string|array $path |
||
| 136 | * @param array $options |
||
| 137 | * @return null|string |
||
| 138 | */ |
||
| 139 | public function less($path, array $options = []) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create an html link. |
||
| 164 | * |
||
| 165 | * @param string $title |
||
| 166 | * @param null|string|array $url |
||
| 167 | * @param array $options |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public function link($title, $url = null, array $options = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Returns one or many `<script>` tags depending on the number of scripts given. |
||
| 200 | * |
||
| 201 | * @param array|string $path |
||
| 202 | * @param array $options |
||
| 203 | * @return null|string |
||
| 204 | */ |
||
| 205 | public function script($path, array $options = []) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Create status icon or link. |
||
| 212 | * |
||
| 213 | * @param int $status |
||
| 214 | * @param array $url |
||
| 215 | * @param string $icon |
||
| 216 | * @return null|string |
||
| 217 | */ |
||
| 218 | public function status($status = 0, array $url = [], $icon = 'circle') |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Create and render ajax toggle element. |
||
| 234 | * |
||
| 235 | * @param Entity $entity |
||
| 236 | * @param array $url |
||
| 237 | * @param array $data |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function toggle(Entity $entity, array $url = [], array $data = []) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Check if need escape link title. |
||
| 263 | * |
||
| 264 | * @param string $title |
||
| 265 | * @param bool $isClear |
||
| 266 | * @param array $options |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | protected function _isEscapeTitle($title, $isClear, array $options = []) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Setup default title attr. |
||
| 276 | * |
||
| 277 | * @param string $title |
||
| 278 | * @param array $options |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | protected function _setTitleAttr($title, array $options = []) |
||
| 289 | } |
||
| 290 |
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.