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 |
||
| 37 | abstract class DriverAbstract extends ObjectAbstract implements DriverInterface, ErrorAwareInterface, ExtensionAwareInterface |
||
| 38 | { |
||
| 39 | use ErrorAwareTrait, ExtensionAwareTrait; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Store meta data in a seperate file |
||
| 43 | * |
||
| 44 | * @var bool |
||
| 45 | * @access protected |
||
| 46 | */ |
||
| 47 | protected $use_metafile = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * {@inheritDoc} |
||
| 51 | */ |
||
| 52 | public function exists(/*# string */ $path)/*# : bool */ |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritDoc} |
||
| 60 | */ |
||
| 61 | public function getContent(/*# string */ $path, /*# bool */ $stream = false) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritDoc} |
||
| 75 | */ |
||
| 76 | public function getMeta(/*# string */ $path)/*# : array */ |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritDoc} |
||
| 92 | */ |
||
| 93 | public function setContent(/*# string */ $path, $content)/*# : bool */ |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritDoc} |
||
| 114 | */ |
||
| 115 | public function setMeta(/*# string */ $path, array $meta)/*# : bool */ |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritDoc} |
||
| 134 | */ |
||
| 135 | public function isDir(/*# string */ $path)/*# : bool */ |
||
| 140 | |||
| 141 | /** |
||
| 142 | * {@inheritDoc} |
||
| 143 | */ |
||
| 144 | View Code Duplication | public function rename(/*# string */ $from, /*# string */ $to)/*# : bool */ |
|
| 164 | |||
| 165 | /** |
||
| 166 | * {@inheritDoc} |
||
| 167 | */ |
||
| 168 | View Code Duplication | public function copy(/*# string */ $from, /*# string */ $to)/*# : bool */ |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritDoc} |
||
| 190 | */ |
||
| 191 | public function delete(/*# string */ $path)/*# : bool */ |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Returns driver specific real path |
||
| 207 | * @param string $path |
||
| 208 | * @return string |
||
| 209 | * @access protected |
||
| 210 | */ |
||
| 211 | abstract protected function realPath(/*# string */ $path)/*# : string */; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Exists of real path |
||
| 215 | * |
||
| 216 | * @param string $realPath |
||
| 217 | * @return bool |
||
| 218 | * @access protected |
||
| 219 | */ |
||
| 220 | abstract protected function realExists(/*# string */ $realPath)/*# : bool */; |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Is path a directory |
||
| 224 | * |
||
| 225 | * @param string $realPath |
||
| 226 | * @return bool |
||
| 227 | * @access protected |
||
| 228 | */ |
||
| 229 | abstract protected function isRealDir(/*# string */ $realPath)/*# : bool */; |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Read directory, returns an array of paths in this directory |
||
| 233 | * |
||
| 234 | * @param string $realPath |
||
| 235 | * @param string $prefix prefix to prepend to the results |
||
| 236 | * @return array |
||
| 237 | * @access protected |
||
| 238 | */ |
||
| 239 | abstract protected function readDir( |
||
| 243 | |||
| 244 | /** |
||
| 245 | * create directory |
||
| 246 | * |
||
| 247 | * @param string $realPath |
||
| 248 | * @access protected |
||
| 249 | */ |
||
| 250 | abstract protected function makeDirectory( |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Open read stream |
||
| 256 | * |
||
| 257 | * @param string $realPath |
||
| 258 | * @return resource|null |
||
| 259 | * @access protected |
||
| 260 | */ |
||
| 261 | abstract protected function openReadStream(/*# string */ $realPath); |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Read file and returns all the content |
||
| 265 | * |
||
| 266 | * @param string $realPath |
||
| 267 | * @return string|null |
||
| 268 | * @access protected |
||
| 269 | */ |
||
| 270 | abstract protected function readFile(/*# string */ $realPath); |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Get the meta data |
||
| 274 | * |
||
| 275 | * @param string $realPath |
||
| 276 | * @return array |
||
| 277 | * @access protected |
||
| 278 | */ |
||
| 279 | abstract protected function getRealMeta(/*# string */ $realPath)/*# : array */; |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Make sure path directory exits. |
||
| 283 | * |
||
| 284 | * @param string $realPath |
||
| 285 | * @return bool |
||
| 286 | * @access protected |
||
| 287 | */ |
||
| 288 | abstract protected function ensurePath(/*# string */ $realPath)/*# : bool */; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Write to file from stream |
||
| 292 | * |
||
| 293 | * @param string $realPath |
||
| 294 | * @param resource $resource |
||
| 295 | * @return bool |
||
| 296 | * @access protected |
||
| 297 | */ |
||
| 298 | abstract protected function writeStream( |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Write to file |
||
| 305 | * |
||
| 306 | * @param string $realPath |
||
| 307 | * @param string $content |
||
| 308 | * @return bool |
||
| 309 | * @access protected |
||
| 310 | */ |
||
| 311 | abstract protected function writeFile( |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Write meta data |
||
| 318 | * |
||
| 319 | * @param string $realPath |
||
| 320 | * @param array $meta |
||
| 321 | * @return bool |
||
| 322 | * @access protected |
||
| 323 | */ |
||
| 324 | abstract protected function setRealMeta( |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Rename directory |
||
| 331 | * |
||
| 332 | * @param string $from |
||
| 333 | * @param string $to |
||
| 334 | * @return bool |
||
| 335 | * @access protected |
||
| 336 | */ |
||
| 337 | abstract protected function renameDir( |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Rename file |
||
| 344 | * |
||
| 345 | * @param string $from |
||
| 346 | * @param string $to |
||
| 347 | * @return bool |
||
| 348 | * @access protected |
||
| 349 | */ |
||
| 350 | abstract protected function renameFile( |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Copy directory |
||
| 357 | * |
||
| 358 | * @param string $from |
||
| 359 | * @param string $to |
||
| 360 | * @return bool |
||
| 361 | * @access protected |
||
| 362 | */ |
||
| 363 | abstract protected function copyDir( |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Copy file |
||
| 370 | * |
||
| 371 | * @param string $from |
||
| 372 | * @param string $to |
||
| 373 | * @return bool |
||
| 374 | * @access protected |
||
| 375 | */ |
||
| 376 | abstract protected function copyFile( |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Delete directory |
||
| 383 | * |
||
| 384 | * @param string $realPath |
||
| 385 | * @param bool keep the upper most directory |
||
| 386 | * @return bool |
||
| 387 | * @access protected |
||
| 388 | */ |
||
| 389 | abstract protected function deleteDir( |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Delete the file |
||
| 396 | * |
||
| 397 | * @param string $realPath |
||
| 398 | * @return bool |
||
| 399 | * @access protected |
||
| 400 | */ |
||
| 401 | abstract protected function deleteFile(/*# string */ $realPath)/*# : bool */; |
||
| 402 | } |
||
| 403 |