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 |
||
36 | abstract class AbstractModule implements ModuleInterface, ConfigProviderInterface |
||
37 | { |
||
38 | /** |
||
39 | * @var string The Module name. |
||
40 | */ |
||
41 | protected $name; |
||
42 | |||
43 | /** |
||
44 | * @var \ReflectionObject |
||
45 | */ |
||
46 | protected $reflected; |
||
47 | |||
48 | /** |
||
49 | * @todo Add inline documentation. |
||
50 | * |
||
51 | * @var null |
||
52 | */ |
||
53 | protected $config; |
||
54 | |||
55 | /** |
||
56 | * Configuration loader. |
||
57 | * |
||
58 | * @var null|\PPI\Framework\Config\ConfigLoader |
||
59 | */ |
||
60 | protected $configLoader; |
||
61 | |||
62 | /** |
||
63 | * @todo Add inline documentation. |
||
64 | * |
||
65 | * @var null |
||
66 | */ |
||
67 | protected $routes; |
||
68 | |||
69 | /** |
||
70 | * @todo Add inline documentation. |
||
71 | * |
||
72 | * @var null |
||
73 | */ |
||
74 | protected $services; |
||
75 | |||
76 | /** |
||
77 | * Load up our routes. |
||
78 | * |
||
79 | * @param string $path |
||
80 | * |
||
81 | * @return \Symfony\Component\Routing\RouteCollection |
||
82 | */ |
||
83 | View Code Duplication | protected function loadSymfonyRoutes($path) |
|
95 | |||
96 | /** |
||
97 | * Load up our routes. |
||
98 | * |
||
99 | * @deprecated Please use loadSymfonyRoutes instead |
||
100 | * @param string $path |
||
101 | * @return \Symfony\Component\Routing\RouteCollection |
||
102 | */ |
||
103 | View Code Duplication | protected function loadYamlRoutes($path) |
|
115 | |||
116 | /** |
||
117 | * @param string $path |
||
118 | * @return AuraRouter |
||
119 | * @throws \Exception when the included routes file doesn't return an AuraRouter back |
||
120 | */ |
||
121 | protected function loadLaravelRoutes($path) |
||
129 | |||
130 | /** |
||
131 | * @param $path |
||
132 | * @return FastRouteWrapper |
||
133 | */ |
||
134 | protected function loadFastRouteRoutes($path) |
||
167 | |||
168 | /** |
||
169 | * @todo - move part of this into AuraRouterWrapper->load() |
||
170 | * @todo - consider adding a setModuleName() to the AuraRouterWrapper instead of _module to each route. |
||
171 | * @param string $path |
||
172 | * @return AuraRouter |
||
173 | * @throws \Exception when the included routes file doesn't return an AuraRouter back |
||
174 | */ |
||
175 | protected function loadAuraRoutes($path) |
||
200 | |||
201 | /** |
||
202 | * Load up our config results from the specific yaml file. |
||
203 | * |
||
204 | * @param string $path |
||
205 | * |
||
206 | * @return array |
||
207 | * |
||
208 | * @deprecated since version 2.1, to be removed in 2.2. Use "loadConfig()" instead. |
||
209 | */ |
||
210 | protected function loadYamlConfig($path) |
||
214 | |||
215 | /** |
||
216 | * Set services for our module. |
||
217 | * |
||
218 | * @param string $services |
||
219 | * |
||
220 | * @return Module |
||
221 | */ |
||
222 | public function setServices($services) |
||
228 | |||
229 | /** |
||
230 | * Get the services. |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function getServices() |
||
238 | |||
239 | /** |
||
240 | * Get a particular service. |
||
241 | * |
||
242 | * @param string $serviceName |
||
243 | * |
||
244 | * @return mixed |
||
245 | */ |
||
246 | public function getService($serviceName) |
||
250 | |||
251 | /** |
||
252 | * Loads a configuration file (PHP, YAML) or PHP array. |
||
253 | * |
||
254 | * @param string $resource The resource |
||
255 | * @param null|string $type The resource type |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | public function loadConfig($resource, $type = null) |
||
263 | |||
264 | /** |
||
265 | * Loads and merges the configuration. |
||
266 | * |
||
267 | * @param mixed $resources |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | public function mergeConfig($resources) |
||
280 | |||
281 | /** |
||
282 | * Set the module name. |
||
283 | * |
||
284 | * @param string $Name |
||
285 | * |
||
286 | * @return $this |
||
287 | */ |
||
288 | public function setName($Name) |
||
294 | |||
295 | /** |
||
296 | * Returns the module name. Defaults to the module namespace stripped of backslashes. |
||
297 | * |
||
298 | * @return string The Module name |
||
299 | */ |
||
300 | public function getName() |
||
310 | |||
311 | /** |
||
312 | * Gets the Module namespace. |
||
313 | * |
||
314 | * @return string The Module namespace |
||
315 | * |
||
316 | * @api |
||
317 | */ |
||
318 | public function getNamespace() |
||
326 | |||
327 | /** |
||
328 | * Gets the Module directory path. |
||
329 | * |
||
330 | * @return string The Module absolute path |
||
331 | * |
||
332 | * @api |
||
333 | */ |
||
334 | public function getPath() |
||
342 | |||
343 | /** |
||
344 | * Returns configuration to merge with application configuration. |
||
345 | * |
||
346 | * @return array|\Traversable |
||
347 | */ |
||
348 | public function getConfig() |
||
352 | |||
353 | /** |
||
354 | * Finds and registers Commands. |
||
355 | * |
||
356 | * Override this method if your module commands do not follow the conventions: |
||
357 | * |
||
358 | * * Commands are in the 'Command' sub-directory |
||
359 | * * Commands extend PPI\Framework\Console\Command\AbstractCommand |
||
360 | * |
||
361 | * @param Application $application An Application instance |
||
362 | */ |
||
363 | public function registerCommands(Application $application) |
||
384 | |||
385 | /** |
||
386 | * Returns a ConfigLoader instance. |
||
387 | * |
||
388 | * @return \PPI\Framework\Config\ConfigLoader |
||
389 | */ |
||
390 | protected function getConfigLoader() |
||
398 | |||
399 | } |
||
400 |
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.