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 |
||
| 29 | class LegacyDFSCluster implements IOMetadataHandler |
||
| 30 | { |
||
| 31 | /** @var Connection */ |
||
| 32 | private $db; |
||
| 33 | |||
| 34 | /** @var UrlDecorator */ |
||
| 35 | private $urlDecorator; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param Connection $connection Doctrine DBAL connection |
||
| 39 | * @param UrlDecorator $urlDecorator The URL decorator used to add a prefix to files path |
||
| 40 | */ |
||
| 41 | public function __construct(Connection $connection, UrlDecorator $urlDecorator = null) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Inserts a new reference to file $spiBinaryFileId. |
||
| 49 | * |
||
| 50 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If a file $spiBinaryFileId already exists |
||
| 51 | * |
||
| 52 | * @param string $spiBinaryFileId |
||
|
|
|||
| 53 | * |
||
| 54 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException if an error occurs creating the record |
||
| 55 | * @throws RuntimeException if a DBAL error occurs |
||
| 56 | * |
||
| 57 | * @return \eZ\Publish\SPI\IO\BinaryFile |
||
| 58 | */ |
||
| 59 | public function create(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Deletes file $spiBinaryFileId. |
||
| 98 | * |
||
| 99 | * @throws BinaryFileNotFoundException If $spiBinaryFileId is not found |
||
| 100 | * |
||
| 101 | * @param string $spiBinaryFileId |
||
| 102 | */ |
||
| 103 | View Code Duplication | public function delete($spiBinaryFileId) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Loads and returns metadata for $spiBinaryFileId. |
||
| 120 | * |
||
| 121 | * @param string $spiBinaryFileId |
||
| 122 | * |
||
| 123 | * @return SPIBinaryFile |
||
| 124 | * |
||
| 125 | * @throws BinaryFileNotFoundException if no row is found for $spiBinaryFileId |
||
| 126 | * @throws DBALException Any unhandled DBAL exception |
||
| 127 | */ |
||
| 128 | public function load($spiBinaryFileId) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Checks if a file $spiBinaryFileId exists. |
||
| 147 | * |
||
| 148 | * @param string $spiBinaryFileId |
||
| 149 | * |
||
| 150 | * @throws NotFoundException |
||
| 151 | * @throws DBALException Any unhandled DBAL exception |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function exists($spiBinaryFileId) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 168 | * |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | protected function getNameTrunk(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Returns the value for the scope meta field, based on the created file's path. |
||
| 178 | * |
||
| 179 | * Note that this is slightly incorrect, as it will return binaryfile for media files as well. It is a bit |
||
| 180 | * of an issue, but shouldn't be a blocker given that this meta field isn't used that much. |
||
| 181 | * |
||
| 182 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | protected function getScope(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Adds the internal prefix string to $id. |
||
| 203 | * |
||
| 204 | * @param string $id |
||
| 205 | * |
||
| 206 | * @return string prefixed id |
||
| 207 | */ |
||
| 208 | protected function addPrefix($id) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Removes the internal prefix string from $prefixedId. |
||
| 215 | * |
||
| 216 | * @param string $prefixedId |
||
| 217 | * |
||
| 218 | * @return string the id without the prefix |
||
| 219 | * |
||
| 220 | * @throws InvalidBinaryFileIdException if the prefix isn't found in $prefixedId |
||
| 221 | */ |
||
| 222 | protected function removePrefix($prefixedId) |
||
| 226 | |||
| 227 | View Code Duplication | public function getMimeType($spiBinaryFileId) |
|
| 241 | |||
| 242 | public function deleteDirectory($spiPath) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Maps an array of data base properties (id, size, mtime, datatype, md5_path, path...) to an SPIBinaryFile object. |
||
| 251 | * |
||
| 252 | * @param array $properties database properties array |
||
| 253 | * |
||
| 254 | * @return SPIBinaryFile |
||
| 255 | */ |
||
| 256 | protected function mapArrayToSPIBinaryFile(array $properties) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 269 | * |
||
| 270 | * @return SPIBinaryFile |
||
| 271 | */ |
||
| 272 | protected function mapSPIBinaryFileCreateStructToSPIBinaryFile(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Count all available files in $scope. |
||
| 285 | * |
||
| 286 | * @param string $scope The file scope, one of 'binaryfile', 'image', 'mediafile' |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | View Code Duplication | public function count($scope) |
|
| 299 | } |
||
| 300 |
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.