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:
Complex classes like AbstractStream often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractStream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class AbstractStream implements StreamInterface |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var resource |
||
| 24 | */ |
||
| 25 | protected $stream; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Reads all data from the stream into a string, from the beginning to end. |
||
| 29 | * |
||
| 30 | * This method MUST attempt to seek to the beginning of the stream before |
||
| 31 | * reading data and read the stream until the end is reached. |
||
| 32 | * |
||
| 33 | * Warning: This could attempt to load a large amount of data into memory. |
||
| 34 | * |
||
| 35 | * This method MUST NOT raise an exception in order to conform with PHP's |
||
| 36 | * string casting operations. |
||
| 37 | * |
||
| 38 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | public function __toString() |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Closes the stream and any underlying resources. |
||
| 57 | * |
||
| 58 | * @return void |
||
| 59 | */ |
||
| 60 | public function close() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Separates any underlying resources from the stream. |
||
| 68 | * |
||
| 69 | * After the stream has been detached, the stream is in an unusable state. |
||
| 70 | * |
||
| 71 | * @return resource|null Underlying PHP stream, if any |
||
| 72 | */ |
||
| 73 | public function detach() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Get the size of the stream if known. |
||
| 82 | * |
||
| 83 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
| 84 | */ |
||
| 85 | public function getSize() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Returns the current position of the file read/write pointer |
||
| 95 | * |
||
| 96 | * @return int Position of the file pointer |
||
| 97 | * @throws RuntimeException on error. |
||
| 98 | */ |
||
| 99 | View Code Duplication | public function tell() |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Returns true if the stream is at the end of the stream. |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function eof() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Returns whether or not the stream is seekable. |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isSeekable() |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Seek to a position in the stream. |
||
| 146 | * |
||
| 147 | * @link http://www.php.net/manual/en/function.fseek.php |
||
| 148 | * @param int $offset Stream offset |
||
| 149 | * @param int $whence Specifies how the cursor position will be calculated |
||
| 150 | * based on the seek offset. Valid values are identical to the built-in |
||
| 151 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
| 152 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
| 153 | * SEEK_END: Set position to end-of-stream plus offset. |
||
| 154 | * @throws RuntimeException on failure. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | public function seek($offset, $whence = SEEK_SET) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Seek to the beginning of the stream. |
||
| 175 | * |
||
| 176 | * If the stream is not seekable, this method will raise an exception; |
||
| 177 | * otherwise, it will perform a seek(0). |
||
| 178 | * |
||
| 179 | * @see seek() |
||
| 180 | * @link http://www.php.net/manual/en/function.fseek.php |
||
| 181 | * @throws \RuntimeException on failure. |
||
| 182 | */ |
||
| 183 | public function rewind() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns whether or not the stream is writable. |
||
| 190 | * |
||
| 191 | * @return bool |
||
| 192 | */ |
||
| 193 | public function isWritable() |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Write data to the stream. |
||
| 211 | * |
||
| 212 | * @param string $string The string that is to be written. |
||
| 213 | * @return int Returns the number of bytes written to the stream. |
||
| 214 | * @throws RuntimeException on failure. |
||
| 215 | */ |
||
| 216 | View Code Duplication | public function write($string) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Returns whether or not the stream is readable. |
||
| 233 | * |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | public function isReadable() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Read data from the stream. |
||
| 248 | * |
||
| 249 | * @param int $length Read up to $length bytes from the object and return |
||
| 250 | * them. Fewer than $length bytes may be returned if underlying stream |
||
| 251 | * call returns fewer bytes. |
||
| 252 | * @return string Returns the data read from the stream, or an empty string |
||
| 253 | * if no bytes are available. |
||
| 254 | * @throws RuntimeException if an error occurs. |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function read($length) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Returns the remaining contents in a string |
||
| 273 | * |
||
| 274 | * @return string |
||
| 275 | * @throws RuntimeException if unable to read or an error occurs while |
||
| 276 | * reading. |
||
| 277 | */ |
||
| 278 | View Code Duplication | public function getContents() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Get stream metadata as an associative array or retrieve a specific key. |
||
| 292 | * |
||
| 293 | * The keys returned are identical to the keys returned from PHP's |
||
| 294 | * stream_get_meta_data() function. |
||
| 295 | * |
||
| 296 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
| 297 | * @param string $key Specific metadata to retrieve. |
||
| 298 | * @return array|mixed|null Returns an associative array if no key is |
||
| 299 | * provided. Returns a specific key value if a key is provided and the |
||
| 300 | * value is found, or null if the key is not found. |
||
| 301 | */ |
||
| 302 | public function getMetadata($key = null) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Closes the stream on destroy |
||
| 316 | */ |
||
| 317 | public function __destruct() |
||
| 321 | } |