Complex classes like ServerRequestCreator 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 ServerRequestCreator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | final class ServerRequestCreator implements ServerRequestCreatorInterface |
||
21 | { |
||
22 | private $serverRequestFactory; |
||
23 | |||
24 | private $uriFactory; |
||
25 | |||
26 | private $uploadedFileFactory; |
||
27 | |||
28 | private $streamFactory; |
||
29 | |||
30 | 29 | public function __construct( |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 8 | public function fromGlobals(): ServerRequestInterface |
|
46 | { |
||
47 | 8 | $server = $_SERVER; |
|
48 | 8 | if (false === isset($server['REQUEST_METHOD'])) { |
|
49 | 1 | $server['REQUEST_METHOD'] = 'GET'; |
|
50 | } |
||
51 | |||
52 | 8 | $headers = \function_exists('getallheaders') ? getallheaders() : static::getHeadersFromServer($_SERVER); |
|
53 | |||
54 | 8 | $post = null; |
|
55 | 8 | if ('POST' === $this->getMethodFromEnv($server)) { |
|
56 | 7 | foreach ($headers as $headerName => $headerValue) { |
|
57 | 6 | if (true === \is_int($headerName) || 'content-type' !== \strtolower($headerName)) { |
|
58 | continue; |
||
59 | } |
||
60 | 6 | if (\in_array( |
|
61 | 6 | \strtolower(\trim(\explode(';', $headerValue, 2)[0])), |
|
62 | 6 | ['application/x-www-form-urlencoded', 'multipart/form-data'] |
|
63 | )) { |
||
64 | 5 | $post = $_POST; |
|
65 | |||
66 | 6 | break; |
|
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | 8 | return $this->fromArrays($server, $headers, $_COOKIE, $_GET, $post, $_FILES, \fopen('php://input', 'r') ?: null); |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 16 | public function fromArrays(array $server, array $headers = [], array $cookie = [], array $get = [], ?array $post = null, array $files = [], $body = null): ServerRequestInterface |
|
114 | |||
115 | /** |
||
116 | * Implementation from Laminas\Diactoros\marshalHeadersFromSapi(). |
||
117 | */ |
||
118 | 10 | public static function getHeadersFromServer(array $server): array |
|
151 | |||
152 | 16 | private function getMethodFromEnv(array $environment): string |
|
160 | |||
161 | 16 | private function getUriFromEnvWithHTTP(array $environment): UriInterface |
|
170 | |||
171 | /** |
||
172 | * Return an UploadedFile instance array. |
||
173 | * |
||
174 | * @param array $files A array which respect $_FILES structure |
||
175 | * |
||
176 | * @return UploadedFileInterface[] |
||
177 | * |
||
178 | * @throws \InvalidArgumentException for unrecognized values |
||
179 | */ |
||
180 | 16 | private function normalizeFiles(array $files): array |
|
198 | |||
199 | /** |
||
200 | * Create and return an UploadedFile instance from a $_FILES specification. |
||
201 | * |
||
202 | * If the specification represents an array of values, this method will |
||
203 | * delegate to normalizeNestedFileSpec() and return that return value. |
||
204 | * |
||
205 | * @param array $value $_FILES struct |
||
206 | * |
||
207 | * @return array|UploadedFileInterface |
||
208 | */ |
||
209 | 5 | private function createUploadedFileFromSpec(array $value) |
|
233 | |||
234 | /** |
||
235 | * Normalize an array of file specifications. |
||
236 | * |
||
237 | * Loops through all nested files and returns a normalized array of |
||
238 | 1 | * UploadedFileInterface instances. |
|
239 | * |
||
240 | 1 | * @return UploadedFileInterface[] |
|
241 | */ |
||
242 | 1 | private function normalizeNestedFileSpec(array $files = []): array |
|
259 | |||
260 | /** |
||
261 | 26 | * Create a new uri from server variable. |
|
262 | * |
||
263 | 26 | * @param array $server typically $_SERVER or similar structure |
|
264 | */ |
||
265 | 26 | private function createUriFromArray(array $server): UriInterface |
|
303 | } |
||
304 |