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 |
||
9 | class FileManagerDatabaseStorage implements FileManagerStorage { |
||
10 | |||
11 | protected $connection; |
||
12 | protected $options = []; |
||
13 | |||
14 | /** |
||
15 | * |
||
16 | */ |
||
17 | public function __construct(ConnectionInterface $connection, array $options = []) { |
||
21 | |||
22 | /** |
||
23 | * |
||
24 | */ |
||
25 | public function getUsageCount(ManagedFile $file) { |
||
31 | |||
32 | /** |
||
33 | * |
||
34 | */ |
||
35 | public function getUsages(ManagedFile $file) { |
||
41 | |||
42 | /** |
||
43 | * |
||
44 | */ |
||
45 | public function getFiles(array $conditions = [], array $options = []) { |
||
54 | |||
55 | /** |
||
56 | * |
||
57 | */ |
||
58 | public function getFileByPath($path) { |
||
64 | |||
65 | /** |
||
66 | * |
||
67 | */ |
||
68 | public function getFile($id) { |
||
74 | |||
75 | /** |
||
76 | * |
||
77 | */ |
||
78 | View Code Duplication | public function addFile(ManagedFile $file) { |
|
86 | |||
87 | /** |
||
88 | * |
||
89 | */ |
||
90 | public function removeFile(ManagedFile $file) { |
||
93 | |||
94 | /** |
||
95 | * |
||
96 | */ |
||
97 | View Code Duplication | public function addUsage(ManagedFile $file, array $params) { |
|
106 | |||
107 | /** |
||
108 | * |
||
109 | */ |
||
110 | public function removeUsage(ManagedFile $file = null, array $params) { |
||
123 | |||
124 | } |
||
125 |
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.