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 |
||
| 21 | class StringStream |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Content of stream |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private static $string = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Whether this stream can be read |
||
| 32 | * @var boolean |
||
| 33 | */ |
||
| 34 | private $read; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Whether this stream can be written |
||
| 38 | * @var boolean |
||
| 39 | */ |
||
| 40 | private $write; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Options |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $options; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Current position within stream |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | private $position; |
||
| 53 | private $path; |
||
| 54 | private static $registered = false; |
||
| 55 | |||
| 56 | private function setFlags($read, $write, $position) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Open stream |
||
| 65 | * @param string $aPath |
||
| 66 | * @param string $aMode |
||
| 67 | * @param int $aOptions |
||
| 68 | * @param string $aOpenedPath |
||
| 69 | * @return boolean |
||
| 70 | */ |
||
| 71 | public function stream_open($aPath, $aMode, $aOptions, &$aOpenedPath) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Read from stream |
||
| 123 | * @param int $aBytes number of bytes to return |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | function stream_read($aBytes) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Write to stream |
||
| 138 | * @param string $aData data to write |
||
| 139 | * @return int |
||
| 140 | */ |
||
| 141 | function stream_write($aData) { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Return current position |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | function stream_tell() { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Return if EOF |
||
| 163 | * @return boolean |
||
| 164 | */ |
||
| 165 | function stream_eof() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Seek to new position |
||
| 171 | * @param int $aOffset |
||
| 172 | * @param int $aWhence |
||
| 173 | * @return boolean |
||
| 174 | */ |
||
| 175 | function stream_seek($aOffset, $aWhence) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Truncate to given size |
||
| 206 | * @param int $aSize |
||
| 207 | */ |
||
| 208 | public function stream_truncate($aSize) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Return info about stream |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function stream_stat() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Return info about stream |
||
| 239 | * @param string $aPath |
||
| 240 | * @param array $aOptions |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function url_stat($aPath, $aOptions) { |
||
| 247 | |||
| 248 | public static function register() { |
||
| 257 | |||
| 258 | public static function unregister() { |
||
| 263 | |||
| 264 | } |
||
| 265 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.