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 | class ZenstruckCacheExtension extends ConfigurableExtension |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | 22 | protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
|
36 | |||
37 | /** |
||
38 | * @param string|null $httpClient |
||
39 | * @param ContainerBuilder $container |
||
40 | */ |
||
41 | 22 | View Code Duplication | private function configureHttpClient($httpClient, ContainerBuilder $container) |
|
|||
42 | { |
||
43 | 22 | $httpClient = $httpClient ?: $this->autoDiscoverHttpClient(); |
|
44 | |||
45 | 22 | if (!class_exists($httpClient)) { |
|
46 | // is a service |
||
47 | 12 | $container->setAlias('zenstruck_cache.http_client', $httpClient); |
|
48 | |||
49 | 12 | return; |
|
50 | } |
||
51 | |||
52 | 10 | $r = new \ReflectionClass($httpClient); |
|
53 | |||
54 | 10 | if (!$r->implementsInterface('Http\Client\HttpClient')) { |
|
55 | 2 | throw new InvalidConfigurationException('HttpClient class must implement "Http\Client\HttpClient".'); |
|
56 | } |
||
57 | |||
58 | 8 | if ($r->isAbstract()) { |
|
59 | 2 | throw new InvalidConfigurationException('HttpClient class must not be abstract.'); |
|
60 | } |
||
61 | |||
62 | 6 | if (null !== $r->getConstructor() && 0 !== $r->getConstructor()->getNumberOfRequiredParameters()) { |
|
63 | 2 | throw new InvalidConfigurationException('HttpClient class must not have required constructor arguments.'); |
|
64 | } |
||
65 | |||
66 | 4 | $httpClient = new Definition($httpClient); |
|
67 | 4 | $httpClient->setPublic(false); |
|
68 | 4 | $container->setDefinition('zenstruck_cache.http_client', $httpClient); |
|
69 | 4 | } |
|
70 | |||
71 | /** |
||
72 | * @param string|null $messageFactory |
||
73 | * @param ContainerBuilder $container |
||
74 | */ |
||
75 | 16 | View Code Duplication | private function configureMessageFactory($messageFactory, ContainerBuilder $container) |
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 2 | private function autoDiscoverHttpClient() |
|
109 | { |
||
110 | try { |
||
111 | 2 | return get_class(HttpClientDiscovery::find()); |
|
112 | } catch (NotFoundException $e) { |
||
113 | throw new InvalidConfigurationException('A HttpClient was not found, please define one in your configuration.', 0, $e); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 2 | private function autoDiscoverMessageFactory() |
|
128 | } |
||
129 |
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.