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 |
||
8 | class TextFile implements LoggerInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $logDirectory; |
||
14 | |||
15 | /** |
||
16 | * @var FileSystem |
||
17 | */ |
||
18 | private $fileSystem; |
||
19 | |||
20 | /** |
||
21 | * TextFile constructor. |
||
22 | * |
||
23 | * @param FileSystem $fileSystem |
||
24 | * @param Config $config |
||
25 | */ |
||
26 | public function __construct(FileSystem $fileSystem, Config $config) |
||
31 | |||
32 | /** |
||
33 | * @param string $jobId |
||
34 | * @param int $startTime |
||
35 | * @param int $endTime |
||
36 | * @param bool $isSuccessful |
||
37 | * @param array $output |
||
38 | * @param array $error |
||
39 | */ |
||
40 | public function log($jobId, $startTime, $endTime, $isSuccessful, $output, $error) |
||
60 | |||
61 | /** |
||
62 | * @param int $startTime |
||
63 | * @param int $endTime |
||
64 | * @return string |
||
65 | */ |
||
66 | private function getExecutionTime($startTime, $endTime) |
||
93 | |||
94 | /** |
||
95 | * @param $output |
||
96 | * @return string|void |
||
97 | */ |
||
98 | View Code Duplication | private function prepareOutput($output) |
|
106 | |||
107 | /** |
||
108 | * @param $error |
||
109 | * @return string|void |
||
110 | */ |
||
111 | View Code Duplication | private function prepareError($error) |
|
119 | } |
||
120 |
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.