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 | class AssemblyStream implements \Icewind\Streams\File { |
||
| 38 | |||
| 39 | /** @var resource */ |
||
| 40 | protected $context; |
||
| 41 | |||
| 42 | /** @var IFile[] */ |
||
| 43 | protected $nodes; |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | protected $pos = 0; |
||
| 47 | |||
| 48 | /** @var array */ |
||
| 49 | protected $sortedNodes; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | protected $size; |
||
| 53 | |||
| 54 | /** @var resource */ |
||
| 55 | protected $currentStream = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $path |
||
| 59 | * @param string $mode |
||
| 60 | * @param int $options |
||
| 61 | * @param string &$opened_path |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function stream_open($path, $mode, $options, &$opened_path) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string $offset |
||
| 93 | * @param int $whence |
||
| 94 | * @return bool |
||
| 95 | */ |
||
| 96 | public function stream_seek($offset, $whence = SEEK_SET) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function stream_tell() { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param int $count |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function stream_read($count) { |
||
| 112 | do { |
||
| 113 | if ($this->currentStream === null) { |
||
| 114 | list($node, $posInNode) = $this->getNodeForPosition($this->pos); |
||
| 115 | if (is_null($node)) { |
||
| 116 | // reached last node, no more data |
||
| 117 | return ''; |
||
| 118 | } |
||
| 119 | $this->currentStream = $this->getStream($node); |
||
| 120 | fseek($this->currentStream, $posInNode); |
||
| 121 | } |
||
| 122 | |||
| 123 | $data = fread($this->currentStream, $count); |
||
| 124 | // isset is faster than strlen |
||
| 125 | View Code Duplication | if (isset($data[$count - 1])) { |
|
| 126 | // we read the full count |
||
| 127 | $read = $count; |
||
| 128 | } else { |
||
| 129 | // reaching end of stream, which happens less often so strlen is ok |
||
| 130 | $read = strlen($data); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (feof($this->currentStream)) { |
||
| 134 | fclose($this->currentStream); |
||
| 135 | $this->currentNode = null; |
||
| 136 | $this->currentStream = null; |
||
| 137 | } |
||
| 138 | // if no data read, try again with the next node because |
||
| 139 | // returning empty data can make the caller think there is no more |
||
| 140 | // data left to read |
||
| 141 | } while ($read === 0); |
||
| 142 | |||
| 143 | // update position |
||
| 144 | $this->pos += $read; |
||
| 145 | return $data; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $data |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function stream_write($data) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @param int $option |
||
| 158 | * @param int $arg1 |
||
| 159 | * @param int $arg2 |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function stream_set_option($option, $arg1, $arg2) { |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param int $size |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | public function stream_truncate($size) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function stream_stat() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param int $operation |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function stream_lock($operation) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function stream_flush() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function stream_eof() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function stream_close() { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * Load the source from the stream context and return the context options |
||
| 213 | * |
||
| 214 | * @param string $name |
||
| 215 | * @return array |
||
| 216 | * @throws \Exception |
||
| 217 | */ |
||
| 218 | protected function loadContext($name) { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param IFile[] $nodes |
||
| 235 | * @return resource |
||
| 236 | * |
||
| 237 | * @throws \BadMethodCallException |
||
| 238 | */ |
||
| 239 | public static function wrap(array $nodes, IFile $backingFile = null, $length = null) { |
||
| 240 | $context = stream_context_create([ |
||
| 241 | 'assembly' => [ |
||
| 242 | 'nodes' => $nodes] |
||
| 243 | ]); |
||
| 244 | stream_wrapper_register('assembly', '\OCA\DAV\Upload\AssemblyStream'); |
||
| 245 | try { |
||
| 246 | $wrapped = fopen('assembly://', 'r', null, $context); |
||
| 247 | } catch (\BadMethodCallException $e) { |
||
| 248 | stream_wrapper_unregister('assembly'); |
||
| 249 | throw $e; |
||
| 250 | } |
||
| 251 | stream_wrapper_unregister('assembly'); |
||
| 252 | return $wrapped; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param $pos |
||
| 257 | * @return IFile | null |
||
| 258 | */ |
||
| 259 | protected function getNodeForPosition($pos) { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param IFile $node |
||
| 270 | * @return resource |
||
| 271 | */ |
||
| 272 | protected function getStream(IFile $node) { |
||
| 280 | |||
| 281 | } |
||
| 282 |
If you suppress an error, we recommend checking for the error condition explicitly: