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 |
||
| 37 | class BazingaGeocoderExtension extends Extension |
||
| 38 | { |
||
| 39 | 28 | public function load(array $configs, ContainerBuilder $container) |
|
| 61 | |||
| 62 | 28 | private function loadProviders(ContainerBuilder $container, array $config) |
|
| 63 | { |
||
| 64 | 28 | foreach ($config['providers'] as $providerName => $providerConfig) { |
|
| 65 | try { |
||
| 66 | 26 | $factoryService = $container->getDefinition($providerConfig['factory']); |
|
| 67 | 26 | $factoryClass = $factoryService->getClass() ?: $providerConfig['factory']; |
|
| 68 | 26 | if (!$this->implementsPoviderFactory($factoryClass)) { |
|
| 69 | throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory'])); |
||
| 70 | } |
||
| 71 | // See if any option has a service reference |
||
| 72 | 26 | $providerConfig['options'] = $this->findReferences($providerConfig['options']); |
|
| 73 | 26 | $factoryClass::validate($providerConfig['options'], $providerName); |
|
| 74 | } catch (ServiceNotFoundException $e) { |
||
| 75 | // Assert: We are using a custom factory. If invalid config, it will be caught in FactoryValidatorPass |
||
| 76 | $providerConfig['options'] = $this->findReferences($providerConfig['options']); |
||
| 77 | FactoryValidatorPass::addFactoryServiceId($providerConfig['factory']); |
||
| 78 | } |
||
| 79 | |||
| 80 | 26 | $serviceId = 'bazinga_geocoder.provider.'.$providerName; |
|
| 81 | 26 | $plugins = $this->configureProviderPlugins($container, $providerConfig, $serviceId); |
|
| 82 | |||
| 83 | 26 | $def = $container->register($serviceId, PluginProvider::class) |
|
| 84 | 26 | ->setFactory([PluginProviderFactory::class, 'createPluginProvider']) |
|
| 85 | 26 | ->addArgument($plugins) |
|
| 86 | 26 | ->addArgument(new Reference($providerConfig['factory'])) |
|
| 87 | 26 | ->addArgument($providerConfig['options']); |
|
| 88 | |||
| 89 | 26 | $def->addTag('bazinga_geocoder.provider'); |
|
| 90 | 26 | foreach ($providerConfig['aliases'] as $alias) { |
|
| 91 | $container->setAlias($alias, $serviceId); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | 28 | } |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Configure plugins for a client. |
||
| 98 | * |
||
| 99 | * @param ContainerBuilder $container |
||
| 100 | * @param array $config |
||
| 101 | * @param string $providerServiceId |
||
| 102 | * |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 26 | public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array |
|
| 106 | { |
||
| 107 | 26 | $plugins = []; |
|
| 108 | 26 | foreach ($config['plugins'] as $plugin) { |
|
| 109 | 2 | if ($plugin['reference']['enabled']) { |
|
| 110 | 2 | $plugins[] = $plugin['reference']['id']; |
|
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | 26 | if (isset($config['cache']) || isset($config['cache_lifetime']) || isset($config['cache_precision'])) { |
|
| 115 | 1 | if (null === $cacheServiceId = $config['cache']) { |
|
| 116 | if (!$container->has('app.cache')) { |
||
| 117 | throw new \LogicException('You need to specify a service for cache.'); |
||
| 118 | } |
||
| 119 | $cacheServiceId = 'app.cache'; |
||
| 120 | } |
||
| 121 | 1 | $plugins[] = $providerServiceId.'.cache'; |
|
| 122 | 1 | $container->register($providerServiceId.'.cache', CachePlugin::class) |
|
| 123 | 1 | ->setPublic(false) |
|
| 124 | 1 | ->setArguments([new Reference($cacheServiceId), (int) $config['cache_lifetime'], $config['cache_precision']]); |
|
| 125 | } |
||
| 126 | |||
| 127 | 26 | View Code Duplication | if (isset($config['limit'])) { |
| 128 | $plugins[] = $providerServiceId.'.limit'; |
||
| 129 | $container->register($providerServiceId.'.limit', LimitPlugin::class) |
||
| 130 | ->setPublic(false) |
||
| 131 | ->setArguments([(int) $config['limit']]); |
||
| 132 | } |
||
| 133 | |||
| 134 | 26 | View Code Duplication | if (isset($config['locale'])) { |
| 135 | $plugins[] = $providerServiceId.'.locale'; |
||
| 136 | $container->register($providerServiceId.'.locale', LocalePlugin::class) |
||
| 137 | ->setPublic(false) |
||
| 138 | ->setArguments([$config['locale']]); |
||
| 139 | } |
||
| 140 | |||
| 141 | 26 | View Code Duplication | if (isset($config['logger'])) { |
| 142 | $plugins[] = $providerServiceId.'.logger'; |
||
| 143 | $container->register($providerServiceId.'.logger', LoggerPlugin::class) |
||
| 144 | ->setPublic(false) |
||
| 145 | ->setArguments([new Reference($config['logger'])]); |
||
| 146 | } |
||
| 147 | |||
| 148 | 26 | if ($container->has(FakeIpPlugin::class)) { |
|
| 149 | $plugins[] = FakeIpPlugin::class; |
||
| 150 | } |
||
| 151 | |||
| 152 | 26 | if ($container->has(GeocoderDataCollector::class)) { |
|
| 153 | 1 | $plugins[] = $providerServiceId.'.profiler'; |
|
| 154 | 1 | $container->register($providerServiceId.'.profiler', ProfilingPlugin::class) |
|
| 155 | 1 | ->setPublic(false) |
|
| 156 | 1 | ->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))]) |
|
| 157 | 1 | ->addTag('bazinga_geocoder.profiling_plugin'); |
|
| 158 | } |
||
| 159 | |||
| 160 | return array_map(function (string $id) { |
||
| 161 | 3 | return new Reference($id); |
|
| 162 | 26 | }, $plugins); |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritdoc} |
||
| 167 | */ |
||
| 168 | 28 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $options |
||
| 175 | * |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | 26 | private function findReferences(array $options): array |
|
| 179 | { |
||
| 180 | 26 | foreach ($options as $key => $value) { |
|
| 181 | 19 | if (is_array($value)) { |
|
| 182 | 1 | $options[$key] = $this->findReferences($value); |
|
| 183 | 19 | } elseif ('_service' === substr((string) $key, -8) || 0 === strpos((string) $value, '@') || 'service' === $key) { |
|
| 184 | $options[$key] = new Reference(ltrim($value, '@')); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | 26 | return $options; |
|
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param mixed $factoryClass |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 26 | private function implementsPoviderFactory($factoryClass): bool |
|
| 204 | } |
||
| 205 |
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: