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 |
||
| 21 | class HtmlHelper |
||
| 22 | { |
||
| 23 | use SingleInstanceTrait; |
||
| 24 | |||
| 25 | |||
| 26 | /** |
||
| 27 | * Css references |
||
| 28 | * |
||
| 29 | * Format: {name: {href, media}} |
||
| 30 | * |
||
| 31 | * For media type and query: |
||
| 32 | * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media |
||
| 33 | * @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $css = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Javascript references |
||
| 41 | * |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $javascript = []; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $rootPath = ''; |
||
| 50 | |||
| 51 | |||
| 52 | /** |
||
| 53 | * Add a css reference |
||
| 54 | * |
||
| 55 | * @param string $name |
||
| 56 | * @param string $href |
||
| 57 | * @param string $media |
||
| 58 | * @return static |
||
| 59 | */ |
||
| 60 | public function addCss($name, $href, $media = 'all') |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Add a js reference |
||
| 73 | * |
||
| 74 | * @param string $name |
||
| 75 | * @param string $path |
||
| 76 | * @return static |
||
| 77 | */ |
||
| 78 | public function addJs($name, $path) |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * Clear all css reference |
||
| 88 | * |
||
| 89 | * @return static |
||
| 90 | */ |
||
| 91 | public function clearCss() |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * Clear all js reference |
||
| 101 | * |
||
| 102 | * @return static |
||
| 103 | */ |
||
| 104 | public function clearJs() |
||
| 110 | |||
| 111 | |||
| 112 | /** |
||
| 113 | * Getter of css reference |
||
| 114 | * |
||
| 115 | * @param string $name Use '*' for all, or string name for single. |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function getCss($name = '*') |
|
| 130 | |||
| 131 | |||
| 132 | /** |
||
| 133 | * Getter of js reference |
||
| 134 | * |
||
| 135 | * @param string $name Use '*' for all, or string name for single. |
||
| 136 | * @return array|string |
||
| 137 | */ |
||
| 138 | View Code Duplication | public function getJs($name = '*') |
|
| 150 | |||
| 151 | |||
| 152 | /** |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getRootPath() |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Remove a css reference |
||
| 163 | * |
||
| 164 | * @param string $name |
||
| 165 | * @return static |
||
| 166 | */ |
||
| 167 | public function removeCss($name) |
||
| 173 | |||
| 174 | |||
| 175 | /** |
||
| 176 | * Remove a js reference |
||
| 177 | * |
||
| 178 | * @param string $name |
||
| 179 | * @return static |
||
| 180 | */ |
||
| 181 | public function removeJs($name) |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @param string $rootPath |
||
| 191 | * @return static |
||
| 192 | */ |
||
| 193 | public function setRootPath($rootPath) |
||
| 205 | } |
||
| 206 |
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.