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:
Complex classes like Configuration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Configuration implements ConfigurationInterface |
||
11 | { |
||
12 | /** |
||
13 | * Generates the configuration tree. |
||
14 | * |
||
15 | * @return TreeBuilder |
||
16 | */ |
||
17 | 9 | public function getConfigTreeBuilder() |
|
18 | { |
||
19 | 9 | $treeBuilder = new TreeBuilder(); |
|
20 | 9 | $rootNode = $treeBuilder->root('dtc_queue'); |
|
21 | |||
22 | $node = $rootNode |
||
23 | 9 | ->children() |
|
24 | 9 | ->append($this->addOrm()) |
|
25 | 9 | ->append($this->addOdm()) |
|
26 | 9 | ->append($this->addManager()) |
|
27 | 9 | ->append($this->addTimings()) |
|
28 | 9 | ->append($this->addBeanstalkd()) |
|
29 | 9 | ->append($this->addRabbitMq()) |
|
30 | 9 | ->append($this->addRedis()) |
|
31 | 9 | ->append($this->addAdmin()) |
|
32 | 9 | ->append($this->addClasses()) |
|
33 | 9 | ->append($this->addPriority()) |
|
34 | 9 | ->append($this->addRetry()); |
|
35 | |||
36 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'document_manager', 'The "%node% option is deprecated, Use "odm: { document_manager: ... }" instead.'); |
|
37 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'entity_manager', 'The "%node% option is deprecated, Use "orm: { entity_manager: ... }" instead.'); |
|
38 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'default_manager', 'The "%node% option is deprecated, Use "manager: { job: ... }" instead.'); |
|
39 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'run_manager', 'The "%node% option is deprecated, Use "manager: { run: ... }" instead.'); |
|
40 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'job_timing_manager', 'The "%node% option is deprecated, Use "manager: { job_timing: ... }" instead.'); |
|
41 | 9 | $node = $this->setDeprecatedNode($node, 'booleanNode', 'record_timings', 'The "%node% option is deprecated, Use "timings: { record: ... }" instead.'); |
|
42 | 9 | $node = $this->setDeprecatedNode($node, 'floatNode', 'record_timings_timezone_offset', 'The "%node% option is deprecated, Use "record: { timezone_offset: ... }" instead.'); |
|
43 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job', 'The "%node% option is deprecated, Use "class: { job: ... }" instead.'); |
|
44 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_archive', 'The "%node% option is deprecated, Use "class: { job_archive: ... }" instead.'); |
|
45 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run', 'The "%node% option is deprecated, Use "class: { run: ... }" instead.'); |
|
46 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_run_archive', 'The "%node% option is deprecated, Use "class: { run_archive: ... }" instead.'); |
|
47 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'class_job_timing', 'The "%node% option is deprecated, Use "class: { job_timing: ... }" instead.'); |
|
48 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_max', 'The "%node% option is deprecated, Use "priority: { max: ... }" instead.'); |
|
49 | 9 | $node = $this->setDeprecatedNode($node, 'scalarNode', 'priority_direction', 'The "%node% option is deprecated, Use "priority: { direction: ... }" instead.'); |
|
50 | 9 | $node->end(); |
|
51 | |||
52 | 9 | return $treeBuilder; |
|
53 | } |
||
54 | |||
55 | 9 | public function setDeprecatedNode($node, $type, $name, $deprecatedMessage) |
|
56 | { |
||
57 | 9 | $node = $node->$type($name); |
|
58 | |||
59 | 9 | if (Kernel::VERSION_ID >= 30400) { |
|
60 | 9 | $node = $node->setDeprecated($deprecatedMessage); |
|
61 | } |
||
62 | |||
63 | 9 | return $node->end(); |
|
64 | } |
||
65 | |||
66 | 9 | protected function addTimings() |
|
67 | { |
||
68 | 9 | $treeBuilder = new TreeBuilder(); |
|
69 | 9 | $rootNode = $treeBuilder->root('timings'); |
|
70 | $rootNode |
||
71 | 9 | ->addDefaultsIfNotSet() |
|
72 | 9 | ->children() |
|
73 | 9 | ->booleanNode('record') |
|
74 | 9 | ->info('Set this to true to record timings (used on the Trends page)') |
|
75 | 9 | ->defaultFalse() |
|
76 | 9 | ->end() |
|
77 | 9 | ->floatNode('timezone_offset') |
|
78 | 9 | ->defaultValue(0) |
|
79 | 9 | ->info('Set this some integer offset from GMT in case your database is not storing things in the proper timezone and the dates look off on the Trends page') |
|
80 | 9 | ->max(24) |
|
81 | 9 | ->min(-24) |
|
82 | 9 | ->end() |
|
83 | 9 | ->end(); |
|
84 | |||
85 | 9 | return $rootNode; |
|
86 | } |
||
87 | |||
88 | 9 | View Code Duplication | protected function addOrm() |
|
|||
89 | { |
||
90 | 9 | $treeBuilder = new TreeBuilder(); |
|
91 | 9 | $rootNode = $treeBuilder->root('orm'); |
|
92 | $rootNode |
||
93 | 9 | ->addDefaultsIfNotSet() |
|
94 | 9 | ->children() |
|
95 | 9 | ->scalarNode('entity_manager') |
|
96 | 9 | ->info('This only needs to be set if orm is used for any of the managers, and you do not want to use the default entity manager') |
|
97 | 9 | ->defaultValue('default') |
|
98 | 9 | ->cannotBeEmpty() |
|
99 | 9 | ->end() |
|
100 | 9 | ->end(); |
|
101 | |||
102 | 9 | return $rootNode; |
|
103 | } |
||
104 | |||
105 | 9 | View Code Duplication | protected function addOdm() |
106 | { |
||
107 | 9 | $treeBuilder = new TreeBuilder(); |
|
108 | 9 | $rootNode = $treeBuilder->root('odm'); |
|
109 | $rootNode |
||
110 | 9 | ->addDefaultsIfNotSet() |
|
111 | 9 | ->children() |
|
112 | 9 | ->scalarNode('document_manager') |
|
113 | 9 | ->info('This only needs to be set if odm is used for any of the managers, and you do not want to use the default document manager') |
|
114 | 9 | ->defaultValue('default') |
|
115 | 9 | ->cannotBeEmpty() |
|
116 | 9 | ->end() |
|
117 | 9 | ->end(); |
|
118 | |||
119 | 9 | return $rootNode; |
|
120 | } |
||
121 | |||
122 | 9 | protected function addManager() |
|
123 | { |
||
124 | 9 | $treeBuilder = new TreeBuilder(); |
|
125 | 9 | $rootNode = $treeBuilder->root('manager'); |
|
126 | $rootNode |
||
127 | 9 | ->addDefaultsIfNotSet() |
|
128 | 9 | ->children() |
|
129 | 9 | ->scalarNode('job') |
|
130 | 9 | ->defaultValue('odm') |
|
131 | 9 | ->info('This can be [odm|orm|beanstalkd|rabbit_mq|redis|(your-own-custom-one)]') |
|
132 | 9 | ->cannotBeEmpty() |
|
133 | 9 | ->end() |
|
134 | 9 | ->scalarNode('run')->end() |
|
135 | 9 | ->scalarNode('job_timing')->end() |
|
136 | 9 | ->end(); |
|
137 | |||
138 | 9 | return $rootNode; |
|
139 | } |
||
140 | |||
141 | 9 | protected function addBeanstalkd() |
|
142 | { |
||
143 | 9 | $treeBuilder = new TreeBuilder(); |
|
144 | 9 | $rootNode = $treeBuilder->root('beanstalkd'); |
|
145 | $rootNode |
||
146 | 9 | ->children() |
|
147 | 9 | ->scalarNode('host')->end() |
|
148 | 9 | ->scalarNode('tube')->end() |
|
149 | 9 | ->end(); |
|
150 | |||
151 | 9 | return $rootNode; |
|
152 | } |
||
153 | |||
154 | 9 | protected function addRetry() |
|
155 | { |
||
156 | 9 | $treeBuilder = new TreeBuilder(); |
|
157 | 9 | $rootNode = $treeBuilder->root('retry'); |
|
158 | $rootNode |
||
159 | 9 | ->addDefaultsIfNotSet() |
|
160 | 9 | ->children() |
|
161 | 9 | ->arrayNode('max') |
|
162 | 9 | ->addDefaultsIfNotSet() |
|
163 | 9 | ->children() |
|
164 | 9 | ->integerNode('retries') |
|
165 | 9 | ->info('This the maximum total number of retries of any type.') |
|
166 | 9 | ->defaultValue(3) |
|
167 | 9 | ->end() |
|
168 | 9 | ->integerNode('failures') |
|
169 | 9 | ->info('This the maximum total number of failures before a job is marked as hitting the maximum failures.') |
|
170 | 9 | ->defaultValue(1) |
|
171 | 9 | ->end() |
|
172 | 9 | ->integerNode('exceptions') |
|
173 | 9 | ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.') |
|
174 | 9 | ->defaultValue(2) |
|
175 | 9 | ->end() |
|
176 | 9 | ->integerNode('stalls') |
|
177 | 9 | ->info('This the maximum total number of exceptions before a job is marked as hitting the maximum exceptions.') |
|
178 | 9 | ->defaultValue(2) |
|
179 | 9 | ->end() |
|
180 | 9 | ->end() |
|
181 | 9 | ->end() |
|
182 | 9 | ->arrayNode('auto') |
|
183 | 9 | ->addDefaultsIfNotSet() |
|
184 | 9 | ->children() |
|
185 | 9 | ->booleanNode('failure') |
|
186 | 9 | ->info('Instantly re-queue the job on failure.') |
|
187 | 9 | ->defaultTrue() |
|
188 | 9 | ->end() |
|
189 | 9 | ->booleanNode('exception') |
|
190 | 9 | ->info('Instantly re-queue the job on exception.') |
|
191 | 9 | ->defaultFalse() |
|
192 | 9 | ->end() |
|
193 | 9 | ->end() |
|
194 | 9 | ->end() |
|
195 | 9 | ->end(); |
|
196 | |||
197 | 9 | return $rootNode; |
|
198 | } |
||
199 | |||
200 | 9 | protected function addPriority() |
|
201 | { |
||
202 | 9 | $treeBuilder = new TreeBuilder(); |
|
203 | 9 | $rootNode = $treeBuilder->root('priority'); |
|
204 | $rootNode |
||
205 | 9 | ->addDefaultsIfNotSet() |
|
206 | 9 | ->children() |
|
207 | 9 | ->integerNode('max') |
|
208 | 9 | ->defaultValue(255) |
|
209 | 9 | ->info('Maximum priority value.') |
|
210 | 9 | ->min(1) |
|
211 | 9 | ->end() |
|
212 | 9 | ->enumNode('direction') |
|
213 | 9 | ->values([PriorityJobManager::PRIORITY_ASC, PriorityJobManager::PRIORITY_DESC]) |
|
214 | 9 | ->info('Whether 1 is high priority or low prioirty. '.PriorityJobManager::PRIORITY_ASC.' means 1 is low, '.PriorityJobManager::PRIORITY_DESC.' means 1 is high.') |
|
215 | 9 | ->defaultValue(PriorityJobManager::PRIORITY_DESC) |
|
216 | 9 | ->end() |
|
217 | 9 | ->end(); |
|
218 | |||
219 | 9 | return $rootNode; |
|
220 | } |
||
221 | |||
222 | 9 | protected function addClasses() |
|
223 | { |
||
224 | 9 | $treeBuilder = new TreeBuilder(); |
|
225 | 9 | $rootNode = $treeBuilder->root('class'); |
|
226 | $rootNode |
||
227 | 9 | ->children() |
|
228 | 9 | ->scalarNode('job') |
|
229 | 9 | ->info('If you want to override the Job class, put the class name here.')->end() |
|
230 | 9 | ->scalarNode('job_archive') |
|
231 | 9 | ->info('If you want to override the JobArchive class, put the class name here.')->end() |
|
232 | 9 | ->scalarNode('job_timing') |
|
233 | 9 | ->info('If you want to override the JobTiming class, put the class name here.')->end() |
|
234 | 9 | ->scalarNode('run') |
|
235 | 9 | ->info('If you want to override the Run class, put the class name here.')->end() |
|
236 | 9 | ->scalarNode('run_archive') |
|
237 | 9 | ->info('If you want to override the RunArchive class, put the class name here.')->end() |
|
238 | 9 | ->end(); |
|
239 | |||
240 | 9 | return $rootNode; |
|
241 | } |
||
242 | |||
243 | 9 | View Code Duplication | protected function addAdmin() |
244 | { |
||
245 | 9 | $treeBuilder = new TreeBuilder(); |
|
246 | 9 | $rootNode = $treeBuilder->root('admin'); |
|
247 | $rootNode |
||
248 | 9 | ->addDefaultsIfNotSet() |
|
249 | 9 | ->children() |
|
250 | 9 | ->scalarNode('chartjs') |
|
251 | 9 | ->defaultValue('https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.bundle.min.js') |
|
252 | 9 | ->info('This can be changed to say a locally hosted path or url.')->end() |
|
253 | 9 | ->end() |
|
254 | 9 | ->end(); |
|
255 | |||
256 | 9 | return $rootNode; |
|
257 | } |
||
258 | |||
259 | 9 | protected function addSncRedis() |
|
260 | { |
||
261 | 9 | $treeBuilder = new TreeBuilder(); |
|
262 | 9 | $rootNode = $treeBuilder->root('snc_redis'); |
|
263 | $rootNode |
||
264 | 9 | ->children() |
|
265 | 9 | ->enumNode('type') |
|
266 | 9 | ->values(['predis', 'phpredis']) |
|
267 | 9 | ->defaultNull()->end() |
|
268 | 9 | ->scalarNode('alias') |
|
269 | 9 | ->defaultNull()->end() |
|
270 | 9 | ->end() |
|
271 | 9 | View Code Duplication | ->validate()->ifTrue(function ($node) { |
272 | 1 | if (isset($node['type']) && !isset($node['alias'])) { |
|
273 | 1 | return true; |
|
274 | } |
||
275 | 1 | if (isset($node['alias']) && !isset($node['type'])) { |
|
276 | 1 | return true; |
|
277 | } |
||
278 | |||
279 | 1 | return false; |
|
280 | 9 | })->thenInvalid('if alias or type is set, then both must be set')->end(); |
|
281 | |||
282 | 9 | return $rootNode; |
|
283 | } |
||
284 | |||
285 | 9 | protected function addPredis() |
|
286 | { |
||
287 | 9 | $treeBuilder = new TreeBuilder(); |
|
288 | 9 | $rootNode = $treeBuilder->root('predis'); |
|
289 | $rootNode |
||
290 | 9 | ->children() |
|
291 | 9 | ->scalarNode('dsn')->defaultNull()->end() |
|
292 | 9 | ->append($this->addPredisArgs()) |
|
293 | 9 | ->end() |
|
294 | 9 | ->validate()->ifTrue(function ($node) { |
|
295 | 1 | if (isset($node['dsn']) && (isset($node['connection_parameters']['host']) || isset($node['connection_parameters']['port']))) { |
|
296 | return true; |
||
297 | } |
||
298 | |||
299 | 1 | return false; |
|
300 | 9 | })->thenInvalid('if dsn is set, do not use connection_parameters for predis (and vice-versa)')->end(); |
|
301 | |||
302 | 9 | return $rootNode; |
|
303 | } |
||
304 | |||
305 | 4 | protected function checkPredis(array $node) |
|
306 | { |
||
307 | 4 | if ((isset($node['predis']['dsn']) || isset($node['predis']['connection_parameters']['host'])) && |
|
308 | 4 | (isset($node['snc_redis']['type']) || isset($node['phpredis']['host']))) { |
|
309 | return true; |
||
310 | } |
||
311 | |||
312 | 4 | return false; |
|
313 | } |
||
314 | |||
315 | 4 | protected function checkSncPhpRedis(array $node) |
|
316 | { |
||
317 | 4 | if (isset($node['snc_redis']['type']) && |
|
318 | 4 | isset($node['phpredis']['host'])) { |
|
319 | return true; |
||
320 | } |
||
321 | |||
322 | 4 | return false; |
|
323 | } |
||
324 | |||
325 | 9 | protected function addRedis() |
|
326 | { |
||
327 | 9 | $treeBuilder = new TreeBuilder(); |
|
328 | 9 | $rootNode = $treeBuilder->root('redis'); |
|
329 | $rootNode |
||
330 | 9 | ->addDefaultsIfNotSet() |
|
331 | 9 | ->children() |
|
332 | 9 | ->scalarNode('prefix')->defaultValue('dtc_queue_')->end() |
|
333 | 9 | ->append($this->addSncRedis()) |
|
334 | 9 | ->append($this->addPredis()) |
|
335 | 9 | ->append($this->addPhpRedisArgs()) |
|
336 | 9 | ->end() |
|
337 | 9 | ->validate()->ifTrue(function ($node) { |
|
338 | 4 | if ($this->checkPredis($node)) { |
|
339 | return true; |
||
340 | } |
||
341 | 4 | if ($this->checkSncPhpRedis($node)) { |
|
342 | return true; |
||
343 | } |
||
344 | |||
345 | 4 | return false; |
|
346 | 9 | })->thenInvalid('only one of [snc_redis | predis | phpredis] should be set')->end(); |
|
347 | |||
348 | 9 | return $rootNode; |
|
349 | } |
||
350 | |||
351 | 9 | protected function addPhpRedisArgs() |
|
352 | { |
||
353 | 9 | $treeBuilder = new TreeBuilder(); |
|
354 | 9 | $rootNode = $treeBuilder->root('phpredis'); |
|
355 | $rootNode |
||
356 | 9 | ->addDefaultsIfNotSet() |
|
357 | 9 | ->children() |
|
358 | 9 | ->scalarNode('host')->end() |
|
359 | 9 | ->integerNode('port')->defaultValue(6379)->end() |
|
360 | 9 | ->floatNode('timeout')->defaultValue(0)->end() |
|
361 | 9 | ->integerNode('retry_interval')->defaultNull()->end() |
|
362 | 9 | ->floatNode('read_timeout')->defaultValue(0)->end() |
|
363 | 9 | ->scalarNode('auth')->end() |
|
364 | 9 | ->end() |
|
365 | 9 | ->validate()->ifTrue(function ($node) { |
|
366 | 2 | if (!empty($node) && !isset($node['host'])) { |
|
367 | return true; |
||
368 | } |
||
369 | |||
370 | 2 | return false; |
|
371 | 9 | })->thenInvalid('phpredis host should be set')->end(); |
|
372 | |||
373 | 9 | return $rootNode; |
|
374 | } |
||
375 | |||
376 | 9 | protected function addPredisArgs() |
|
377 | { |
||
378 | 9 | $treeBuilder = new TreeBuilder(); |
|
379 | 9 | $rootNode = $treeBuilder->root('connection_parameters'); |
|
380 | $rootNode |
||
381 | 9 | ->addDefaultsIfNotSet() |
|
382 | 9 | ->children() |
|
383 | 9 | ->scalarNode('scheme')->defaultValue('tcp')->end() |
|
384 | 9 | ->scalarNode('host')->defaultNull()->end() |
|
385 | 9 | ->integerNode('port')->defaultNull()->end() |
|
386 | 9 | ->scalarNode('path')->defaultNull()->end() |
|
387 | 9 | ->scalarNode('database')->defaultNull()->end() |
|
388 | 9 | ->scalarNode('password')->defaultNull()->end() |
|
389 | 9 | ->booleanNode('async')->defaultFalse()->end() |
|
390 | 9 | ->booleanNode('persistent')->defaultFalse()->end() |
|
391 | 9 | ->floatNode('timeout')->defaultValue(5.0)->end() |
|
392 | 9 | ->floatNode('read_write_timeout')->defaultNull()->end() |
|
393 | 9 | ->scalarNode('alias')->defaultNull()->end() |
|
394 | 9 | ->integerNode('weight')->defaultNull()->end() |
|
395 | 9 | ->booleanNode('iterable_multibulk')->defaultFalse()->end() |
|
396 | 9 | ->booleanNode('throw_errors')->defaultTrue()->end() |
|
397 | 9 | ->end() |
|
398 | 9 | View Code Duplication | ->validate()->ifTrue(function ($node) { |
399 | 1 | if (isset($node['host']) && !isset($node['port'])) { |
|
400 | return true; |
||
401 | } |
||
402 | 1 | if (isset($node['port']) && !isset($node['host'])) { |
|
403 | return true; |
||
404 | } |
||
405 | |||
406 | 1 | return false; |
|
407 | 9 | })->thenInvalid('preids connection_parameters host and port should both be set')->end(); |
|
408 | |||
409 | 9 | return $rootNode; |
|
410 | } |
||
411 | |||
412 | 9 | protected function addRabbitMqOptions() |
|
430 | |||
431 | 9 | protected function addRabbitMqSslOptions() |
|
463 | |||
464 | 9 | View Code Duplication | protected function addRabbitMqExchange() |
480 | |||
481 | 9 | View Code Duplication | protected function addRabbitMqArgs() |
497 | |||
498 | 9 | protected function addRabbitMq() |
|
536 | } |
||
537 |
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.