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 declare(strict_types = 1); |
||
14 | class Filesystem implements FilesystemContract |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @var FilesystemInterface |
||
19 | */ |
||
20 | private $flysystem; |
||
21 | |||
22 | /** |
||
23 | * Filesystem constructor. |
||
24 | * |
||
25 | * @param FilesystemInterface $flysystem |
||
26 | */ |
||
27 | 6 | public function __construct(FilesystemInterface $flysystem) |
|
31 | |||
32 | /** |
||
33 | * @inheritDoc |
||
34 | */ |
||
35 | 1 | public function append(string $path, string $contents, array $config = []): bool |
|
43 | |||
44 | /** |
||
45 | * @inheritDoc |
||
46 | */ |
||
47 | 1 | public function copy(string $path, string $newPath): bool |
|
51 | |||
52 | /** |
||
53 | * @inheritDoc |
||
54 | */ |
||
55 | 1 | public function createDir(string $dirname, array $config = []): bool |
|
59 | |||
60 | /** |
||
61 | * @inheritDoc |
||
62 | */ |
||
63 | 1 | public function delete(string $path): bool |
|
67 | |||
68 | /** |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | 1 | public function deleteDir(string $dirname): bool |
|
75 | |||
76 | /** |
||
77 | * @inheritDoc |
||
78 | */ |
||
79 | 2 | public function exists(string $path): bool |
|
83 | |||
84 | /** |
||
85 | * @inheritDoc |
||
86 | */ |
||
87 | public function list(string $path = '.', bool $recursive = false): array |
||
94 | |||
95 | /** |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 1 | View Code Duplication | public function listDirectories(string $path = '.', bool $recursive = false): array |
108 | |||
109 | /** |
||
110 | * @inheritDoc |
||
111 | */ |
||
112 | 1 | View Code Duplication | public function listFiles(string $path = '.', bool $recursive = false): array |
122 | |||
123 | /** |
||
124 | * @inheritDoc |
||
125 | */ |
||
126 | 1 | public function metadata($path): MetadataContract |
|
130 | |||
131 | /** |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | 1 | public function move(string $path, string $newPath): bool |
|
138 | |||
139 | /** |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | 1 | public function prepend(string $path, string $contents, array $config = []): bool |
|
150 | |||
151 | /** |
||
152 | * @inheritDoc |
||
153 | */ |
||
154 | 2 | public function read(string $path): string |
|
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | 2 | public function write(string $path, string $contents, array $config = []): bool |
|
166 | |||
167 | } |
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.