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 |
||
| 45 | class ProposedOkFilenameLoader extends FilteredLoader implements FilteredLoaderInterface, SortedLoaderInterface |
||
| 46 | { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The regular expression used to load the files with. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $regex = '/^.*\/%s\\.%s$/'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The actual source directory to use. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $sourceDir; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The file resolver configuration instance. |
||
| 64 | * |
||
| 65 | * @var \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface |
||
| 66 | */ |
||
| 67 | private $fileResolverConfiguration; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The sorter instance to use. |
||
| 71 | * |
||
| 72 | * @var \TechDivision\Import\Loaders\Sorters\SorterImplInterface |
||
| 73 | */ |
||
| 74 | private $sorterImpl; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Initializes the file handler instance. |
||
| 78 | * |
||
| 79 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
|
|
|||
| 80 | * @param \TechDivision\Import\Loaders\FilteredLoaderInterface $loader The parent loader instance |
||
| 81 | * @param \TechDivision\Import\Loaders\Filters\FilterImplInterface $filterImpl The filter instance to use |
||
| 82 | * @param \TechDivision\Import\Loaders\Sorters\SorterImplInterface $sorterImpl The sorter instance to use |
||
| 83 | */ |
||
| 84 | public function __construct( |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns the file resolver configuration instance. |
||
| 99 | * |
||
| 100 | * @return \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface The configuration instance |
||
| 101 | */ |
||
| 102 | protected function getFileResolverConfiguration() : FileResolverConfigurationInterface |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Return's the sorter instance to use. |
||
| 109 | * |
||
| 110 | * @return \TechDivision\Import\Loaders\Sorters\SorterImplInterface The sorter instance to use |
||
| 111 | */ |
||
| 112 | protected function getSorterImpl() : SorterImplInterface |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Returns the regular expression used to load the files with. |
||
| 119 | * |
||
| 120 | * @return string The regular expression |
||
| 121 | */ |
||
| 122 | protected function getRegex() : string |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Remove's the passed line from the file with the passed name. |
||
| 129 | * |
||
| 130 | * @param string $line The line to be removed |
||
| 131 | * @param string $filename The name of the file the line has to be removed |
||
| 132 | * |
||
| 133 | * @return void |
||
| 134 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 135 | */ |
||
| 136 | protected function removeLineFromFile(string $line, string $filename) : void |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Query whether or not the basename, without suffix, of the passed filenames are equal. |
||
| 143 | * |
||
| 144 | * @param string $filename1 The first filename to compare |
||
| 145 | * @param string $filename2 The second filename to compare |
||
| 146 | * |
||
| 147 | * @return boolean TRUE if the passed files basename are equal, else FALSE |
||
| 148 | * @todo Refactorig required, because of duplicate method |
||
| 149 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::isEqualFilename() |
||
| 150 | */ |
||
| 151 | protected function isEqualFilename(string $filename1, string $filename2) : bool |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Strips the passed suffix, including the (.), from the filename and returns it. |
||
| 158 | * |
||
| 159 | * @param string $filename The filename to return the suffix from |
||
| 160 | * @param string $suffix The suffix to return |
||
| 161 | * |
||
| 162 | * @return string The filname without the suffix |
||
| 163 | * @todo Refactorig required, because of duplicate method |
||
| 164 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::stripSuffix() |
||
| 165 | */ |
||
| 166 | protected function stripSuffix(string $filename, string $suffix) : string |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Prepares and returns an OK filename from the passed parts. |
||
| 173 | * |
||
| 174 | * @param array $parts The parts to concatenate the OK filename from |
||
| 175 | * |
||
| 176 | * @return string The OK filename |
||
| 177 | * @todo Refactorig required, because of duplicate method |
||
| 178 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::prepareOkFilename() |
||
| 179 | */ |
||
| 180 | protected function prepareOkFilename(array $parts) : string |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Returns the delement separator char. |
||
| 187 | * |
||
| 188 | * @return string The element separator char |
||
| 189 | */ |
||
| 190 | protected function getElementSeparator() : string |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the elements the filenames consists of. |
||
| 197 | * |
||
| 198 | * @return array The array with the filename elements |
||
| 199 | * @todo Refactorig required, because of duplicate method |
||
| 200 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getPatternElements() |
||
| 201 | */ |
||
| 202 | protected function getPatternElements() : array |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns the suffix for the import files. |
||
| 209 | * |
||
| 210 | * @return string The suffix |
||
| 211 | */ |
||
| 212 | protected function getSuffix() : string |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Returns the elements the filenames consists of. |
||
| 219 | * |
||
| 220 | * @return array The array with the filename elements |
||
| 221 | * @todo Refactorig required, because of duplicate method |
||
| 222 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getOkFileSuffix() |
||
| 223 | */ |
||
| 224 | protected function getOkFileSuffix() : string |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns the actual source directory to load the files from. |
||
| 231 | * |
||
| 232 | * @return string The actual source directory |
||
| 233 | */ |
||
| 234 | protected function getSourceDir() : string |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Returns the elements the filenames consists of, converted to lowercase. |
||
| 241 | * |
||
| 242 | * @return array The array with the filename elements |
||
| 243 | * @todo Refactorig required, because of duplicate method |
||
| 244 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::getPatternKeys() |
||
| 245 | */ |
||
| 246 | protected function getPatternKeys() : array |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Resolves the pattern value for the given element name. |
||
| 263 | * |
||
| 264 | * @param string $element The element name to resolve the pattern value for |
||
| 265 | * |
||
| 266 | * @return string The resolved pattern value |
||
| 267 | * @throws \InvalidArgumentException Is thrown, if the element value can not be loaded from the file resolver configuration |
||
| 268 | * @todo Refactorig required, because of duplicate method |
||
| 269 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::resolvePatternValue() |
||
| 270 | */ |
||
| 271 | View Code Duplication | protected function resolvePatternValue(string $element) : string |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Returns the values to create the regex pattern from. |
||
| 294 | * |
||
| 295 | * @return array The array with the pattern values |
||
| 296 | * @todo Refactorig required, because of duplicate method |
||
| 297 | * @see \TechDivision\Import\Loaders\Filters\OkFileFilter::resolvePatternValues() |
||
| 298 | */ |
||
| 299 | protected function resolvePatternValues() : array |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Prepares and returns the pattern for the regex to load the files from the |
||
| 316 | * source directory for the passed subject. |
||
| 317 | * |
||
| 318 | * @return string The prepared regex pattern |
||
| 319 | */ |
||
| 320 | protected function getPattern() : string |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Returns the match with the passed name. |
||
| 331 | * |
||
| 332 | * @param array $keys The keys of the match to return |
||
| 333 | * |
||
| 334 | * @return string|null The match itself |
||
| 335 | */ |
||
| 336 | protected function proposedOkFilenamesByKeys(array $keys) : array |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return's an array with the proposed .OK filenames as key and the matching CSV |
||
| 375 | * files as values for the import with the passed serial. |
||
| 376 | * |
||
| 377 | * Based on the passed serial, the files with the configured prefix from the also |
||
| 378 | * configured source directory will be loaded and processed to return the array |
||
| 379 | * with the proposed .OK filenames. |
||
| 380 | * |
||
| 381 | * @return array The array with key => value pairs of the proposed .OK and the matching CSV files |
||
| 382 | */ |
||
| 383 | protected function propsedOkFilenames() : array |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return's the actual source directory. |
||
| 412 | * |
||
| 413 | * @param string $sourceDir The actual source directory |
||
| 414 | */ |
||
| 415 | public function setSourceDir(string $sourceDir) : void |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Add's the passed sorter to the loader instance. |
||
| 422 | * |
||
| 423 | * @param callable $sorter The sorter to add |
||
| 424 | * |
||
| 425 | * @return void |
||
| 426 | */ |
||
| 427 | public function addSorter(callable $sorter) : void |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Return's the array with the sorter callbacks. |
||
| 434 | * |
||
| 435 | * @return callable[] The sorter callbacks |
||
| 436 | */ |
||
| 437 | public function getSorters() : array |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set's the file resolver configuration. |
||
| 444 | * |
||
| 445 | * @param \TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface $fileResolverConfiguration The file resolver configuration |
||
| 446 | */ |
||
| 447 | public function setFileResolverConfiguration(FileResolverConfigurationInterface $fileResolverConfiguration) : void |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Loads, sorts and returns the files by using the passed glob pattern. |
||
| 454 | * |
||
| 455 | * If no pattern will be passed to the `load()` method, the files of |
||
| 456 | * the actual directory using `getcwd()` will be returned. |
||
| 457 | * |
||
| 458 | * @param string|null $pattern The pattern to load the files from the filesystem |
||
| 459 | * |
||
| 460 | * @return array The array with the data |
||
| 461 | */ |
||
| 462 | public function load(string $pattern = null) : array |
||
| 500 | } |
||
| 501 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.