Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | final class Request implements RequestInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var ServerRequestCreatorInterface|null |
||
15 | */ |
||
16 | private static $serverRequestCreator = null; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | private static $bufferSize = 10485760; // 10 MB |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private static $uploadDir = null; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $uploadedFiles = []; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | private $params; |
||
37 | |||
38 | /** |
||
39 | * @var resource |
||
40 | */ |
||
41 | private $stdin; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param array $params The FastCGI server params as an associative array |
||
47 | * @param resource $stdin The FastCGI stdin data as a stream resource |
||
48 | */ |
||
49 | public function __construct(array $params, $stdin) |
||
61 | |||
62 | public static function setServerRequestCreator(ServerRequestCreatorInterface $serverRequestCreator): void |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function getParams() |
||
74 | |||
75 | /** |
||
76 | * Remove all uploaded files |
||
77 | */ |
||
78 | public function cleanUploadedFiles(): void |
||
84 | |||
85 | /** |
||
86 | * Set a buffer size to read uploaded files |
||
87 | */ |
||
88 | public static function setBufferSize(int $size): void |
||
92 | |||
93 | public static function getBufferSize(): int |
||
97 | |||
98 | public static function setUploadDir(string $dir): void |
||
102 | |||
103 | public static function getUploadDir(): string |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function getQuery() |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function getPost() |
||
152 | |||
153 | private function parseMultipartFormData($stream, $boundary) { |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function getCookies() |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function getStdin() |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function getServerRequest() |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function getHttpFoundationRequest() |
||
277 | |||
278 | /** |
||
279 | * Add a file to the $files array |
||
280 | */ |
||
281 | private function addFile(array &$files, string $fieldName, string $filename, string $tmpPath, string $mimeType, bool $err): void |
||
312 | } |
||
313 |
If you suppress an error, we recommend checking for the error condition explicitly: