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 ProjectVersion |
||
9 | { |
||
10 | private static $instance; |
||
11 | private $structure; |
||
12 | |||
13 | /** |
||
14 | * @return $this |
||
15 | */ |
||
16 | 2 | final public static function getInstance() |
|
24 | |||
25 | /** |
||
26 | * private construct - singleton |
||
27 | */ |
||
28 | 1 | private function __construct() |
|
31 | |||
32 | /** |
||
33 | * private clone - singleton |
||
34 | * @codeCoverageIgnore |
||
35 | */ |
||
36 | private function __clone() |
||
39 | |||
40 | /** |
||
41 | * Get version name |
||
42 | * @return string |
||
43 | */ |
||
44 | 6 | public function get() |
|
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | * @codeCoverageIgnore |
||
63 | */ |
||
64 | protected function getFromGit() |
||
70 | |||
71 | /** |
||
72 | * Retrieve environment variable |
||
73 | * @param string $name |
||
74 | * @return array|false|string |
||
75 | * @codeCoverageIgnore |
||
76 | */ |
||
77 | protected function getEnv($name) |
||
81 | |||
82 | 1 | public function name() |
|
86 | |||
87 | /** |
||
88 | * Return composer.json path to project |
||
89 | * @return string |
||
90 | */ |
||
91 | 1 | protected function getComposerPath() |
|
95 | |||
96 | /** |
||
97 | * Get composer file structure |
||
98 | * @return Object |
||
99 | */ |
||
100 | 3 | View Code Duplication | protected function getStructure() |
113 | } |
||
114 |
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.