Complex classes like Data_and_files 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 Data_and_files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | trait Data_and_files { |
||
15 | /** |
||
16 | * Data array, similar to `$_POST` |
||
17 | * |
||
18 | * @var array |
||
19 | */ |
||
20 | public $data; |
||
21 | /** |
||
22 | * Normalized files array |
||
23 | * |
||
24 | * Each file item can be either single file or array of files (in contrast with native PHP arrays where each field like `name` become an array) with keys |
||
25 | * `name`, `type`, `size`, `tmp_name`, `stream` and `error` |
||
26 | * |
||
27 | * `name`, `type`, `size` and `error` keys are similar to native PHP fields in `$_FILES`; `tmp_name` might not be temporary file, but file descriptor |
||
28 | * wrapper like `request-file:///file` instead and `stream` is resource like obtained with `fopen('/tmp/xyz', 'rb')` |
||
29 | * |
||
30 | * @var array[] |
||
31 | */ |
||
32 | public $files; |
||
33 | /** |
||
34 | * Data stream resource, similar to `fopen('php://input', 'rb')` |
||
35 | * |
||
36 | * Make sure you're controlling position in stream where you read something, if code in some other place might seek on this stream |
||
37 | * |
||
38 | * Stream is read-only |
||
39 | * |
||
40 | * @var null|resource |
||
41 | */ |
||
42 | public $data_stream; |
||
43 | /** |
||
44 | * `$this->init_server()` assumed to be called already |
||
45 | * |
||
46 | * @param array $data Typically `$_POST` |
||
47 | * @param array[] $files Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys |
||
48 | * `name`, `type`, `size`, `error` and at least one of `tmp_name` or `stream` |
||
49 | * @param null|resource|string $data_stream String, like `php://input` or resource, like `fopen('php://input', 'rb')` with request body, will be parsed for |
||
50 | * data and files if necessary |
||
51 | * @param bool $copy_stream Sometimes data stream can only being read once (like most of times with `php://input`), so it is necessary to |
||
52 | * copy it and store its contents for longer period of time |
||
53 | * |
||
54 | * @throws ExitException |
||
55 | */ |
||
56 | 36 | public function init_data_and_files ($data = [], $files = [], $data_stream = null, $copy_stream = true) { |
|
86 | /** |
||
87 | * Get data item by name |
||
88 | * |
||
89 | * @param string[]|string[][] $name |
||
90 | * |
||
91 | * @return mixed|mixed[]|null Data items (or associative array of data items) if exists or `null` otherwise (in case if `$name` is an array even one |
||
92 | * missing key will cause the whole thing to fail) |
||
93 | */ |
||
94 | 32 | public function data (...$name) { |
|
97 | /** |
||
98 | * Get file item by name |
||
99 | * |
||
100 | * @param string $name |
||
101 | * |
||
102 | * @return array|null File item if exists or `null` otherwise |
||
103 | */ |
||
104 | 2 | public function files ($name) { |
|
107 | /** |
||
108 | * @param array[] $files |
||
109 | * @param string $file_path |
||
110 | * |
||
111 | * @return array[] |
||
112 | */ |
||
113 | 36 | protected function normalize_files ($files, $file_path = '') { |
|
140 | /** |
||
141 | * @param array $file |
||
142 | * @param string $file_path |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 6 | protected function normalize_file ($file, $file_path) { |
|
162 | /** |
||
163 | * Parsing request body for following Content-Type: `application/json`, `application/x-www-form-urlencoded` and `multipart/form-data` |
||
164 | * |
||
165 | * @throws ExitException |
||
166 | */ |
||
167 | 8 | protected function parse_data_stream () { |
|
197 | /** |
||
198 | * Parse content stream |
||
199 | * |
||
200 | * @param resource $stream |
||
201 | * @param string $boundary |
||
202 | * |
||
203 | * @return array[]|false |
||
204 | * |
||
205 | * @throws UnexpectedValueException |
||
206 | * @throws ExitException |
||
207 | */ |
||
208 | 4 | protected function parse_multipart_into_parts ($stream, $boundary) { |
|
277 | /** |
||
278 | * @param resource $stream |
||
279 | * @param array[] $parts |
||
280 | * |
||
281 | * @return array[] |
||
282 | */ |
||
283 | 4 | protected function parse_multipart_analyze_parts ($stream, $parts) { |
|
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | 4 | protected function post_max_size () { |
|
335 | /** |
||
336 | * @return int |
||
337 | */ |
||
338 | 4 | protected function upload_max_file_size () { |
|
342 | /** |
||
343 | * @param int|string $size |
||
344 | * |
||
345 | * @return int |
||
346 | */ |
||
347 | 4 | protected function convert_size_to_bytes ($size) { |
|
358 | /** |
||
359 | * @param resource $stream |
||
360 | * @param string $next_data |
||
361 | * @param string $target |
||
362 | * |
||
363 | * @return array |
||
364 | * |
||
365 | * @throws UnexpectedValueException |
||
366 | */ |
||
367 | 4 | protected function parse_multipart_find ($stream, $next_data, $target) { |
|
387 | /** |
||
388 | * @param string $content |
||
389 | * |
||
390 | * @return array |
||
391 | */ |
||
392 | 4 | protected function parse_multipart_headers ($content) { |
|
414 | /** |
||
415 | * @param array $source |
||
416 | * @param string $name |
||
417 | * @param array|string $value |
||
418 | */ |
||
419 | 4 | protected function parse_multipart_set_target (&$source, $name, $value) { |
|
436 | } |
||
437 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.