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 | trait LocalDirTrait |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * {@inheritDoc} |
||
| 33 | */ |
||
| 34 | protected function isRealDir(/*# string */ $realPath)/*# : bool */ |
||
| 35 | { |
||
| 36 | return is_dir($realPath); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | protected function readDir( |
||
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritDoc} |
||
| 65 | */ |
||
| 66 | protected function renameDir( |
||
| 72 | |||
| 73 | /** |
||
| 74 | * {@inheritDoc} |
||
| 75 | */ |
||
| 76 | protected function copyDir( |
||
| 77 | /*# string */ $from, |
||
| 78 | /*# string */ $to |
||
| 79 | )/*# : bool */ { |
||
| 80 | $res = $this->makeDirectory($to); |
||
| 81 | |||
| 82 | $files = $this->readDir($from); |
||
| 83 | foreach ($files as $file) { |
||
| 84 | if (false === $res) { |
||
| 85 | return false; |
||
| 86 | } |
||
| 87 | |||
| 88 | $f = $from . \DIRECTORY_SEPARATOR . $file; |
||
| 89 | $t = $to . \DIRECTORY_SEPARATOR . $file; |
||
| 90 | if (is_dir($f)) { |
||
| 91 | $res = $this->copyDir($f, $t); |
||
| 92 | } else { |
||
| 93 | $res = @copy($f, $t); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | return $res; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * {@inheritDoc} |
||
| 101 | */ |
||
| 102 | protected function deleteDir( |
||
| 103 | /*# string */ $realPath, |
||
| 104 | /*# bool */ $keep = false |
||
| 105 | )/*# : bool */ { |
||
| 106 | $pref = rtrim($realPath, '/\\') . \DIRECTORY_SEPARATOR; |
||
| 107 | $files = $this->readDir($realPath, $pref); |
||
| 108 | |||
| 109 | foreach ($files as $file) { |
||
| 110 | if (is_dir($file)) { |
||
| 111 | $res = $this->deleteDir($file); |
||
| 112 | } else { |
||
| 113 | $res = unlink($file); |
||
| 114 | } |
||
| 115 | if (false === $res) { |
||
| 116 | return $this->setError( |
||
| 117 | Message::get(Message::STR_DELETE_FAIL, $file), |
||
| 118 | Message::STR_DELETE_FAIL |
||
| 119 | ); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | return $keep ? true : rmdir($realPath); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritDoc} |
||
| 128 | */ |
||
| 129 | protected function makeDirectory(/*# string */ $realPath)/*# : bool */ |
||
| 130 | { |
||
| 131 | if (!is_dir($realPath)) { |
||
| 132 | $umask = umask(0); |
||
| 133 | @mkdir($realPath, 0755, true); |
||
|
|
|||
| 134 | umask($umask); |
||
| 135 | |||
| 136 | View Code Duplication | if (!is_dir($realPath)) { |
|
| 137 | $this->setError( |
||
| 138 | Message::get(Message::STR_MKDIR_FAIL, $realPath), |
||
| 139 | Message::STR_MKDIR_FAIL |
||
| 140 | ); |
||
| 141 | return false; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | return true; |
||
| 145 | } |
||
| 146 | |||
| 147 | abstract public function setError( |
||
| 151 | } |
If you suppress an error, we recommend checking for the error condition explicitly: