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 |
||
7 | View Code Duplication | class PhpUnitTool implements CommandInterface |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * @var bool |
||
11 | */ |
||
12 | private $randomMode; |
||
13 | /** |
||
14 | * @var null|string |
||
15 | */ |
||
16 | private $options; |
||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | private $errorMessage; |
||
21 | |||
22 | /** |
||
23 | * PhpUnitToolCommand constructor. |
||
24 | * |
||
25 | * @param bool $randomMode |
||
26 | * @param string|null $options |
||
27 | * @param string $errorMessage |
||
28 | */ |
||
29 | 4 | public function __construct($randomMode, $options, $errorMessage) |
|
35 | |||
36 | /** |
||
37 | * @return bool |
||
38 | */ |
||
39 | 2 | public function isRandomMode() |
|
43 | |||
44 | /** |
||
45 | * @return null|string |
||
46 | */ |
||
47 | 2 | public function getOptions() |
|
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | 2 | public function getErrorMessage() |
|
59 | } |
||
60 |
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.