Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
43 | class FilesDataHandler |
||
44 | { |
||
45 | /** |
||
46 | * @var Request |
||
47 | */ |
||
48 | protected $request; |
||
49 | |||
50 | /** |
||
51 | * @var array |
||
52 | */ |
||
53 | protected $file_objects; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $initialized = false; |
||
59 | |||
60 | /** |
||
61 | * FilesDataHandler constructor. |
||
62 | * @param Request $request |
||
63 | */ |
||
64 | public function __construct(Request $request) |
||
68 | |||
69 | /** |
||
70 | * @since $VID:$ |
||
71 | * @return array with a similar structure to $_POST and $_GET (ie, it can be multi-dimensional) but the "leaf" |
||
72 | * nodes are all of type FileSubmissionInterface |
||
73 | * @throws CollectionDetailsException |
||
74 | * @throws CollectionLoaderException |
||
75 | * @throws UnexpectedValueException |
||
76 | */ |
||
77 | public function getFileObjects() |
||
84 | |||
85 | /** |
||
86 | * Sets up the file objects from the request's $_FILES data. |
||
87 | * @since $VID:$ |
||
88 | * @throws UnexpectedValueException |
||
89 | */ |
||
90 | protected function initialize() |
||
104 | |||
105 | /** |
||
106 | * Detects if $_FILES is a weird multi-dimensional array that needs fixing or not. |
||
107 | * @since $VID:$ |
||
108 | * @param $files_data |
||
109 | * @return bool |
||
110 | * @throws UnexpectedValueException |
||
111 | */ |
||
112 | protected function isStrangeFilesArray($files_data) |
||
145 | |||
146 | /** |
||
147 | * Takes into account that $_FILES does a weird thing when you have hierarchical form names (eg `<input type="file" |
||
148 | * name="my[hierarchical][form]">`): it leaves the top-level form part alone, but replaces the SECOND part with |
||
149 | * "name", "size", "tmp_name", etc. So that file's data is located at "my[name][hierarchical][form]", |
||
150 | * "my[size][hierarchical][form]", "my[tmp_name][hierarchical][form]", etc. It's really weird. |
||
151 | * @since $VID:$ |
||
152 | * @param $files_data |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function fixFilesDataArray($files_data) |
||
175 | |||
176 | /** |
||
177 | * Recursively explores the array until it finds a leaf node, and tacks `$type` as a final index in front of it. |
||
178 | * @since $VID:$ |
||
179 | * @param $data either 'name', 'tmp_name', 'size', or 'error' |
||
180 | * @param $type |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function organizeFilesData($data, $type) |
||
195 | |||
196 | /** |
||
197 | * Takes the organized $_FILES array (where all file info is located at the same spot as you'd expect an input |
||
198 | * to be in $_GET or $_POST, with all the file's data located side-by-side in an array) and creates a |
||
199 | * multi-dimensional array of FileSubmissionInterface objects. |
||
200 | * @since $VID:$ |
||
201 | * @param $organized_files |
||
202 | * @return array |
||
203 | * @throws UnexpectedValueException |
||
204 | */ |
||
205 | protected function createFileObjects($organized_files) |
||
233 | } |
||
234 | // End of file FilesDataHandler.php |
||
236 |