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 SpomkyLabsJoseBundleExtension 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 SpomkyLabsJoseBundleExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class SpomkyLabsJoseBundleExtension extends Extension |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] |
||
| 26 | */ |
||
| 27 | private $jwk_sources; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] |
||
| 31 | */ |
||
| 32 | private $jwk_set_sources; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $alias; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $alias |
||
| 41 | */ |
||
| 42 | public function __construct($alias) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | public function load(array $configs, ContainerBuilder $container) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function getConfiguration(array $configs, ContainerBuilder $container) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function getAlias() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 89 | * @param array $config |
||
| 90 | */ |
||
| 91 | private function initConfiguration(ContainerBuilder $container, array $config) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $name |
||
| 132 | * @param array $config |
||
| 133 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 134 | * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSetSource\JWKSetSourceInterface[] $jwk_set_sources |
||
| 135 | */ |
||
| 136 | View Code Duplication | private function createJWKSet($name, array $config, ContainerBuilder $container, array $jwk_set_sources) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $name |
||
| 151 | * @param array $config |
||
| 152 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 153 | * @param \SpomkyLabs\JoseBundle\DependencyInjection\JWKSource\JWKSourceInterface[] $jwk_sources |
||
| 154 | */ |
||
| 155 | View Code Duplication | private function createJWK($name, array $config, ContainerBuilder $container, array $jwk_sources) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $name |
||
| 170 | * @param array $config |
||
| 171 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 172 | */ |
||
| 173 | private function createEncrypter($name, array $config, ContainerBuilder $container) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $name |
||
| 197 | * @param array $config |
||
| 198 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 199 | */ |
||
| 200 | private function createDecrypter($name, array $config, ContainerBuilder $container) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $name |
||
| 220 | * @param array $config |
||
| 221 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 222 | */ |
||
| 223 | private function createSigner($name, array $config, ContainerBuilder $container) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $name |
||
| 245 | * @param array $config |
||
| 246 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 247 | */ |
||
| 248 | View Code Duplication | private function createVerifier($name, array $config, ContainerBuilder $container) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param string $name |
||
| 266 | * @param array $config |
||
| 267 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 268 | */ |
||
| 269 | View Code Duplication | private function createChecker($name, array $config, ContainerBuilder $container) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $name |
||
| 287 | * @param array $config |
||
| 288 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 289 | */ |
||
| 290 | private function createJWTLoader($name, array $config, ContainerBuilder $container) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $name |
||
| 310 | * @param array $config |
||
| 311 | * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
||
| 312 | */ |
||
| 313 | View Code Duplication | private function createJWTCreator($name, array $config, ContainerBuilder $container) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @return string[] |
||
| 331 | */ |
||
| 332 | private function getXmlFileToLoad() |
||
| 345 | |||
| 346 | View Code Duplication | private function createJWKSources() |
|
| 366 | |||
| 367 | View Code Duplication | private function createJWKSetSources() |
|
| 387 | } |
||
| 388 |
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.