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 Stream 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 Stream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Stream implements StreamInterface |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var resource |
||
| 27 | */ |
||
| 28 | protected $resource; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string|resource |
||
| 32 | */ |
||
| 33 | protected $stream; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param string|resource $stream |
||
| 37 | * @param string $mode Mode with which to open stream |
||
| 38 | * @throws InvalidArgumentException |
||
| 39 | */ |
||
| 40 | 274 | public function __construct($stream, $mode = 'r') |
|
| 44 | |||
| 45 | /** |
||
| 46 | * Reads all data from the stream into a string, from the beginning to end. |
||
| 47 | * |
||
| 48 | * This method MUST attempt to seek to the beginning of the stream before |
||
| 49 | * reading data and read the stream until the end is reached. |
||
| 50 | * |
||
| 51 | * Warning: This could attempt to load a large amount of data into memory. |
||
| 52 | * |
||
| 53 | * This method MUST NOT raise an exception in order to conform with PHP's |
||
| 54 | * string casting operations. |
||
| 55 | * |
||
| 56 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | 8 | public function __toString() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Closes the stream and any underlying resources. |
||
| 77 | * |
||
| 78 | * @return void |
||
| 79 | */ |
||
| 80 | 6 | public function close() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Separates any underlying resources from the stream. |
||
| 91 | * |
||
| 92 | * After the stream has been detached, the stream is in an unusable state. |
||
| 93 | * |
||
| 94 | * @return resource|null Underlying PHP stream, if any |
||
| 95 | */ |
||
| 96 | 26 | public function detach() |
|
| 102 | |||
| 103 | /** |
||
| 104 | * Attach a new stream/resource to the instance. |
||
| 105 | * |
||
| 106 | * @param string|resource $resource |
||
| 107 | * @param string $mode |
||
| 108 | * @throws InvalidArgumentException for stream identifier that cannot be |
||
| 109 | * cast to a resource |
||
| 110 | * @throws InvalidArgumentException for non-resource stream |
||
| 111 | */ |
||
| 112 | 32 | public function attach($resource, $mode = 'r') |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get the size of the stream if known. |
||
| 119 | * |
||
| 120 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
| 121 | */ |
||
| 122 | 6 | public function getSize() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Returns the current position of the file read/write pointer |
||
| 133 | * |
||
| 134 | * @return int Position of the file pointer |
||
| 135 | * @throws \RuntimeException on error. |
||
| 136 | */ |
||
| 137 | 8 | public function tell() |
|
| 138 | { |
||
| 139 | 8 | if (!$this->resource) { |
|
| 140 | 2 | throw new RuntimeException( |
|
| 141 | 1 | 'No resource available; cannot tell position' |
|
| 142 | 1 | ); |
|
| 143 | } |
||
| 144 | 6 | $result = ftell($this->resource); |
|
| 145 | 6 | if (!is_int($result)) { |
|
| 146 | throw new RuntimeException( |
||
| 147 | 'Error occurred during tell operation' |
||
| 148 | ); |
||
| 149 | } |
||
| 150 | 6 | return $result; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Returns true if the stream is at the end of the stream. |
||
| 155 | * |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 8 | public function eof() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Returns whether or not the stream is seekable. |
||
| 169 | * |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | 20 | public function isSeekable() |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Seek to a position in the stream. |
||
| 184 | * |
||
| 185 | * @link http://www.php.net/manual/en/function.fseek.php |
||
| 186 | * @param int $offset Stream offset |
||
| 187 | * @param int $whence Specifies how the cursor position will be calculated |
||
| 188 | * based on the seek offset. Valid values are identical to the built-in |
||
| 189 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
| 190 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
| 191 | * SEEK_END: Set position to end-of-stream plus offset. |
||
| 192 | * @throws \RuntimeException on failure. |
||
| 193 | * |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 18 | public function seek($offset, $whence = SEEK_SET) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Seek to the beginning of the stream. |
||
| 213 | * |
||
| 214 | * If the stream is not seekable, this method will raise an exception; |
||
| 215 | * otherwise, it will perform a seek(0). |
||
| 216 | * |
||
| 217 | * @see seek() |
||
| 218 | * @link http://www.php.net/manual/en/function.fseek.php |
||
| 219 | * @throws \RuntimeException on failure. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | 12 | public function rewind() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Returns whether or not the stream is writable. |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | 80 | public function isWritable() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Write data to the stream. |
||
| 251 | * |
||
| 252 | * @param string $string The string that is to be written. |
||
| 253 | * @return int Returns the number of bytes written to the stream. |
||
| 254 | * @throws \RuntimeException on failure. |
||
| 255 | */ |
||
| 256 | 34 | View Code Duplication | public function write($string) |
| 270 | |||
| 271 | /** |
||
| 272 | * Returns whether or not the stream is readable. |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | 64 | public function isReadable() |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Read data from the stream. |
||
| 288 | * |
||
| 289 | * @param int $length Read up to $length bytes from the object and return |
||
| 290 | * them. Fewer than $length bytes may be returned if underlying stream |
||
| 291 | * call returns fewer bytes. |
||
| 292 | * @return string Returns the data read from the stream, or an empty string |
||
| 293 | * if no bytes are available. |
||
| 294 | * @throws \RuntimeException if an error occurs. |
||
| 295 | */ |
||
| 296 | 6 | View Code Duplication | public function read($length) |
| 310 | |||
| 311 | /** |
||
| 312 | * Returns the remaining contents in a string |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | * @throws \RuntimeException if unable to read or an error occurs while |
||
| 316 | * reading. |
||
| 317 | */ |
||
| 318 | 12 | public function getContents() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Get stream metadata as an associative array or retrieve a specific key. |
||
| 332 | * |
||
| 333 | * The keys returned are identical to the keys returned from PHP's |
||
| 334 | * stream_get_meta_data() function. |
||
| 335 | * |
||
| 336 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
| 337 | * @param string $key Specific metadata to retrieve. |
||
| 338 | * @return array|mixed|null Returns an associative array if no key is |
||
| 339 | * provided. Returns a specific key value if a key is provided and the |
||
| 340 | * value is found, or null if the key is not found. |
||
| 341 | */ |
||
| 342 | 6 | public function getMetadata($key = null) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Set the internal stream resource. |
||
| 356 | * |
||
| 357 | * @param string|resource $stream String stream target or stream resource. |
||
| 358 | * @param string $mode Resource mode for stream target. |
||
| 359 | * @throws InvalidArgumentException for invalid streams or resources. |
||
| 360 | */ |
||
| 361 | 274 | private function setStream($stream, $mode = 'r') |
|
| 391 | } |