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 |
||
| 38 | class OkFileAwareFileWriter implements OkFileAwareFileWriterInterface |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The registry processor instance. |
||
| 43 | * |
||
| 44 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 45 | */ |
||
| 46 | private $registryProcessor; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The actual source directory to load the files from. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $sourceDir; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The filesystem adapter instance. |
||
| 57 | * |
||
| 58 | * @var \TechDivision\Import\Adapter\FilesystemAdapterInterface |
||
| 59 | */ |
||
| 60 | private $filesystemAdapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The .OK file handler instance to use. |
||
| 64 | * |
||
| 65 | * @var \TechDivision\Import\Handlers\OkFileHandlerInterface |
||
| 66 | */ |
||
| 67 | private $handler; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The file resolver configuration instance. |
||
| 71 | * |
||
| 72 | * @var \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface |
||
| 73 | */ |
||
| 74 | private $fileResolverConfiguration; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initializes the file resolver with the application and the registry instance. |
||
| 78 | * |
||
| 79 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry instance |
||
| 80 | */ |
||
| 81 | public function __construct(RegistryProcessorInterface $registryProcessor) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Return's the registry processor instance. |
||
| 88 | * |
||
| 89 | * @return \TechDivision\Import\Services\RegistryProcessorInterface the registry processor instance |
||
| 90 | */ |
||
| 91 | protected function getRegistryProcessor() : RegistryProcessorInterface |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Return's the .OK file handler instance. |
||
| 98 | * |
||
| 99 | * @return \TechDivision\Import\Handlers\OkFileHandlerInterface The .OK file handler instance |
||
| 100 | */ |
||
| 101 | protected function getHandler() : OkFileHandlerInterface |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Return's the filesystem adapter instance. |
||
| 108 | * |
||
| 109 | * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance |
||
| 110 | */ |
||
| 111 | protected function getFilesystemAdapter() : FilesystemAdapterInterface |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Returns the file resolver configuration instance. |
||
| 118 | * |
||
| 119 | * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
||
| 120 | */ |
||
| 121 | protected function getFileResolverConfiguration() : FileResolverConfigurationInterface |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Sets the actual source directory to load the files from. |
||
| 128 | * |
||
| 129 | * @param string $sourceDir The actual source directory |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | protected function setSourceDir(string $sourceDir) : void |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Returns the actual source directory to load the files from. |
||
| 140 | * |
||
| 141 | * @return string The actual source directory |
||
| 142 | */ |
||
| 143 | protected function getSourceDir() : string |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Initializes the file resolver for the import process with the passed serial. |
||
| 150 | * |
||
| 151 | * @param string $serial The unique identifier of the actual import process |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | * @throws \Exception Is thrown if the configured source directory is not available |
||
| 155 | */ |
||
| 156 | View Code Duplication | protected function initialize(string $serial) : void |
|
| 157 | { |
||
| 158 | |||
| 159 | // load the actual status |
||
| 160 | $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS); |
||
| 161 | |||
| 162 | // query whether or not the configured source directory is available |
||
| 163 | if ($this->getFilesystemAdapter()->isDir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) { |
||
| 164 | // set the source directory, if it is accessible |
||
| 165 | $this->setSourceDir($sourceDir); |
||
| 166 | // return immediately |
||
| 167 | return; |
||
| 168 | } |
||
| 169 | |||
| 170 | // throw an exception otherwise |
||
| 171 | throw new \Exception(sprintf('Configured source directory "%s" is not available!', $sourceDir)); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns the suffix for the import files. |
||
| 176 | * |
||
| 177 | * @return string The suffix |
||
| 178 | */ |
||
| 179 | protected function getSuffix() : string |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set's he .OK file handler instance. |
||
| 186 | * |
||
| 187 | * @param \TechDivision\Import\Handlers\OkFileHandlerInterface $handler The .OK file handler instance |
||
| 188 | * |
||
| 189 | * @return void |
||
| 190 | */ |
||
| 191 | public function setHandler(OkFileHandlerInterface $handler) :void |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set's the filesystem adapter instance. |
||
| 198 | * |
||
| 199 | * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance |
||
| 200 | * |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Set's the file resolver configuration. |
||
| 210 | * |
||
| 211 | * @param \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface $fileResolverConfiguration The file resolver configuration |
||
| 212 | * |
||
| 213 | * @return void |
||
| 214 | */ |
||
| 215 | public function setFileResolverConfiguration(FileResolverConfigurationInterface $fileResolverConfiguration) : void |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Create's the .OK files for the import with the passed serial. |
||
| 222 | * |
||
| 223 | * @param string $serial The serial to create the .OK files for |
||
| 224 | * |
||
| 225 | * @return int Return's the number of created .OK files |
||
| 226 | * @throws \Exception Is thrown, one of the proposed .OK files can not be created |
||
| 227 | */ |
||
| 228 | public function createOkFiles(string $serial) : int |
||
| 238 | } |
||
| 239 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.