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 |
||
22 | class UrlBuilder |
||
23 | { |
||
24 | /** |
||
25 | * @var \League\Flysystem\FilesystemInterface|null |
||
26 | */ |
||
27 | private $fileSystem; |
||
28 | /** |
||
29 | * @var array|null |
||
30 | */ |
||
31 | private $config; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $adapterUrlEncoders = []; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $adapterUrlMappings = []; |
||
40 | |||
41 | /** |
||
42 | * @param string $adapterType |
||
43 | * |
||
44 | * @return array |
||
45 | */ |
||
46 | public function getAdapterUrlMappings($adapterType = '') |
||
54 | |||
55 | public function setAdapterUrlMappings($adapterType, $data) |
||
65 | |||
66 | /** |
||
67 | * UrlBuilder constructor. |
||
68 | * |
||
69 | * @param PrimaryFileSystemWrapper|null $PrimaryFileSystemWrapper |
||
70 | * @param array|null $config |
||
71 | */ |
||
72 | public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper = null, array $config = null) |
||
79 | |||
80 | /** |
||
81 | * @param $key |
||
82 | * @param UrlEncoderInterface $encoder |
||
83 | */ |
||
84 | public function setAdapterUrlEncoder($key, UrlEncoderInterface $encoder) |
||
88 | |||
89 | /** |
||
90 | * @param $relativeFilePath |
||
91 | * @param array $urlData |
||
92 | * |
||
93 | * @return string |
||
94 | * @internal param string $adapterUrlData |
||
95 | */ |
||
96 | public function filePublicUrl($relativeFilePath, array $urlData = []) |
||
130 | |||
131 | private function getAdapterTypeFromFilesystem() |
||
137 | |||
138 | /** |
||
139 | * @param $base |
||
140 | * @param $path |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | protected function formatAsUrl($base, $path) |
||
173 | |||
174 | /** |
||
175 | * @return string |
||
176 | * @internal param array $data |
||
177 | * |
||
178 | */ |
||
179 | protected function getUrlFromFileSystem() |
||
192 | |||
193 | /** |
||
194 | * @param AdapterInterface $adapter |
||
195 | * |
||
196 | * @return mixed |
||
197 | */ |
||
198 | View Code Duplication | protected function getAdapterType(AdapterInterface $adapter) |
|
205 | } |
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.