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 |
||
| 35 | class BazingaGeocoderExtension extends Extension |
||
| 36 | { |
||
| 37 | 23 | public function load(array $configs, ContainerBuilder $container) |
|
| 59 | |||
| 60 | 23 | private function loadProviders(ContainerBuilder $container, array $config) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Configure plugins for a client. |
||
| 91 | * |
||
| 92 | * @param ContainerBuilder $container |
||
| 93 | * @param array $config |
||
| 94 | * @param string $providerServiceId |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 22 | public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array |
|
| 99 | { |
||
| 100 | 22 | $plugins = []; |
|
| 101 | 22 | foreach ($config['plugins'] as $plugin) { |
|
| 102 | $plugins[] = $plugin['id']; |
||
| 103 | } |
||
| 104 | |||
| 105 | 22 | if (isset($config['cache']) || isset($config['cache_lifetime'])) { |
|
| 106 | 1 | if (null === $cacheServiceId = $config['cache']) { |
|
| 107 | if (!$container->has('app.cache')) { |
||
| 108 | throw new \LogicException('You need to specify a service for cache.'); |
||
| 109 | } |
||
| 110 | $cacheServiceId = 'app.cache'; |
||
| 111 | } |
||
| 112 | 1 | $plugins[] = $providerServiceId.'.cache'; |
|
| 113 | 1 | $container->register($providerServiceId.'.cache', CachePlugin::class) |
|
| 114 | 1 | ->setPublic(false) |
|
| 115 | 1 | ->setArguments([new Reference($cacheServiceId), (int) $config['cache_lifetime']]); |
|
| 116 | } |
||
| 117 | |||
| 118 | 22 | View Code Duplication | if (isset($config['limit'])) { |
| 119 | $plugins[] = $providerServiceId.'.limit'; |
||
| 120 | $container->register($providerServiceId.'.limit', LimitPlugin::class) |
||
| 121 | ->setPublic(false) |
||
| 122 | ->setArguments([(int) $config['limit']]); |
||
| 123 | } |
||
| 124 | |||
| 125 | 22 | View Code Duplication | if (isset($config['locale'])) { |
| 126 | $plugins[] = $providerServiceId.'.locale'; |
||
| 127 | $container->register($providerServiceId.'.locale', LocalePlugin::class) |
||
| 128 | ->setPublic(false) |
||
| 129 | ->setArguments([$config['locale']]); |
||
| 130 | } |
||
| 131 | |||
| 132 | 22 | View Code Duplication | if (isset($config['logger'])) { |
| 133 | $plugins[] = $providerServiceId.'.logger'; |
||
| 134 | $container->register($providerServiceId.'.logger', LoggerPlugin::class) |
||
| 135 | ->setPublic(false) |
||
| 136 | ->setArguments([new Reference($config['logger'])]); |
||
| 137 | } |
||
| 138 | |||
| 139 | 22 | if ($container->has(FakeIpPlugin::class)) { |
|
| 140 | $plugins[] = FakeIpPlugin::class; |
||
| 141 | } |
||
| 142 | |||
| 143 | 22 | if ($container->has(GeocoderDataCollector::class)) { |
|
| 144 | $plugins[] = $providerServiceId.'.profiler'; |
||
| 145 | $container->register($providerServiceId.'.profiler', ProfilingPlugin::class) |
||
| 146 | ->setPublic(false) |
||
| 147 | ->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))]) |
||
| 148 | ->addTag('bazinga_geocoder.profiling_plugin'); |
||
| 149 | } |
||
| 150 | |||
| 151 | 22 | return array_map(function (string $id) { |
|
| 152 | 1 | return new Reference($id); |
|
| 153 | 22 | }, $plugins); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | 23 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $options |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 22 | private function findReferences(array $options): array |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @param mixed $factoryClass |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 22 | private function implementsPoviderFactory($factoryClass): bool |
|
| 195 | } |
||
| 196 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: