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 |
||
10 | class ComposeFileCollection |
||
11 | { |
||
12 | /** |
||
13 | * @var ComposeFiles |
||
14 | */ |
||
15 | private $composeFiles = []; |
||
16 | |||
17 | /** |
||
18 | * @var projectName |
||
19 | */ |
||
20 | private $projectName; |
||
21 | |||
22 | /** |
||
23 | * @param array of string or ComposeFile |
||
24 | * |
||
25 | * @throws \Exception When a composeFile definition is invalid |
||
26 | */ |
||
27 | public function __construct() |
||
52 | |||
53 | /** |
||
54 | * Return all composeFiles |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | public function getAll() |
||
62 | |||
63 | /** |
||
64 | * Add a compose file |
||
65 | */ |
||
66 | public function add(ComposeFile $composeFile) |
||
71 | |||
72 | /** |
||
73 | * Set Project Name |
||
74 | * |
||
75 | * @param string $projectName |
||
76 | * |
||
77 | * @return ComposeFileCollection |
||
78 | */ |
||
79 | public function setProjectName($projectName) |
||
84 | |||
85 | /** |
||
86 | * Get Project Name |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getProjectName() |
||
94 | } |
||
95 |
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.