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 |
||
| 79 | * The filepath, resource or StreamInterface of the data. |
||
| 80 | * @param array $headers |
||
| 81 | * Additional headers array: ['header-name' => 'header-value']. |
||
| 82 | * |
||
| 83 | * @return MultipartStreamBuilder |
||
| 84 | */ |
||
| 85 | 13 | public function addData($resource, array $headers = []) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Add a resource to the Multipart Stream. |
||
| 95 | * |
||
| 96 | * @param string $name the formpost name |
||
| 97 | * @param string|resource|StreamInterface $resource |
||
| 98 | * @param array $options { |
||
| 99 | * |
||
| 100 | * @var array $headers additional headers ['header-name' => 'header-value'] |
||
| 101 | * @var string $filename |
||
| 102 | * } |
||
| 103 | * |
||
| 104 | * @return MultipartStreamBuilder |
||
| 105 | */ |
||
| 106 | 14 | public function addResource($name, $resource, array $options = []) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Build the stream. |
||
| 131 | * |
||
| 132 | * @return StreamInterface |
||
| 133 | */ |
||
| 134 | 13 | public function build() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Add extra headers if they are missing. |
||
| 162 | * |
||
| 163 | * @param string $name |
||
| 164 | * @param string $filename |
||
| 165 | * @param array &$headers |
||
| 166 | */ |
||
| 167 | 13 | private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Get the headers formatted for the HTTP message. |
||
| 196 | * |
||
| 197 | * @return string |
||
| 198 | */ |
||
| 199 | 12 | private function getHeaders(array $headers) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Check if header exist. |
||
| 211 | * |
||
| 212 | * @param string $key case insensitive |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 13 | private function hasHeader(array $headers, $key) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get the boundary that separates the streams. |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | 13 | public function getBoundary() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param string $boundary |
||
| 244 | * |
||
| 245 | * @return MultipartStreamBuilder |
||
| 246 | */ |
||
| 247 | 2 | public function setBoundary($boundary) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return MimetypeHelper |
||
| 256 | */ |
||
| 257 | 6 | private function getMimetypeHelper() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * If you have custom file extension you may overwrite the default MimetypeHelper with your own. |
||
| 268 | * |
||
| 269 | * @return MultipartStreamBuilder |
||
| 270 | */ |
||
| 271 | public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Reset and clear all stored data. This allows you to use builder for a subsequent request. |
||
| 280 | * |
||
| 281 | * @return MultipartStreamBuilder |
||
| 282 | */ |
||
| 283 | 1 | public function reset() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Gets the filename from a given path. |
||
| 293 | * |
||
| 294 | * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. |
||
| 295 | * |
||
| 296 | * @author Drupal 8.2 |
||
| 297 | * |
||
| 298 | * @param string $path |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | 6 | private function basename($path) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param string|resource|StreamInterface $resource |
||
| 321 | * |
||
| 322 | * @return StreamInterface |
||
| 323 | */ |
||
| 324 | 14 | private function createStream($resource) |
|
| 345 | } |
||
| 346 |