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 StreamFactory|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', 'filename'] |
||
38 | */ |
||
39 | private $data = []; |
||
40 | |||
41 | /** |
||
42 | * @param StreamFactory|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 $name the formpost name |
||
79 | * @param string|resource|StreamInterface $resource |
||
80 | * @param array $options { |
||
81 | * |
||
82 | * @var array $headers additional headers ['header-name' => 'header-value'] |
||
83 | * @var string $filename |
||
84 | * } |
||
85 | * |
||
86 | * @return MultipartStreamBuilder |
||
87 | */ |
||
88 | 14 | public function addResource($name, $resource, array $options = []) |
|
111 | |||
112 | /** |
||
113 | * Build the stream. |
||
114 | * |
||
115 | * @return StreamInterface |
||
116 | */ |
||
117 | 13 | public function build() |
|
142 | |||
143 | /** |
||
144 | * Add extra headers if they are missing. |
||
145 | * |
||
146 | * @param string $name |
||
147 | * @param StreamInterface $stream |
||
148 | * @param string $filename |
||
149 | * @param array &$headers |
||
150 | */ |
||
151 | 13 | private function prepareHeaders($name, StreamInterface $stream, $filename, array &$headers) |
|
177 | |||
178 | /** |
||
179 | * Get the headers formatted for the HTTP message. |
||
180 | * |
||
181 | * @param array $headers |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | 12 | private function getHeaders(array $headers) |
|
194 | |||
195 | /** |
||
196 | * Check if header exist. |
||
197 | * |
||
198 | * @param array $headers |
||
199 | * @param string $key case insensitive |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 13 | private function hasHeader(array $headers, $key) |
|
214 | |||
215 | /** |
||
216 | * Get the boundary that separates the streams. |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | 13 | public function getBoundary() |
|
228 | |||
229 | /** |
||
230 | * @param string $boundary |
||
231 | * |
||
232 | * @return MultipartStreamBuilder |
||
233 | */ |
||
234 | 2 | public function setBoundary($boundary) |
|
240 | |||
241 | /** |
||
242 | * @return MimetypeHelper |
||
243 | */ |
||
244 | 6 | private function getMimetypeHelper() |
|
252 | |||
253 | /** |
||
254 | * If you have custom file extension you may overwrite the default MimetypeHelper with your own. |
||
255 | * |
||
256 | * @param MimetypeHelper $mimetypeHelper |
||
257 | * |
||
258 | * @return MultipartStreamBuilder |
||
259 | */ |
||
260 | public function setMimetypeHelper(MimetypeHelper $mimetypeHelper) |
||
266 | |||
267 | /** |
||
268 | * Reset and clear all stored data. This allows you to use builder for a subsequent request. |
||
269 | * |
||
270 | * @return MultipartStreamBuilder |
||
271 | */ |
||
272 | 1 | public function reset() |
|
279 | |||
280 | /** |
||
281 | * Gets the filename from a given path. |
||
282 | * |
||
283 | * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character. |
||
284 | * |
||
285 | * @author Drupal 8.2 |
||
286 | * |
||
287 | * @param string $path |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | 6 | private function basename($path) |
|
307 | |||
308 | /** |
||
309 | * @param string|resource|StreamInterface $resource |
||
310 | * |
||
311 | * @return StreamInterface |
||
312 | */ |
||
313 | 14 | private function createStream($resource) |
|
334 | } |
||
335 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..