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 |
||
| 44 | class FileResolverFactory implements FileResolverFactoryInterface |
||
| 45 | { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The DI container instance. |
||
| 49 | * |
||
| 50 | * @var \Symfony\Component\DependencyInjection\ContainerInterface |
||
| 51 | */ |
||
| 52 | private $container; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The .OK file handler factory instance |
||
| 56 | * |
||
| 57 | * @var \TechDivision\Import\Handlers\HandlerFactoryInterface |
||
| 58 | */ |
||
| 59 | private $handlerFactory; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The registry processor instance. |
||
| 63 | * |
||
| 64 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 65 | */ |
||
| 66 | private $registryProcessor; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initialize the factory with the DI container instance. |
||
| 70 | * |
||
| 71 | * @param \Symfony\Component\DependencyInjection\ContainerInterface $container The DI container instance |
||
| 72 | * @param \TechDivision\Import\Handlers\HandlerFactoryInterface $handlerFactory The handler factory instance |
||
| 73 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 74 | */ |
||
| 75 | public function __construct( |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Return's the container instance. |
||
| 87 | * |
||
| 88 | * @return \Symfony\Component\DependencyInjection\ContainerInterface The container instance |
||
| 89 | */ |
||
| 90 | protected function getContainer() : ContainerInterface |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Return's the registry processor instance. |
||
| 97 | * |
||
| 98 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
| 99 | */ |
||
| 100 | protected function getRegistryProcessor() : RegistryProcessorInterface |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Return's the .OK file handler factory instance. |
||
| 107 | * |
||
| 108 | * @return \TechDivision\Import\Handlers\HandlerFactoryInterface The .OK file handler factory instance |
||
| 109 | */ |
||
| 110 | protected function getHandlerFactory() : HandlerFactoryInterface |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Return's the actual source directory. |
||
| 117 | * |
||
| 118 | * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter to validate the source directory with |
||
| 119 | * |
||
| 120 | * @return string The actual source directory |
||
| 121 | * @throws \Exception Is thrown, if the actual source directory can not be loaded |
||
| 122 | */ |
||
| 123 | View Code Duplication | protected function getSourceDir(FilesystemAdapterInterface $filesystemAdapter) : string |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Creates and returns the file resolver instance for the subject with the passed configuration. |
||
| 140 | * |
||
| 141 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subject The subject to create the file resolver for |
||
| 142 | * |
||
| 143 | * @return \TechDivision\Import\Subjects\FileResolver\FileResolverInterface The file resolver instance |
||
| 144 | */ |
||
| 145 | public function createFileResolver(SubjectConfigurationInterface $subject) : FileResolverInterface |
||
| 183 | } |
||
| 184 |
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.