Complex classes like MultipartStreamBuilder 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 MultipartStreamBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class MultipartStreamBuilder |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var HttplugStreamFactory|StreamFactoryInterface |
||
| 23 | */ |
||
| 24 | private $streamFactory; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var MimetypeHelper |
||
| 28 | */ |
||
| 29 | private $mimetypeHelper; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $boundary; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array Element where each Element is an array with keys ['contents', 'headers'] |
||
| 38 | */ |
||
| 39 | private $data = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param HttplugStreamFactory|StreamFactoryInterface|null $streamFactory |
||
| 43 | */ |
||
| 44 | 15 | public function __construct($streamFactory = null) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Add a resource to the Multipart Stream. |
||
| 77 | * |
||
| 78 | * @param string|resource|\Psr\Http\Message\StreamInterface $resource the filepath, resource or StreamInterface of the data |
||
| 79 | * @param array $headers additional headers array: ['header-name' => 'header-value'] |
||
| 80 | * |
||
| 81 | * @return MultipartStreamBuilder |
||
| 82 | */ |
||
| 83 | 13 | public function addData($resource, array $headers = []) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Add a resource to the Multipart Stream. |
||
| 93 | * |
||
| 94 | * @param string $name the formpost name |
||
| 95 | * @param string|resource|StreamInterface $resource |
||
| 96 | * @param array $options { |
||
| 97 | * |
||
| 98 | * @var array $headers additional headers ['header-name' => 'header-value'] |
||
| 99 | * @var string $filename |
||
| 100 | * } |
||
| 101 | * |
||
| 102 | * @return MultipartStreamBuilder |
||
| 103 | */ |
||
| 104 | 14 | public function addResource($name, $resource, array $options = []) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Build the stream. |
||
| 129 | * |
||
| 130 | * @return StreamInterface |
||
| 131 | */ |
||
| 132 | 13 | public function build() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * Add extra headers if they are missing. |
||
| 172 | * |
||
| 173 | * @param string $name |
||
| 174 | * @param string $filename |
||
| 175 | * @param array &$headers |
||
| 176 | */ |
||
| 177 | 13 | private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Get the headers formatted for the HTTP message. |
||
| 206 | * |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | 12 | private function getHeaders(array $headers) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Check if header exist. |
||
| 221 | * |
||
| 222 | * @param string $key case insensitive |
||
| 223 | * |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | 13 | private function hasHeader(array $headers, $key) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Get the boundary that separates the streams. |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | 13 | public function getBoundary() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $boundary |
||
| 254 | * |
||
| 255 | * @return MultipartStreamBuilder |
||
| 256 | */ |
||
| 257 | 2 | public function setBoundary($boundary) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return MimetypeHelper |
||
| 266 | */ |
||
| 267 | 6 | private function getMimetypeHelper() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * If you have custom file extension you may overwrite the default MimetypeHelper with your own. |
||
| 278 | * |
||
| 279 | * @return MultipartStreamBuilder |
||
| 280 | */ |
||
| 281 | public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Reset and clear all stored data. This allows you to use builder for a subsequent request. |
||
| 290 | * |
||
| 291 | * @return MultipartStreamBuilder |
||
| 292 | */ |
||
| 293 | 1 | public function reset() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Gets the filename from a given path. |
||
| 303 | * |
||
| 304 | * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. |
||
| 305 | * |
||
| 306 | * @author Drupal 8.2 |
||
| 307 | * |
||
| 308 | * @param string $path |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | 6 | private function basename($path) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @param string|resource|StreamInterface $resource |
||
| 331 | * |
||
| 332 | * @return StreamInterface |
||
| 333 | */ |
||
| 334 | 14 | private function createStream($resource) |
|
| 355 | } |
||
| 356 |