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 |
||
18 | class Vasri |
||
19 | { |
||
20 | |||
21 | |||
22 | /** |
||
23 | * @var Builder |
||
24 | */ |
||
25 | private $builder; |
||
26 | |||
27 | /** |
||
28 | * @var ManifestReader |
||
29 | */ |
||
30 | private $manifestReader; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $vasriManifest; |
||
36 | |||
37 | /** |
||
38 | * @var mixed |
||
39 | */ |
||
40 | private $appEnvironment; |
||
41 | |||
42 | /** |
||
43 | * @var |
||
44 | */ |
||
45 | private $vasriConfig; |
||
46 | |||
47 | /** |
||
48 | * @var |
||
49 | */ |
||
50 | private $isMixManifestAltEnabled; |
||
51 | |||
52 | /** |
||
53 | * Vasri constructor. |
||
54 | */ |
||
55 | View Code Duplication | public function __construct() |
|
64 | |||
65 | /** |
||
66 | * The Vasri helper function |
||
67 | * |
||
68 | * @param string $file |
||
69 | * @param bool $enableVersioning |
||
70 | * @param bool $enableSRI |
||
71 | * |
||
72 | * @param string $keyword |
||
73 | * |
||
74 | * @return string |
||
75 | * @throws Exception |
||
76 | */ |
||
77 | public function vasri( |
||
95 | |||
96 | /** |
||
97 | * Fetches the SRI hash from the Vasri Manifest and adds the crossorigin attribute |
||
98 | * |
||
99 | * @param string $file |
||
100 | * |
||
101 | * @param string $keyword |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | private function getSRI(string $file, string $keyword): string |
||
110 | |||
111 | /** |
||
112 | * @param string $file |
||
113 | */ |
||
114 | private function checkFileName(string &$file): void |
||
122 | |||
123 | /** |
||
124 | * Builds all the attributes |
||
125 | * |
||
126 | * @param string $file |
||
127 | * @param bool $enableVersioning |
||
128 | * |
||
129 | * @param bool $enableSRI |
||
130 | * @param string $keyword |
||
131 | * |
||
132 | * @return string |
||
133 | * @throws Exception |
||
134 | */ |
||
135 | private function addAttributes(string $file, bool $enableVersioning, bool $enableSRI, string $keyword): string |
||
153 | |||
154 | /** |
||
155 | * Fetches the version query string from the Vasri Manifest |
||
156 | * |
||
157 | * @param string $file |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | private function getVersioning(string $file): string |
||
165 | |||
166 | /** |
||
167 | * Figures out whether or not to toggle versioning and sri |
||
168 | * |
||
169 | * @param bool $enableVersioning |
||
170 | * @param bool $enableSRI |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | private function getOptions(bool $enableVersioning, bool $enableSRI): array |
||
185 | |||
186 | /** |
||
187 | * Gets source attribute based on the extension, adds file path and version |
||
188 | * |
||
189 | * @param string $file |
||
190 | * @param string $version |
||
191 | * |
||
192 | * @return string |
||
193 | * @throws Exception |
||
194 | */ |
||
195 | private function getSourceAttribute(string $file, string $version = ''): string |
||
199 | |||
200 | /** |
||
201 | * Checks if the file is in the Laravel public path |
||
202 | * |
||
203 | * @param string $file |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | private static function isPublicFile(string $file): bool |
||
211 | |||
212 | } |
||
213 |
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.