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 | * @since 6.10 The mtime of the $binaryFileCreateStruct must be a DateTime, as specified in the struct doc. |
||
| 51 | * |
||
| 52 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 53 | * |
||
| 54 | * @throws InvalidArgumentException if the $binaryFileCreateStruct is invalid |
||
| 55 | * @throws RuntimeException if a DBAL error occurs |
||
| 56 | * |
||
| 57 | * @return \eZ\Publish\SPI\IO\BinaryFile |
||
| 58 | */ |
||
| 59 | public function create(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Deletes file $spiBinaryFileId. |
||
| 90 | * |
||
| 91 | * @throws BinaryFileNotFoundException If $spiBinaryFileId is not found |
||
| 92 | * |
||
| 93 | * @param string $spiBinaryFileId |
||
| 94 | */ |
||
| 95 | public function delete($spiBinaryFileId) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Loads and returns metadata for $spiBinaryFileId. |
||
| 112 | * |
||
| 113 | * @param string $spiBinaryFileId |
||
| 114 | * |
||
| 115 | * @return SPIBinaryFile |
||
| 116 | * |
||
| 117 | * @throws BinaryFileNotFoundException if no row is found for $spiBinaryFileId |
||
| 118 | * @throws DBALException Any unhandled DBAL exception |
||
| 119 | */ |
||
| 120 | public function load($spiBinaryFileId) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Checks if a file $spiBinaryFileId exists. |
||
| 139 | * |
||
| 140 | * @param string $spiBinaryFileId |
||
| 141 | * |
||
| 142 | * @throws NotFoundException |
||
| 143 | * @throws DBALException Any unhandled DBAL exception |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function exists($spiBinaryFileId) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 160 | * |
||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | protected function getNameTrunk(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Returns the value for the scope meta field, based on the created file's path. |
||
| 170 | * |
||
| 171 | * Note that this is slightly incorrect, as it will return binaryfile for media files as well. It is a bit |
||
| 172 | * of an issue, but shouldn't be a blocker given that this meta field isn't used that much. |
||
| 173 | * |
||
| 174 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 175 | * |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | protected function getScope(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Adds the internal prefix string to $id. |
||
| 195 | * |
||
| 196 | * @param string $id |
||
| 197 | * |
||
| 198 | * @return string prefixed id |
||
| 199 | */ |
||
| 200 | protected function addPrefix($id) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Removes the internal prefix string from $prefixedId. |
||
| 207 | * |
||
| 208 | * @param string $prefixedId |
||
| 209 | * |
||
| 210 | * @return string the id without the prefix |
||
| 211 | * |
||
| 212 | * @throws InvalidBinaryFileIdException if the prefix isn't found in $prefixedId |
||
| 213 | */ |
||
| 214 | protected function removePrefix($prefixedId) |
||
| 218 | |||
| 219 | View Code Duplication | public function getMimeType($spiBinaryFileId) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Delete directory and all the content under specified directory. |
||
| 236 | * |
||
| 237 | * @param string $spiPath SPI Path, not prefixed by URL decoration |
||
| 238 | */ |
||
| 239 | public function deleteDirectory($spiPath) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Maps an array of data base properties (id, size, mtime, datatype, md5_path, path...) to an SPIBinaryFile object. |
||
| 255 | * |
||
| 256 | * @param array $properties database properties array |
||
| 257 | * |
||
| 258 | * @return SPIBinaryFile |
||
| 259 | */ |
||
| 260 | protected function mapArrayToSPIBinaryFile(array $properties) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param SPIBinaryFileCreateStruct $binaryFileCreateStruct |
||
| 273 | * |
||
| 274 | * @return SPIBinaryFile |
||
| 275 | */ |
||
| 276 | protected function mapSPIBinaryFileCreateStructToSPIBinaryFile(SPIBinaryFileCreateStruct $binaryFileCreateStruct) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Generate the correct SQL query to insert an entry. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | protected function getFileInsertQuery() |
||
| 319 | } |
||
| 320 |