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 OkFileHandler implements OkFileHandlerInterface |
||
| 39 | { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The loader instance used to load the proposed .OK filenames and it's content. |
||
| 43 | * |
||
| 44 | * @var \TechDivision\Import\Loaders\FilteredLoaderInterface |
||
| 45 | */ |
||
| 46 | private $loader; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The generic file handler instance. |
||
| 50 | * |
||
| 51 | * @var \TechDivision\Import\Handlers\GenericFileHandlerInterface |
||
| 52 | */ |
||
| 53 | private $genericFileHandler; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The filesystem adapter instance. |
||
| 57 | * |
||
| 58 | * @var \TechDivision\Import\Adapter\FilesystemAdapterInterface |
||
| 59 | */ |
||
| 60 | private $filesystemAdapter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The file resolver configuration instance. |
||
| 64 | * |
||
| 65 | * @var \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface |
||
| 66 | */ |
||
| 67 | private $fileResolverConfiguration; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Initializes the file handler instance. |
||
| 71 | * |
||
| 72 | * @param \TechDivision\Import\Handlers\GenericFileHandlerInterface|null $genericFileHandler The generic file handler instance |
||
| 73 | */ |
||
| 74 | public function __construct(GenericFileHandlerInterface $genericFileHandler = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Return's the generic file handler instance. |
||
| 81 | * |
||
| 82 | * @return \TechDivision\Import\Handlers\GenericFileHandlerInterface The generic file handler instance |
||
| 83 | */ |
||
| 84 | protected function getGenericFileHandler() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Return's the filesystem adapter instance. |
||
| 91 | * |
||
| 92 | * @return \TechDivision\Import\Adapter\FilesystemAdapterInterface The filesystem adapter instance |
||
| 93 | */ |
||
| 94 | protected function getFilesystemAdapter() |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Return's the loader instance used to load the proposed .OK filenames and it's content. |
||
| 101 | * |
||
| 102 | * @return \TechDivision\Import\Loaders\FilteredLoaderInterface The loader instance |
||
| 103 | */ |
||
| 104 | protected function getLoader() : FilteredLoaderInterface |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns the file resolver configuration instance. |
||
| 111 | * |
||
| 112 | * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
||
| 113 | */ |
||
| 114 | protected function getFileResolverConfiguration() : FileResolverConfigurationInterface |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Remove's the passed line from the file with the passed name. |
||
| 121 | * |
||
| 122 | * @param string $line The line to be removed |
||
| 123 | * @param string $filename The name of the file the line has to be removed |
||
| 124 | * |
||
| 125 | * @return void |
||
| 126 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 127 | */ |
||
| 128 | protected function removeLineFromFile(string $line, string $filename) : void |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Query whether or not the basename, without suffix, of the passed filenames are equal. |
||
| 135 | * |
||
| 136 | * @param string $filename1 The first filename to compare |
||
| 137 | * @param string $filename2 The second filename to compare |
||
| 138 | * |
||
| 139 | * @return boolean TRUE if the passed files basename are equal, else FALSE |
||
| 140 | * @todo Refactorig required, because of duplicate method |
||
| 141 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::isEqualFilename() |
||
| 142 | */ |
||
| 143 | protected function isEqualFilename(string $filename1, string $filename2) : bool |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Strips the passed suffix, including the (.), from the filename and returns it. |
||
| 150 | * |
||
| 151 | * @param string $filename The filename to return the suffix from |
||
| 152 | * @param string $suffix The suffix to return |
||
| 153 | * |
||
| 154 | * @return string The filname without the suffix |
||
| 155 | * @todo Refactorig required, because of duplicate method |
||
| 156 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::stripSuffix() |
||
| 157 | */ |
||
| 158 | protected function stripSuffix(string $filename, string $suffix) : string |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Prepares and returns an OK filename from the passed parts. |
||
| 165 | * |
||
| 166 | * @param array $parts The parts to concatenate the OK filename from |
||
| 167 | * |
||
| 168 | * @return string The OK filename |
||
| 169 | * @todo Refactorig required, because of duplicate method |
||
| 170 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::prepareOkFilename() |
||
| 171 | */ |
||
| 172 | protected function prepareOkFilename(array $parts) : string |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Returns the delement separator char. |
||
| 179 | * |
||
| 180 | * @return string The element separator char |
||
| 181 | */ |
||
| 182 | protected function getElementSeparator() : string |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns the elements the filenames consists of. |
||
| 189 | * |
||
| 190 | * @return array The array with the filename elements |
||
| 191 | * @todo Refactorig required, because of duplicate method |
||
| 192 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getPatternElements() |
||
| 193 | */ |
||
| 194 | protected function getPatternElements() : array |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Returns the elements the filenames consists of. |
||
| 201 | * |
||
| 202 | * @return array The array with the filename elements |
||
| 203 | * @todo Refactorig required, because of duplicate method |
||
| 204 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getOkFileSuffix() |
||
| 205 | */ |
||
| 206 | protected function getOkFileSuffix() : array |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns the actual source directory to load the files from. |
||
| 213 | * |
||
| 214 | * @return string The actual source directory |
||
| 215 | */ |
||
| 216 | protected function getSourceDir() : string |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns the elements the filenames consists of, converted to lowercase. |
||
| 223 | * |
||
| 224 | * @return array The array with the filename elements |
||
| 225 | * @todo Refactorig required, because of duplicate method |
||
| 226 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getPatternKeys() |
||
| 227 | */ |
||
| 228 | protected function getPatternKeys() : array |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return's an array with the names of the expected OK files for the actual subject. |
||
| 245 | * |
||
| 246 | * @return array The array with the expected OK filenames |
||
| 247 | * @todo Refactorig required, because of duplicate method |
||
| 248 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getOkFilenames() |
||
| 249 | */ |
||
| 250 | View Code Duplication | protected function getOkFilenames() : array |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Set's the loader instance used to load the proposed .OK filenames and it's content. |
||
| 278 | * |
||
| 279 | * @param \TechDivision\Import\Loaders\FilteredLoaderInterface $loader The loader instance |
||
| 280 | * |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | public function setLoader(FilteredLoaderInterface $loader) : void |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set's the file resolver configuration. |
||
| 290 | * |
||
| 291 | * @param \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface $fileResolverConfiguration The file resolver configuration |
||
| 292 | */ |
||
| 293 | public function setFileResolverConfiguration(FileResolverConfigurationInterface $fileResolverConfiguration) : void |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Set's the filesystem adapter instance. |
||
| 300 | * |
||
| 301 | * @param \TechDivision\Import\Adapter\FilesystemAdapterInterface $filesystemAdapter The filesystem adapter instance |
||
| 302 | */ |
||
| 303 | public function setFilesystemAdapter(FilesystemAdapterInterface $filesystemAdapter) : void |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
| 310 | * the OK file will be cleaned-up. |
||
| 311 | * |
||
| 312 | * @param string $filename The filename to be cleaned-up |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or the OK can not be cleaned-up |
||
| 316 | */ |
||
| 317 | View Code Duplication | public function cleanUpOkFile(string $filename) : void |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Create's the .OK files for all .CSV files that matches the passed pattern. |
||
| 379 | * |
||
| 380 | * @param string $pattern The pattern that matches the .CSV files we want to create the .OK files for |
||
| 381 | * |
||
| 382 | * @return int Return's the number of created .OK files |
||
| 383 | * @throws \Exception Is thrown, one of the proposed .OK files can not be created |
||
| 384 | */ |
||
| 385 | public function createOkFiles(string $pattern) : int |
||
| 407 | } |
||
| 408 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: