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 |
||
17 | class App |
||
18 | { |
||
19 | /** @var ContainerBuilder */ |
||
20 | protected static $container; |
||
21 | |||
22 | 5 | public static function init() |
|
34 | |||
35 | 4 | public static function get(string $id) |
|
39 | |||
40 | View Code Duplication | public static function developmentServer(): DevelopmentServer |
|
51 | |||
52 | View Code Duplication | public static function productionServer(): ProductionServer |
|
63 | |||
64 | 5 | protected static function loadConfig(array $config): void |
|
70 | |||
71 | 5 | protected static function loadServices(string $servicesPath): void |
|
82 | |||
83 | 5 | protected static function loadPlugins(): void |
|
97 | |||
98 | 5 | protected static function loadPluginConfiguration(string $pluginClass): void |
|
99 | { |
||
100 | 5 | $configurationPath = forward_static_call([$pluginClass, 'getConfigurationPath']); |
|
101 | |||
102 | 5 | if (!$configurationPath) { |
|
103 | return; |
||
104 | } |
||
105 | |||
106 | 5 | if (!file_exists($configurationPath)) { |
|
107 | throw InvalidPlugin::configurationFileNotFound($pluginClass, $configurationPath); |
||
108 | } |
||
109 | |||
110 | 5 | $pluginConfiguration = require $configurationPath; |
|
111 | |||
112 | 5 | if (!is_array($pluginConfiguration)) { |
|
113 | throw InvalidPlugin::configurationMustBeArray($pluginClass, $configurationPath); |
||
114 | } |
||
115 | |||
116 | 5 | self::loadConfig(Arr::dot($pluginConfiguration)); |
|
117 | 5 | } |
|
118 | |||
119 | 5 | protected static function loadPluginServices(string $pluginClass): void |
|
120 | { |
||
121 | 5 | $servicesPath = forward_static_call([$pluginClass, 'getServicesPath']); |
|
122 | |||
123 | 5 | if (!$servicesPath) { |
|
124 | return; |
||
125 | } |
||
126 | |||
127 | 5 | if (!file_exists($servicesPath)) { |
|
128 | throw InvalidPlugin::serviceFileNotFound($pluginClass, $servicesPath); |
||
129 | } |
||
130 | |||
131 | 5 | self::loadServices($servicesPath); |
|
132 | 5 | } |
|
133 | |||
134 | 5 | protected static function registerPluginDefinition(string $pluginClass): void |
|
142 | } |
||
143 |
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.