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 |
||
| 29 | trait HelperTrait |
||
| 30 | { |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Adds the given class to the element options |
||
| 34 | * |
||
| 35 | * @param array $options Array options/attributes to add a class to |
||
| 36 | * @param string $class The class name being added. |
||
| 37 | * @param string $key the key to use for class. |
||
| 38 | * @return array Array of options with $key set. |
||
| 39 | */ |
||
| 40 | protected function _addClass(array $options = [], $class = null, $key = 'class') |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Get class with union prefix. |
||
| 52 | * |
||
| 53 | * @param string $class |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | protected function _class($class = 'cms') |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Create current icon. |
||
| 63 | * |
||
| 64 | * @param HtmlHelper $html |
||
| 65 | * @param string|int $title |
||
| 66 | * @param array $options |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | protected function _createIcon(HtmlHelper $html, $title, array $options = []) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create icon attributes. |
||
| 88 | * |
||
| 89 | * @param array $options |
||
| 90 | * @return array |
||
| 91 | */ |
||
| 92 | protected function _createIconAttr(array $options = []) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create and get button classes. |
||
| 110 | * |
||
| 111 | * @param array $options |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | protected function _getBtnClass(array $options = []) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Create and get tooltip attributes. |
||
| 132 | * |
||
| 133 | * @param array $options |
||
| 134 | * @param string $toggle |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | protected function _getToolTipAttr(array $options = [], $toggle = 'tooltip') |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Prepare before after content for input container. |
||
| 167 | * |
||
| 168 | * @param string|int $type |
||
| 169 | * @param string|int $value |
||
| 170 | * @return null|string |
||
| 171 | */ |
||
| 172 | protected function _prepareBeforeAfterContainer($type = 'before', $value) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Setup button classes by options. |
||
| 194 | * |
||
| 195 | * @param string $button |
||
| 196 | * @param array $options |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | protected function _setBtnClass($button, array $options = []) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Setup icon options. |
||
| 214 | * |
||
| 215 | * @param array $options |
||
| 216 | * @param array $iconOptions |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function _setIconOptions(array $options = [], array $iconOptions = []) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Setup tooltip title by options. |
||
| 241 | * |
||
| 242 | * @param string $tooltip |
||
| 243 | * @param array $options |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | View Code Duplication | protected function _setTooltipTitle($tooltip, array $options = []) |
|
| 258 | } |
||
| 259 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.