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 |
||
26 | class DIFactoryConfig extends DI implements ContainerInterface |
||
27 | { |
||
28 | /** |
||
29 | * Create services by using $item as a reference to find a |
||
30 | * configuration for the services. The $item can be an array, |
||
31 | * a file.php, or an directory containing files named *.php. |
||
32 | * |
||
33 | * @param array|string $item referencing the source for configuration. |
||
34 | * |
||
35 | * @return $this |
||
36 | */ |
||
37 | 7 | public function loadServices($item) : object |
|
71 | |||
72 | |||
73 | |||
74 | /** |
||
75 | * Create services from an array containing a list of services. |
||
76 | * |
||
77 | * @param array $service details to use when creating the service. |
||
78 | * |
||
79 | * @throws Exception when configuration is corrupt. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | 6 | protected function createServicesFromArray( |
|
99 | |||
100 | |||
101 | |||
102 | /** |
||
103 | * Create a service from a name and an array containing details on |
||
104 | * how to create it. |
||
105 | * |
||
106 | * @param string $name of service. |
||
107 | * @param array $service details to use when creating the service. |
||
108 | * |
||
109 | * @throws Exception when configuration is corrupt. |
||
110 | * |
||
111 | * @return void |
||
112 | */ |
||
113 | 5 | protected function createService(string $name, array $service) : void |
|
129 | } |
||
130 |
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.