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) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritDoc} |
||
| 77 | */ |
||
| 78 | public function getMeta(/*# string */ $path)/*# : array */ |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritDoc} |
||
| 96 | */ |
||
| 97 | public function setContent(/*# string */ $path, $content)/*# : bool */ |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritDoc} |
||
| 118 | */ |
||
| 119 | public function setMeta(/*# string */ $path, array $meta)/*# : bool */ |
||
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritDoc} |
||
| 138 | */ |
||
| 139 | public function isDir(/*# string */ $path)/*# : bool */ |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritDoc} |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function rename(/*# string */ $from, /*# string */ $to)/*# : bool */ |
|
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritDoc} |
||
| 171 | */ |
||
| 172 | View Code Duplication | public function copy(/*# string */ $from, /*# string */ $to)/*# : bool */ |
|
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritDoc} |
||
| 194 | */ |
||
| 195 | public function delete(/*# string */ $path)/*# : bool */ |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns driver specific real path |
||
| 211 | * @param string $path |
||
| 212 | * @return string |
||
| 213 | * @access protected |
||
| 214 | */ |
||
| 215 | abstract protected function realPath(/*# string */ $path)/*# : string */; |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Exists of real path |
||
| 219 | * |
||
| 220 | * @param string $realPath |
||
| 221 | * @return bool |
||
| 222 | * @access protected |
||
| 223 | */ |
||
| 224 | abstract protected function realExists(/*# string */ $realPath)/*# : bool */; |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Is path a directory |
||
| 228 | * |
||
| 229 | * @param string $realPath |
||
| 230 | * @return bool |
||
| 231 | * @access protected |
||
| 232 | */ |
||
| 233 | abstract protected function isRealDir(/*# string */ $realPath)/*# : bool */; |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Read directory, returns an array of paths in this directory |
||
| 237 | * |
||
| 238 | * @param string $realPath |
||
| 239 | * @param string $prefix prefix to prepend to the results |
||
| 240 | * @return array |
||
| 241 | * @access protected |
||
| 242 | */ |
||
| 243 | abstract protected function readDir( |
||
| 247 | |||
| 248 | /** |
||
| 249 | * create directory |
||
| 250 | * |
||
| 251 | * @param string $realPath |
||
| 252 | * @access protected |
||
| 253 | */ |
||
| 254 | abstract protected function makeDirectory( |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Open read stream |
||
| 260 | * |
||
| 261 | * @param string $realPath |
||
| 262 | * @return resource|null |
||
| 263 | * @access protected |
||
| 264 | */ |
||
| 265 | abstract protected function openReadStream(/*# string */ $realPath); |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Read file and returns all the content |
||
| 269 | * |
||
| 270 | * @param string $realPath |
||
| 271 | * @return string|null |
||
| 272 | * @access protected |
||
| 273 | */ |
||
| 274 | abstract protected function readFile(/*# string */ $realPath); |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the meta data |
||
| 278 | * |
||
| 279 | * @param string $realPath |
||
| 280 | * @return array |
||
| 281 | * @access protected |
||
| 282 | */ |
||
| 283 | abstract protected function getRealMeta(/*# string */ $realPath)/*# : array */; |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Make sure path directory exits. |
||
| 287 | * |
||
| 288 | * @param string $realPath |
||
| 289 | * @return bool |
||
| 290 | * @access protected |
||
| 291 | */ |
||
| 292 | abstract protected function ensurePath(/*# string */ $realPath)/*# : bool */; |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Write to file from stream |
||
| 296 | * |
||
| 297 | * @param string $realPath |
||
| 298 | * @param resource $resource |
||
| 299 | * @return bool |
||
| 300 | * @access protected |
||
| 301 | */ |
||
| 302 | abstract protected function writeStream( |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Write to file |
||
| 309 | * |
||
| 310 | * @param string $realPath |
||
| 311 | * @param string $content |
||
| 312 | * @return bool |
||
| 313 | * @access protected |
||
| 314 | */ |
||
| 315 | abstract protected function writeFile( |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Write meta data |
||
| 322 | * |
||
| 323 | * @param string $realPath |
||
| 324 | * @param array $meta |
||
| 325 | * @return bool |
||
| 326 | * @access protected |
||
| 327 | */ |
||
| 328 | abstract protected function setRealMeta( |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Rename directory |
||
| 335 | * |
||
| 336 | * @param string $from |
||
| 337 | * @param string $to |
||
| 338 | * @return bool |
||
| 339 | * @access protected |
||
| 340 | */ |
||
| 341 | abstract protected function renameDir( |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Rename file |
||
| 348 | * |
||
| 349 | * @param string $from |
||
| 350 | * @param string $to |
||
| 351 | * @return bool |
||
| 352 | * @access protected |
||
| 353 | */ |
||
| 354 | abstract protected function renameFile( |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Copy directory |
||
| 361 | * |
||
| 362 | * @param string $from |
||
| 363 | * @param string $to |
||
| 364 | * @return bool |
||
| 365 | * @access protected |
||
| 366 | */ |
||
| 367 | abstract protected function copyDir( |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Copy file |
||
| 374 | * |
||
| 375 | * @param string $from |
||
| 376 | * @param string $to |
||
| 377 | * @return bool |
||
| 378 | * @access protected |
||
| 379 | */ |
||
| 380 | abstract protected function copyFile( |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Delete directory |
||
| 387 | * |
||
| 388 | * @param string $realPath |
||
| 389 | * @param bool keep the upper most directory |
||
| 390 | * @return bool |
||
| 391 | * @access protected |
||
| 392 | */ |
||
| 393 | abstract protected function deleteDir( |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Delete the file |
||
| 400 | * |
||
| 401 | * @param string $realPath |
||
| 402 | * @return bool |
||
| 403 | * @access protected |
||
| 404 | */ |
||
| 405 | abstract protected function deleteFile(/*# string */ $realPath)/*# : bool */; |
||
| 406 | } |
||
| 407 |