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 |
||
23 | class DriverFactory |
||
24 | { |
||
25 | /** |
||
26 | * @var DriverFactory |
||
27 | */ |
||
28 | protected static $instance = null; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $drivers = array(); |
||
34 | |||
35 | /** |
||
36 | * @param string $driver |
||
37 | */ |
||
38 | public static function registerAnnotationDriver($driver) |
||
42 | |||
43 | /** |
||
44 | * @param string $driver |
||
45 | */ |
||
46 | View Code Duplication | protected function addAnnotationDriver($driver) |
|
56 | |||
57 | /** |
||
58 | * @param string $driver |
||
59 | */ |
||
60 | public static function registerYamlDriver($driver) |
||
64 | |||
65 | /** |
||
66 | * @param string $driver |
||
67 | */ |
||
68 | View Code Duplication | protected function addYamlDriver($driver) |
|
78 | |||
79 | /** |
||
80 | * @param string $driver |
||
81 | */ |
||
82 | public static function registerXmlDriver($driver) |
||
86 | |||
87 | /** |
||
88 | * @param string $driver |
||
89 | */ |
||
90 | View Code Duplication | protected function addXmlDriver($driver) |
|
100 | |||
101 | /** |
||
102 | * @param Reader $reader |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function createAnnotationDriver(Reader $reader) |
||
115 | |||
116 | /** |
||
117 | * @param FileLocatorInterface $locator |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | View Code Duplication | public function createYamlDriver(FileLocatorInterface $locator) |
|
130 | |||
131 | /** |
||
132 | * @param FileLocatorInterface $locator |
||
133 | * |
||
134 | * @return array |
||
135 | */ |
||
136 | View Code Duplication | public function createXmlDriver(FileLocatorInterface $locator) |
|
145 | |||
146 | /** |
||
147 | * @param string $driver |
||
148 | */ |
||
149 | private function checkDriver($driver) |
||
160 | |||
161 | /** |
||
162 | * @return DriverFactory |
||
163 | */ |
||
164 | public static function instance() |
||
172 | } |
||
173 |
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.