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 |
||
| 15 | class Html extends BaseHtml |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Render html 5 audio tag structure. |
||
| 19 | * |
||
| 20 | * @param string $src |
||
| 21 | * @param array $options |
||
| 22 | * |
||
| 23 | * @return string |
||
| 24 | */ |
||
| 25 | public static function audio(string $src, array $options = []): string |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Render html 5 video tag structure. |
||
| 36 | * |
||
| 37 | * @param string $src |
||
| 38 | * @param array $options |
||
| 39 | * |
||
| 40 | * @return string |
||
| 41 | */ |
||
| 42 | public static function video(string $src, array $options = []): string |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Get main options of the mediafile. |
||
| 55 | * |
||
| 56 | * @param array $options |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | View Code Duplication | private static function getMainOptions(array $options = []): array |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Get source options of the mediafile. |
||
| 74 | * |
||
| 75 | * @param string $src |
||
| 76 | * @param array $options |
||
| 77 | * |
||
| 78 | * @return array |
||
| 79 | */ |
||
| 80 | View Code Duplication | private static function getSourceOptions(string $src, array $options = []): array |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get track options of the mediafile. |
||
| 94 | * |
||
| 95 | * @param array $options |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | private static function getTrackOptions(array $options = []): array |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get tag of the mediafile. |
||
| 112 | * |
||
| 113 | * @param string $tagName |
||
| 114 | * @param array $mainOptions |
||
| 115 | * @param array $sourceOptions |
||
| 116 | * @param array $trackOptions |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | private static function getTag( |
||
| 132 | } |
||
| 133 |
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.