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 |
||
18 | final class DoctrineDbalServiceProvider implements ServiceProviderInterface |
||
19 | { |
||
20 | /** |
||
21 | * @param Container $container |
||
22 | */ |
||
23 | 3 | public function register(Container $container) |
|
34 | |||
35 | /** |
||
36 | * @return array |
||
37 | */ |
||
38 | 3 | private function getDbDefaultOptions(): array |
|
48 | |||
49 | /** |
||
50 | * @param Container $container |
||
51 | * |
||
52 | * @return callable |
||
53 | */ |
||
54 | private function getDbsOptionsInitializerDefinition(Container $container): callable |
||
55 | { |
||
56 | 3 | return $container->protect(function () use ($container) { |
|
57 | 3 | static $initialized = false; |
|
58 | |||
59 | 3 | if ($initialized) { |
|
60 | 3 | return; |
|
61 | } |
||
62 | |||
63 | 3 | $initialized = true; |
|
64 | |||
65 | 3 | if (!isset($container['doctrine.dbal.dbs.options'])) { |
|
66 | 2 | $container['doctrine.dbal.dbs.options'] = [ |
|
67 | 2 | 'default' => $container['doctrine.dbal.db.options'] ?? [], |
|
68 | ]; |
||
69 | } |
||
70 | |||
71 | 3 | $tmp = $container['doctrine.dbal.dbs.options']; |
|
72 | 3 | foreach ($tmp as $name => &$options) { |
|
73 | 3 | $options = array_replace($container['doctrine.dbal.db.default_options'], $options); |
|
74 | |||
75 | 3 | if (!isset($container['doctrine.dbal.dbs.default'])) { |
|
76 | 3 | $container['doctrine.dbal.dbs.default'] = $name; |
|
77 | } |
||
78 | } |
||
79 | |||
80 | 3 | $container['doctrine.dbal.dbs.options'] = $tmp; |
|
81 | 3 | }); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param Container $container |
||
86 | * |
||
87 | * @return callable |
||
88 | */ |
||
89 | private function getDbsDefinition(Container $container): callable |
||
113 | |||
114 | /** |
||
115 | * @param Container $container |
||
116 | * |
||
117 | * @return callable |
||
118 | */ |
||
119 | private function getDbsConfigDefinition(Container $container): callable |
||
141 | |||
142 | 3 | /** |
|
143 | 3 | * @param Container $container |
|
144 | * |
||
145 | * @return callable |
||
146 | */ |
||
147 | View Code Duplication | private function getDbsEventManagerDefinition(Container $container): callable |
|
162 | |||
163 | 3 | /*** |
|
164 | 3 | * @param Container $container |
|
165 | * @return callable |
||
166 | */ |
||
167 | private function getDbDefinition(Container $container): callable |
||
175 | |||
176 | 2 | /*** |
|
177 | 3 | * @param Container $container |
|
178 | * @return callable |
||
179 | */ |
||
180 | private function getDbConfigDefinition(Container $container): callable |
||
188 | |||
189 | 3 | /*** |
|
190 | 3 | * @param Container $container |
|
191 | * @return callable |
||
192 | */ |
||
193 | private function getDbEventManagerDefinition(Container $container): callable |
||
201 | } |
||
202 |
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.