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 | abstract class AbstractFileResolver implements FileResolverInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The registry processor instance. |
||
| 42 | * |
||
| 43 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 44 | */ |
||
| 45 | private $registryProcessor; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The appliation instance. |
||
| 49 | * |
||
| 50 | * @var \TechDivision\Import\ApplicationInterface |
||
| 51 | */ |
||
| 52 | private $application; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The actual source directory to load the files from. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $sourceDir; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The OK file suffix to use. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $okFileSuffix = 'ok'; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The subject configuraiton instance. |
||
| 70 | * |
||
| 71 | * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
||
| 72 | */ |
||
| 73 | private $subjectConfiguration; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initializes the file resolver with the application and the registry instance. |
||
| 77 | * |
||
| 78 | * @param \TechDivision\Import\ApplicationInterface $application The application instance |
||
| 79 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance |
||
| 80 | */ |
||
| 81 | 3 | public function __construct(ApplicationInterface $application, RegistryProcessorInterface $registryProcessor) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Returns the registry processor instance. |
||
| 89 | * |
||
| 90 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance |
||
| 91 | */ |
||
| 92 | protected function getRegistryProcessor() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return's the application instance. |
||
| 99 | * |
||
| 100 | * @return \TechDivision\Import\ApplicationInterface The application instance |
||
| 101 | */ |
||
| 102 | protected function getApplication() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets the actual source directory to load the files from. |
||
| 109 | * |
||
| 110 | * @param string $sourceDir The actual source directory |
||
| 111 | * |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | protected function setSourceDir($sourceDir) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns the actual source directory to load the files from. |
||
| 121 | * |
||
| 122 | * @return string The actual source directory |
||
| 123 | */ |
||
| 124 | protected function getSourceDir() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the file resolver configuration instance. |
||
| 131 | * |
||
| 132 | * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
||
| 133 | */ |
||
| 134 | 3 | protected function getFileResolverConfiguration() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the delement separator char. |
||
| 141 | * |
||
| 142 | * @return string The element separator char |
||
| 143 | */ |
||
| 144 | 3 | protected function getElementSeparator() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the elements the filenames consists of. |
||
| 151 | * |
||
| 152 | * @return array The array with the filename elements |
||
| 153 | */ |
||
| 154 | 3 | protected function getPatternElements() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the suffix for the import files. |
||
| 161 | * |
||
| 162 | * @return string The suffix |
||
| 163 | */ |
||
| 164 | 3 | protected function getSuffix() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Returns the OK file suffix to use. |
||
| 171 | * |
||
| 172 | * @return string The OK file suffix |
||
| 173 | */ |
||
| 174 | 1 | protected function getOkFileSuffix() |
|
| 175 | { |
||
| 176 | 1 | return $this->getFileResolverConfiguration()->getOkFileSuffix(); |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Initializes the file resolver for the import process with the passed serial. |
||
| 181 | * |
||
| 182 | * @param string $serial The unique identifier of the actual import process |
||
| 183 | * |
||
| 184 | * @return void |
||
| 185 | * @throws \Exception Is thrown if the configured source directory is not available |
||
| 186 | */ |
||
| 187 | View Code Duplication | protected function initialize($serial) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * Sets the subject configuration instance. |
||
| 204 | * |
||
| 205 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
||
| 206 | * |
||
| 207 | * @return void |
||
| 208 | */ |
||
| 209 | 3 | public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Returns the subject configuration instance. |
||
| 216 | * |
||
| 217 | * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
||
| 218 | */ |
||
| 219 | 3 | public function getSubjectConfiguration() |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Loads the files from the source directory and return's them sorted. |
||
| 226 | * |
||
| 227 | * @param string $serial The unique identifier of the actual import process |
||
| 228 | * |
||
| 229 | * @return array The array with the files matching the subjects suffix |
||
| 230 | * @throws \Exception Is thrown, when the source directory is NOT available |
||
| 231 | */ |
||
| 232 | public function loadFiles($serial) |
||
| 252 | } |
||
| 253 |
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.