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 |
||
| 39 | abstract class AbstractFileResolver implements FileResolverInterface |
||
| 40 | { |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The registry processor instance. |
||
| 44 | * |
||
| 45 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 46 | */ |
||
| 47 | private $registryProcessor; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * The actual source directory to load the files from. |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $sourceDir; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The subject configuraiton instance. |
||
| 58 | * |
||
| 59 | * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
||
| 60 | */ |
||
| 61 | private $subjectConfiguration; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The filesystem adapter instance. |
||
| 65 | * |
||
| 66 | * @var \TechDivision\Import\Adapter\PhpFilesystemAdapterInterface |
||
| 67 | */ |
||
| 68 | private $filesystemAdapter; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The filesystem loader instance. |
||
| 72 | * |
||
| 73 | * @var \TechDivision\Import\Loaders\PregMatchFilteredLoaderInterface |
||
| 74 | */ |
||
| 75 | private $filesystemLoader; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Initializes the file resolver with the application and the registry instance. |
||
| 79 | * |
||
| 80 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance |
||
| 81 | */ |
||
| 82 | public function __construct(RegistryProcessorInterface $registryProcessor) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Return's the registry processor instance. |
||
| 89 | * |
||
| 90 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The processor instance |
||
| 91 | */ |
||
| 92 | protected function getRegistryProcessor() : RegistryProcessorInterface |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Return's the filesystem loader instance. |
||
| 99 | * |
||
| 100 | * @return \TechDivision\Import\Loaders\PregMatchFilteredLoaderInterface The loader instance |
||
| 101 | */ |
||
| 102 | protected function getFilesystemLoader() : PregMatchFilteredLoaderInterface |
||
| 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(string $sourceDir) : void |
||
| 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() : string |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns the file resolver configuration instance. |
||
| 131 | * |
||
| 132 | * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
||
| 133 | */ |
||
| 134 | protected function getFileResolverConfiguration() : FileResolverConfigurationInterface |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the suffix for the import files. |
||
| 141 | * |
||
| 142 | * @return string The suffix |
||
| 143 | */ |
||
| 144 | protected function getSuffix() : string |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Initializes the file resolver for the import process with the passed serial. |
||
| 151 | * |
||
| 152 | * @param string $serial The unique identifier of the actual import process |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | * @throws \Exception Is thrown if the configured source directory is not available |
||
| 156 | */ |
||
| 157 | View Code Duplication | protected function initialize(string $serial) : void |
|
| 158 | { |
||
| 159 | |||
| 160 | // load the actual status |
||
| 161 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
||
| 162 | |||
| 163 | // query whether or not the configured source directory is available |
||
| 164 | if ($this->getFilesystemAdapter()->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
||
| 165 | // set the source directory, if it is accessible |
||
| 166 | $this->setSourceDir($sourceDir); |
||
| 167 | // return immediately |
||
| 168 | return; |
||
| 169 | } |
||
| 170 | |||
| 171 | // throw an exception otherwise |
||
| 172 | throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir)); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Set's the filesystem loader used to resolve the file that has to be processed. |
||
| 177 | * |
||
| 178 | * @param \TechDivision\Import\Loaders\PregMatchFilteredLoaderInterface $loader The filesystem loader instance |
||
| 179 | * |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | public function setLoader(PregMatchFilteredLoaderInterface $loader) : void |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Sets the subject configuration instance. |
||
| 189 | * |
||
| 190 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $subjectConfiguration The subject configuration |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | public function setSubjectConfiguration(SubjectConfigurationInterface $subjectConfiguration) : void |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the subject configuration instance. |
||
| 201 | * |
||
| 202 | * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
||
| 203 | */ |
||
| 204 | public function getSubjectConfiguration() : SubjectConfigurationInterface |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set's the filesystem adapter instance. |
||
| 211 | * |
||
| 212 | * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance |
||
| 213 | * |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Return's the filesystem adapter instance. |
||
| 223 | * |
||
| 224 | * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance |
||
| 225 | */ |
||
| 226 | public function getFilesystemAdapter() : FilesystemAdapterInterface |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Return's the matches of all filters. |
||
| 233 | * |
||
| 234 | * @return array The array with the matches |
||
| 235 | */ |
||
| 236 | public function getMatches() : array |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Reset's the registered filters. |
||
| 243 | * |
||
| 244 | * @return void |
||
| 245 | */ |
||
| 246 | public function reset() : void |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Loads the files from the source directory and return's them sorted. |
||
| 253 | * |
||
| 254 | * @param string $serial The unique identifier of the actual import process |
||
| 255 | * |
||
| 256 | * @return array The array with the files matching the subjects suffix |
||
| 257 | * @throws \Exception Is thrown, when the source directory is NOT available |
||
| 258 | */ |
||
| 259 | public function loadFiles(string $serial) : array |
||
| 269 | } |
||
| 270 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.