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 |
||
9 | class Locator |
||
10 | { |
||
11 | /** |
||
12 | * The normalizer associated to the file locator. |
||
13 | * |
||
14 | * @var Normalizer |
||
15 | */ |
||
16 | protected $normalizer; |
||
17 | |||
18 | /** |
||
19 | * Locator constructor. |
||
20 | * |
||
21 | * @param Normalizer $normalizer |
||
22 | */ |
||
23 | 5 | public function __construct(Normalizer $normalizer) |
|
27 | |||
28 | /** |
||
29 | * Locate a source either if it a file or a directory and normalize it. Return an array of SplFileInfo. |
||
30 | * |
||
31 | * @param mixed $source |
||
32 | * @return SplFileInfo[] |
||
33 | * @throws Exception |
||
34 | */ |
||
35 | 5 | public function locate($source) |
|
73 | |||
74 | /** |
||
75 | * Return the normalizer associated to the file locator. |
||
76 | * |
||
77 | * @return Normalizer |
||
78 | */ |
||
79 | 2 | public function getNormalizer() |
|
83 | |||
84 | /** |
||
85 | * Return files sources using the finder to allow wild wards. |
||
86 | * |
||
87 | * @param $source |
||
88 | * |
||
89 | * @return SplFileInfo[] |
||
90 | */ |
||
91 | 2 | protected function getSourcesFromFinder($source) |
|
116 | |||
117 | /** |
||
118 | * Remove the last slash of the string. |
||
119 | * |
||
120 | * @param string $string |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | 2 | View Code Duplication | private function removeLastSlash($string) |
132 | } |
||
133 |
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.