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 |
||
| 11 | class AwsS3ResolverFactory implements ResolverFactoryInterface |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * {@inheritdoc} |
||
| 15 | */ |
||
| 16 | public function create(ContainerBuilder $container, $resolverName, array $config) |
||
| 17 | { |
||
| 18 | $awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client'; |
||
| 19 | $awsS3ClientDefinition = new Definition('Aws\S3\S3Client'); |
||
| 20 | if (method_exists($awsS3ClientDefinition, 'setFactory')) { |
||
| 21 | $awsS3ClientDefinition->setFactory(array('Aws\S3\S3Client', 'factory')); |
||
| 22 | } else { |
||
| 23 | // to be removed when dependency on Symfony DependencyInjection is bumped to 2.6 |
||
| 24 | $awsS3ClientDefinition->setFactoryClass('Aws\S3\S3Client'); |
||
|
|
|||
| 25 | $awsS3ClientDefinition->setFactoryMethod('factory'); |
||
| 26 | } |
||
| 27 | $awsS3ClientDefinition->addArgument($config['client_config']); |
||
| 28 | $container->setDefinition($awsS3ClientId, $awsS3ClientDefinition); |
||
| 29 | |||
| 30 | $resolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.aws_s3'); |
||
| 31 | $resolverDefinition->replaceArgument(0, new Reference($awsS3ClientId)); |
||
| 32 | $resolverDefinition->replaceArgument(1, $config['bucket']); |
||
| 33 | $resolverDefinition->replaceArgument(2, $config['acl']); |
||
| 34 | $resolverDefinition->replaceArgument(3, array_replace($config['url_options'], $config['get_options'])); |
||
| 35 | $resolverDefinition->replaceArgument(4, $config['put_options']); |
||
| 36 | $resolverId = 'liip_imagine.cache.resolver.'.$resolverName; |
||
| 37 | $container->setDefinition($resolverId, $resolverDefinition); |
||
| 38 | |||
| 39 | if (isset($config['cache_prefix'])) { |
||
| 40 | $resolverDefinition->addMethodCall('setCachePrefix', array($config['cache_prefix'])); |
||
| 41 | } |
||
| 42 | |||
| 43 | View Code Duplication | if ($config['proxies']) { |
|
| 44 | $proxiedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.proxied'; |
||
| 45 | |||
| 46 | $container->setDefinition($proxiedResolverId, $resolverDefinition); |
||
| 47 | |||
| 48 | $proxyResolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.proxy'); |
||
| 49 | $proxyResolverDefinition->replaceArgument(0, new Reference($proxiedResolverId)); |
||
| 50 | $proxyResolverDefinition->replaceArgument(1, $config['proxies']); |
||
| 51 | |||
| 52 | $container->setDefinition($resolverId, $proxyResolverDefinition); |
||
| 53 | } |
||
| 54 | |||
| 55 | View Code Duplication | if ($config['cache']) { |
|
| 56 | $cachedResolverId = 'liip_imagine.cache.resolver.'.$resolverName.'.cached'; |
||
| 57 | |||
| 58 | $container->setDefinition($cachedResolverId, $container->getDefinition($resolverId)); |
||
| 59 | |||
| 60 | $cacheResolverDefinition = new DefinitionDecorator('liip_imagine.cache.resolver.prototype.cache'); |
||
| 61 | $cacheResolverDefinition->replaceArgument(0, new Reference($config['cache'])); |
||
| 62 | $cacheResolverDefinition->replaceArgument(1, new Reference($cachedResolverId)); |
||
| 63 | |||
| 64 | $container->setDefinition($resolverId, $cacheResolverDefinition); |
||
| 65 | } |
||
| 66 | |||
| 67 | $container->getDefinition($resolverId)->addTag('liip_imagine.cache.resolver', array( |
||
| 68 | 'resolver' => $resolverName, |
||
| 69 | )); |
||
| 70 | |||
| 71 | return $resolverId; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function getName() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function addConfiguration(ArrayNodeDefinition $builder) |
||
| 120 | } |
||
| 121 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.