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 /** MicroTagTrait */ |
||
| 18 | trait TagTrait |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Render BR tag |
||
| 22 | * |
||
| 23 | * @access public |
||
| 24 | * |
||
| 25 | * @param integer $num number of render BR's |
||
| 26 | * @param array $attributes attributes tag |
||
| 27 | * |
||
| 28 | * @return string |
||
| 29 | * @static |
||
| 30 | */ |
||
| 31 | public static function br($num = 1, array $attributes = []) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Render tag |
||
| 43 | * |
||
| 44 | * @access public |
||
| 45 | * |
||
| 46 | * @param string $name tag name |
||
| 47 | * @param array $attributes tag attributes |
||
| 48 | * |
||
| 49 | * @return string |
||
| 50 | * @static |
||
| 51 | */ |
||
| 52 | View Code Duplication | public static function tag($name, array $attributes = []) |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Render mail a tag |
||
| 64 | * |
||
| 65 | * @access public |
||
| 66 | * |
||
| 67 | * @param string $name name of e-mail |
||
| 68 | * @param string $email e-mail path |
||
| 69 | * @param array $attributes attributes tag |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | * @static |
||
| 73 | */ |
||
| 74 | public static function mailto($name, $email, array $attributes = []) |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Render open tag |
||
| 83 | * |
||
| 84 | * @access public |
||
| 85 | * |
||
| 86 | * @param string $name tag name |
||
| 87 | * @param array $attributes tag attributes |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | * @static |
||
| 91 | */ |
||
| 92 | View Code Duplication | public static function openTag($name, array $attributes = []) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * Render close tag |
||
| 104 | * |
||
| 105 | * @access public |
||
| 106 | * |
||
| 107 | * @param string $name tag name |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | * @static |
||
| 111 | */ |
||
| 112 | public static function closeTag($name) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Render anchor |
||
| 119 | * |
||
| 120 | * @access public |
||
| 121 | * |
||
| 122 | * @param string $name name to link |
||
| 123 | * @param string $url path to link |
||
| 124 | * @param array $attributes attributes tag |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | * @static |
||
| 128 | */ |
||
| 129 | public static function href($name, $url, array $attributes = []) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Render H{1-N} tag |
||
| 138 | * |
||
| 139 | * @access public |
||
| 140 | * |
||
| 141 | * @param string $num H number |
||
| 142 | * @param string $value H value |
||
| 143 | * @param array $attributes attributes tag |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | * @static |
||
| 147 | */ |
||
| 148 | public static function heading($num, $value = null, array $attributes = []) |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Render image map tag |
||
| 155 | * |
||
| 156 | * @access public |
||
| 157 | * |
||
| 158 | * @param string $alt Alternative text |
||
| 159 | * @param string $source Path to image |
||
| 160 | * @param string $name Map name |
||
| 161 | * @param array $attributeImg Attributes for image |
||
| 162 | * @param array $coordinates Coordinates for image |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | * @static |
||
| 166 | */ |
||
| 167 | public static function imageMap($alt, $source, $name, array $attributeImg = [], array $coordinates = []) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Render image file |
||
| 182 | * |
||
| 183 | * @access public |
||
| 184 | * |
||
| 185 | * @param string $name name of image |
||
| 186 | * @param string $file path image file |
||
| 187 | * @param array $attributes attributes tag |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | * @static |
||
| 191 | */ |
||
| 192 | public static function image($name, $file, array $attributes = []) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Render object tag |
||
| 202 | * |
||
| 203 | * @access public |
||
| 204 | * |
||
| 205 | * @param string $source Path to content |
||
| 206 | * @param array $attributes Attributes for object |
||
| 207 | * @param array $params Parameters for object |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | * @static |
||
| 211 | */ |
||
| 212 | public static function object($source, array $attributes = [], array $params = []) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Embedding objects (video, audio, flash, etc.) |
||
| 226 | * |
||
| 227 | * @access public |
||
| 228 | * |
||
| 229 | * @param string $source Path to content |
||
| 230 | * @param array $attributes Attributes for embedding |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public static function embed($source, array $attributes = []) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * List elements generator |
||
| 243 | * |
||
| 244 | * @access public |
||
| 245 | * |
||
| 246 | * @param array $items lists multiple array |
||
| 247 | * @param array $attributes attributes tag |
||
| 248 | * @param bool $isNumeric Is a numeric list? |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | * @static |
||
| 252 | */ |
||
| 253 | public static function lists(array $items = [], array $attributes = [], $isNumeric = false) |
||
| 275 | } |
||
| 276 |
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.