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 |
||
28 | class PrimaryFileSystemWrapper |
||
29 | { |
||
30 | /** |
||
31 | * @var FilesystemInterface |
||
32 | */ |
||
33 | private $fileSystem; |
||
34 | /** |
||
35 | * @var EventDispatcherInterface |
||
36 | */ |
||
37 | private $eventDispatcher; |
||
38 | |||
39 | /** |
||
40 | * PrimaryFileSystemWrapper constructor. |
||
41 | * |
||
42 | * @param EventDispatcherInterface|null $eventDispatcher |
||
43 | * @param FilesystemInterface|null $fileSystem |
||
44 | */ |
||
45 | public function __construct(EventDispatcherInterface $eventDispatcher = null, FilesystemInterface $fileSystem = null) |
||
50 | |||
51 | /** |
||
52 | * @return FilesystemInterface|null |
||
53 | */ |
||
54 | View Code Duplication | public function getFileSystem() |
|
63 | |||
64 | public function getAdapter() |
||
72 | |||
73 | /** |
||
74 | * @param FilesystemInterface $fileSystem |
||
75 | */ |
||
76 | View Code Duplication | public function setFileSystem(FilesystemInterface $fileSystem) |
|
84 | |||
85 | /** |
||
86 | * @param $path |
||
87 | * @param $contents |
||
88 | */ |
||
89 | public function write($path, $contents) |
||
93 | |||
94 | /** |
||
95 | * @param $path |
||
96 | * @param $contents |
||
97 | */ |
||
98 | public function update($path, $contents) |
||
102 | |||
103 | /** |
||
104 | * @param $path |
||
105 | * @param $contents |
||
106 | */ |
||
107 | public function put($path, $contents) |
||
111 | |||
112 | /** |
||
113 | * @param $path |
||
114 | * |
||
115 | * @return false|string |
||
116 | */ |
||
117 | public function read($path) |
||
121 | |||
122 | /** |
||
123 | * @param $path |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function has($path) |
||
131 | |||
132 | /** |
||
133 | * @param $path |
||
134 | */ |
||
135 | public function delete($path) |
||
139 | |||
140 | /** |
||
141 | * @param $path |
||
142 | * |
||
143 | * @return false|string |
||
144 | */ |
||
145 | public function readAndDelete($path) |
||
149 | |||
150 | /** |
||
151 | * @param $currentPath |
||
152 | * @param $newPath |
||
153 | */ |
||
154 | public function rename($currentPath, $newPath) |
||
158 | |||
159 | /** |
||
160 | * @param $currentPath |
||
161 | * @param $duplicatePath |
||
162 | */ |
||
163 | public function copy($currentPath, $duplicatePath) |
||
167 | |||
168 | /** |
||
169 | * @param $path |
||
170 | * |
||
171 | * @return false|string |
||
172 | */ |
||
173 | public function getMimetype($path) |
||
177 | |||
178 | /** |
||
179 | * @param $path |
||
180 | * |
||
181 | * @return false|string |
||
182 | */ |
||
183 | public function getTimeStamp($path) |
||
187 | |||
188 | /** |
||
189 | * @param $path |
||
190 | * |
||
191 | * @return false|int |
||
192 | */ |
||
193 | public function getSize($path) |
||
197 | |||
198 | /** |
||
199 | * @param $path |
||
200 | */ |
||
201 | public function createDir($path) |
||
205 | |||
206 | /** |
||
207 | * @param $path |
||
208 | */ |
||
209 | public function deleteDir($path) |
||
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.