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 Tar extends SimulatorExecutable implements Simulator |
||
23 | { |
||
24 | /** |
||
25 | * Tar Executable |
||
26 | * |
||
27 | * @var \phpbu\App\Cli\Executable\Tar |
||
28 | */ |
||
29 | protected $executable; |
||
30 | |||
31 | /** |
||
32 | * Path to executable. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $pathToTar; |
||
37 | |||
38 | /** |
||
39 | * Path to backup |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | private $path; |
||
44 | |||
45 | /** |
||
46 | * Tar should ignore failed reads |
||
47 | * --ignore-failed-read |
||
48 | * |
||
49 | * @var boolean |
||
50 | */ |
||
51 | private $ignoreFailedRead; |
||
52 | |||
53 | /** |
||
54 | * Remove the packed data |
||
55 | * |
||
56 | * @var boolean |
||
57 | */ |
||
58 | private $removeSourceDir; |
||
59 | |||
60 | /** |
||
61 | * Compression to use. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private $compression = ''; |
||
66 | |||
67 | /** |
||
68 | * Path where to store the archive. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $pathToArchive; |
||
73 | |||
74 | /** |
||
75 | * Setup. |
||
76 | * |
||
77 | * @see \phpbu\App\Backup\Source |
||
78 | * @param array $conf |
||
79 | * @throws \phpbu\App\Exception |
||
80 | */ |
||
81 | 7 | public function setup(array $conf = []) |
|
92 | |||
93 | /** |
||
94 | * Execute the backup. |
||
95 | * |
||
96 | * @see \phpbu\App\Backup\Source |
||
97 | * @param \phpbu\App\Backup\Target $target |
||
98 | * @param \phpbu\App\Result $result |
||
99 | * @return \phpbu\App\Backup\Source\Status |
||
100 | * @throws \phpbu\App\Exception |
||
101 | */ |
||
102 | 3 | View Code Duplication | public function backup(Target $target, Result $result) : Status |
118 | |||
119 | /** |
||
120 | 2 | * Check if tar failed. |
|
121 | * |
||
122 | * @param \phpbu\App\Cli\Result |
||
123 | * @return bool |
||
124 | */ |
||
125 | public function tarFailed(CliResult $tar) : bool |
||
130 | |||
131 | 6 | /** |
|
132 | * Setup the Executable to run the 'tar' command. |
||
133 | 3 | * |
|
134 | 2 | * @param \phpbu\App\Backup\Target |
|
135 | 1 | * @return \phpbu\App\Cli\Executable |
|
136 | 1 | */ |
|
137 | protected function createExecutable(Target $target) : Executable |
||
161 | |||
162 | /** |
||
163 | * Check the source to compress. |
||
164 | * |
||
165 | * @throws \phpbu\App\Exception |
||
166 | */ |
||
167 | private function validatePath() |
||
173 | |||
174 | /** |
||
175 | * Create backup status. |
||
176 | * |
||
177 | * @param \phpbu\App\Backup\Target |
||
178 | * @return \phpbu\App\Backup\Source\Status |
||
179 | */ |
||
180 | protected function createStatus(Target $target) : Status |
||
189 | } |
||
190 |
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.