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 |
||
32 | class WebApplicationServiceProvider implements ServiceProviderInterface |
||
33 | { |
||
34 | /** |
||
35 | * Registers the service provider with a DI container. |
||
36 | * |
||
37 | * @param Container $container The DI container. |
||
38 | * |
||
39 | * @return void |
||
40 | */ |
||
41 | 11 | public function register(Container $container): void |
|
69 | |||
70 | /** |
||
71 | * Get the controller resolver service |
||
72 | * |
||
73 | * @param Container $container The DI container. |
||
74 | * |
||
75 | * @return ControllerResolverInterface |
||
76 | */ |
||
77 | 8 | public function getControllerResolverService(Container $container): ControllerResolverInterface |
|
81 | |||
82 | /** |
||
83 | * Get the DisplayControllerGet class service |
||
84 | * |
||
85 | * @param Container $container The DI container. |
||
86 | * |
||
87 | * @return DisplayStatisticsController |
||
88 | */ |
||
89 | 3 | View Code Duplication | public function getDisplayStatisticsControllerService(Container $container): DisplayStatisticsController |
|
|||
90 | { |
||
91 | 3 | $controller = new DisplayStatisticsController( |
|
92 | 3 | $container->get(StatsJsonView::class) |
|
93 | ); |
||
94 | |||
95 | 3 | $controller->setApplication($container->get(AbstractApplication::class)); |
|
96 | 3 | $controller->setInput($container->get(Input::class)); |
|
97 | |||
98 | 3 | return $controller; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * Get the Input class service |
||
103 | * |
||
104 | * @param Container $container The DI container. |
||
105 | * |
||
106 | * @return Input |
||
107 | */ |
||
108 | 8 | public function getInputService(Container $container): Input |
|
112 | |||
113 | /** |
||
114 | * Get the router service |
||
115 | * |
||
116 | * @param Container $container The DI container. |
||
117 | * |
||
118 | * @return Router |
||
119 | */ |
||
120 | 8 | public function getRouterService(Container $container): Router |
|
144 | |||
145 | /** |
||
146 | * Get the StatsJsonView class service |
||
147 | * |
||
148 | * @param Container $container The DI container. |
||
149 | * |
||
150 | * @return StatsJsonView |
||
151 | */ |
||
152 | 3 | public function getStatsJsonViewService(Container $container): StatsJsonView |
|
153 | { |
||
154 | 3 | return new StatsJsonView( |
|
155 | 3 | $container->get(StatisticsRepository::class) |
|
156 | ); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the SubmitControllerCreate class service |
||
161 | * |
||
162 | * @param Container $container The DI container. |
||
163 | * |
||
164 | * @return SubmitDataController |
||
165 | */ |
||
166 | 4 | View Code Duplication | public function getSubmitDataControllerService(Container $container): SubmitDataController |
167 | { |
||
168 | 4 | $controller = new SubmitDataController( |
|
169 | 4 | $container->get(StatisticsRepository::class), |
|
170 | 4 | $container->get('filesystem.versions') |
|
171 | ); |
||
172 | |||
173 | 4 | $controller->setApplication($container->get(AbstractApplication::class)); |
|
174 | 4 | $controller->setInput($container->get(Input::class)); |
|
175 | |||
176 | 4 | return $controller; |
|
177 | } |
||
178 | |||
179 | /** |
||
180 | * Get the web application service |
||
181 | * |
||
182 | * @param Container $container The DI container. |
||
183 | * |
||
184 | * @return WebApplication |
||
185 | */ |
||
186 | 8 | public function getWebApplicationService(Container $container): WebApplication |
|
203 | |||
204 | /** |
||
205 | * Get the web client service |
||
206 | * |
||
207 | * @param Container $container The DI container. |
||
208 | * |
||
209 | * @return WebClient |
||
210 | */ |
||
211 | 8 | public function getWebClientService(Container $container): WebClient |
|
221 | } |
||
222 |
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.