Complex classes like MessageBody 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 MessageBody, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class MessageBody implements StreamInterface |
||
21 | { |
||
22 | /** |
||
23 | * Modes in which a stream is readable |
||
24 | * |
||
25 | * @var array |
||
26 | * @link http://php.net/manual/function.fopen.php |
||
27 | */ |
||
28 | protected static $readableModes = array('r', 'r+', 'w+', 'a+', 'x+', 'c+'); |
||
29 | |||
30 | /** |
||
31 | * Modes in which a stream is writable |
||
32 | * |
||
33 | * @var array |
||
34 | * @link http://php.net/manual/function.fopen.php |
||
35 | */ |
||
36 | protected static $writableModes = array('r+', 'w', 'w+', 'a', 'a+', 'x', 'x+', 'c', 'c+'); |
||
37 | |||
38 | /** |
||
39 | * The stream underlying the message body |
||
40 | * |
||
41 | * @var resource |
||
42 | */ |
||
43 | protected $stream; |
||
44 | |||
45 | /** |
||
46 | * The underlying stream's metadata |
||
47 | * |
||
48 | * @var null|array |
||
49 | */ |
||
50 | protected $metadata; |
||
51 | |||
52 | /** |
||
53 | * Is the underlying stream readable? |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $readable; |
||
58 | |||
59 | /** |
||
60 | * Is the underlying stream writable? |
||
61 | * |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $writable; |
||
65 | |||
66 | /** |
||
67 | * Is the underlying stream seekable? |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $seekable; |
||
72 | |||
73 | /** |
||
74 | * Create a new message body with the provided stream underlying it. |
||
75 | * |
||
76 | * @param resource $stream |
||
77 | */ |
||
78 | public function __construct($stream) |
||
88 | |||
89 | /** |
||
90 | * Attach a resource to this message body. |
||
91 | * |
||
92 | * @param resource $stream |
||
93 | */ |
||
94 | public function attach($stream) |
||
110 | |||
111 | /** |
||
112 | * Separates any underlying resources from the stream. |
||
113 | * |
||
114 | * After the stream has been detached, the stream is in an unusable state. |
||
115 | * |
||
116 | * @return resource|null Underlying PHP stream, if any |
||
117 | */ |
||
118 | public function detach() |
||
125 | |||
126 | /** |
||
127 | * Check if a stream resource is already attached to this message body. |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function isAttached() |
||
135 | |||
136 | /** |
||
137 | * Set the metadata state information on this object, sourced from the stream metadata. |
||
138 | * |
||
139 | * @param resource $stream |
||
140 | */ |
||
141 | protected function setMetadata($stream) |
||
166 | |||
167 | /** |
||
168 | * Get the size of the stream if known |
||
169 | * |
||
170 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
171 | */ |
||
172 | public function getSize() |
||
181 | |||
182 | /** |
||
183 | * Returns the current position of the file read/write pointer |
||
184 | * |
||
185 | * @return int|bool Position of the file pointer or false on error. |
||
186 | */ |
||
187 | public function tell() |
||
191 | |||
192 | /** |
||
193 | * Returns true if the stream is at the end of the stream. |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function eof() |
||
201 | |||
202 | /** |
||
203 | * Returns whether or not the stream is seekable. |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function isSeekable() |
||
211 | |||
212 | /** |
||
213 | * Seek to a position in the stream. |
||
214 | * |
||
215 | * @link http://www.php.net/manual/en/function.fseek.php |
||
216 | * @param int $offset Stream offset |
||
217 | * @param int $whence Specifies how the cursor position will be calculated |
||
218 | * based on the seek offset. Valid values are identical to the built-in |
||
219 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to |
||
220 | * offset bytes SEEK_CUR: Set position to current location plus offset |
||
221 | * SEEK_END: Set position to end-of-stream plus offset. |
||
222 | * @return bool Returns TRUE on success or FALSE on failure. |
||
223 | */ |
||
224 | public function seek($offset, $whence = SEEK_SET) |
||
230 | |||
231 | /** |
||
232 | * Seek to the beginning of the stream. |
||
233 | * |
||
234 | * If the stream is not seekable, this method will return FALSE, indicating |
||
235 | * failure; otherwise, it will perform a seek(0), and return the status of |
||
236 | * that operation. |
||
237 | * |
||
238 | * @see seek() |
||
239 | * @link http://www.php.net/manual/en/function.fseek.php |
||
240 | * @return bool Returns TRUE on success or FALSE on failure. |
||
241 | */ |
||
242 | public function rewind() |
||
246 | |||
247 | /** |
||
248 | * Returns whether or not the stream is writable. |
||
249 | * |
||
250 | * @return bool |
||
251 | */ |
||
252 | public function isWritable() |
||
256 | |||
257 | /** |
||
258 | * Write data to the stream. |
||
259 | * |
||
260 | * @param string $string The string that is to be written. |
||
261 | * @return int|bool Returns the number of bytes written to the stream on |
||
262 | * success or FALSE on failure. |
||
263 | */ |
||
264 | public function write($string) |
||
268 | |||
269 | /** |
||
270 | * Returns whether or not the stream is readable. |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function isReadable() |
||
278 | |||
279 | /** |
||
280 | * Read data from the stream. |
||
281 | * |
||
282 | * @param int $length Read up to $length bytes from the object and return |
||
283 | * them. Fewer than $length bytes may be returned if underlying stream |
||
284 | * call returns fewer bytes. |
||
285 | * @return string|false Returns the data read from the stream, false if |
||
286 | * unable to read or if an error occurs. |
||
287 | */ |
||
288 | public function read($length) |
||
292 | |||
293 | /** |
||
294 | * Returns the remaining contents in a string |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | public function getContents() |
||
302 | |||
303 | /** |
||
304 | * Get stream metadata as an associative array or retrieve a specific key. |
||
305 | * |
||
306 | * The keys returned are identical to the keys returned from PHP's |
||
307 | * stream_get_meta_data() function. |
||
308 | * |
||
309 | * @link http://php.net/manual/en/function.stream-get-meta-data.php |
||
310 | * @param string $key Specific metadata to retrieve. |
||
311 | * @return array|mixed|null Returns an associative array if no key is |
||
312 | * provided. Returns a specific key value if a key is provided and the |
||
313 | * value is found, or null if the key is not found. |
||
314 | */ |
||
315 | public function getMetadata($key = null) |
||
323 | |||
324 | /** |
||
325 | * Closes the stream and any underlying resources. |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function close() |
||
333 | |||
334 | /** |
||
335 | * Read the entire contents of the message body into a PHP string. |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | public function __toString() |
||
343 | } |
||
344 |