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:
Complex classes like FileProperty 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 FileProperty, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class FileProperty extends AbstractProperty |
||
24 | { |
||
25 | const DEFAULT_PUBLIC_ACCESS = false; |
||
26 | const DEFAULT_UPLOAD_PATH = 'uploads/'; |
||
27 | const DEFAULT_FILESYSTEM = 'public'; |
||
28 | const DEFAULT_OVERWRITE = false; |
||
29 | const ERROR_MESSAGES = [ |
||
30 | UPLOAD_ERR_OK => 'There is no error, the file uploaded with success', |
||
31 | UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini', |
||
32 | UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive'. |
||
33 | 'that was specified in the HTML form', |
||
34 | UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded', |
||
35 | UPLOAD_ERR_NO_FILE => 'No file was uploaded', |
||
36 | UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder', |
||
37 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk', |
||
38 | UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Whether uploaded files should be accessible from the web root. |
||
43 | * |
||
44 | * @var boolean |
||
45 | */ |
||
46 | private $publicAccess = self::DEFAULT_PUBLIC_ACCESS; |
||
47 | |||
48 | /** |
||
49 | * The relative path to the storage directory. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $uploadPath = self::DEFAULT_UPLOAD_PATH; |
||
54 | |||
55 | /** |
||
56 | * The base path for the Charcoal installation. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $basePath; |
||
61 | |||
62 | /** |
||
63 | * The path to the public / web directory. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $publicPath; |
||
68 | |||
69 | /** |
||
70 | * Whether existing destinations should be overwritten. |
||
71 | * |
||
72 | * @var boolean |
||
73 | */ |
||
74 | private $overwrite = self::DEFAULT_OVERWRITE; |
||
75 | |||
76 | /** |
||
77 | * Collection of accepted MIME types. |
||
78 | * |
||
79 | * @var string[] |
||
80 | */ |
||
81 | private $acceptedMimetypes; |
||
82 | |||
83 | /** |
||
84 | * Current file mimetype |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $mimetype; |
||
89 | |||
90 | /** |
||
91 | * Maximum allowed file size, in bytes. |
||
92 | * |
||
93 | * @var integer |
||
94 | */ |
||
95 | private $maxFilesize; |
||
96 | |||
97 | /** |
||
98 | * Current file size, in bytes. |
||
99 | * |
||
100 | * @var integer |
||
101 | */ |
||
102 | private $filesize; |
||
103 | |||
104 | /** |
||
105 | * @var string |
||
106 | */ |
||
107 | private $fallbackFilename; |
||
108 | |||
109 | /** |
||
110 | * The filesystem to use while uploading a file. |
||
111 | * |
||
112 | * @var string |
||
113 | */ |
||
114 | private $filesystem = self::DEFAULT_FILESYSTEM; |
||
115 | |||
116 | /** |
||
117 | * Holds a list of all normalized paths. |
||
118 | * |
||
119 | * @var string[] |
||
120 | */ |
||
121 | protected static $normalizePathCache = []; |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | public function type() |
||
130 | |||
131 | /** |
||
132 | * Set whether uploaded files should be publicly available. |
||
133 | * |
||
134 | * @param boolean $public Whether uploaded files should be accessible (TRUE) or not (FALSE) from the web root. |
||
135 | * @return self |
||
136 | */ |
||
137 | public function setPublicAccess($public) |
||
143 | |||
144 | /** |
||
145 | * Determine if uploaded files should be publicly available. |
||
146 | * |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function getPublicAccess() |
||
153 | |||
154 | /** |
||
155 | * Set the destination (directory) where uploaded files are stored. |
||
156 | * |
||
157 | * The path must be relative to the {@see self::basePath()}, |
||
158 | * |
||
159 | * @param string $path The destination directory, relative to project's root. |
||
160 | * @throws InvalidArgumentException If the path is not a string. |
||
161 | * @return self |
||
162 | */ |
||
163 | public function setUploadPath($path) |
||
176 | |||
177 | /** |
||
178 | * Retrieve the destination for the uploaded file(s). |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getUploadPath() |
||
186 | |||
187 | /** |
||
188 | * Set whether existing destinations should be overwritten. |
||
189 | * |
||
190 | * @param boolean $overwrite Whether existing destinations should be overwritten (TRUE) or not (FALSE). |
||
191 | * @return self |
||
192 | */ |
||
193 | public function setOverwrite($overwrite) |
||
199 | |||
200 | /** |
||
201 | * Determine if existing destinations should be overwritten. |
||
202 | * |
||
203 | * @return boolean |
||
204 | */ |
||
205 | public function getOverwrite() |
||
209 | |||
210 | /** |
||
211 | * Sets the acceptable MIME types for uploaded files. |
||
212 | * |
||
213 | * @param mixed $types One or many MIME types. |
||
214 | * @throws InvalidArgumentException If the $types argument is not NULL or a list. |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setAcceptedMimetypes($types) |
||
236 | |||
237 | /** |
||
238 | * Determines if any acceptable MIME types are defined. |
||
239 | * |
||
240 | * @return boolean |
||
241 | */ |
||
242 | public function hasAcceptedMimetypes() |
||
250 | |||
251 | /** |
||
252 | * Retrieves a list of acceptable MIME types for uploaded files. |
||
253 | * |
||
254 | * @return string[] |
||
255 | */ |
||
256 | public function getAcceptedMimetypes() |
||
264 | |||
265 | /** |
||
266 | * Retrieves the default list of acceptable MIME types for uploaded files. |
||
267 | * |
||
268 | * This method should be overriden. |
||
269 | * |
||
270 | * @return string[] |
||
271 | */ |
||
272 | public function getDefaultAcceptedMimetypes() |
||
276 | |||
277 | /** |
||
278 | * Set the MIME type. |
||
279 | * |
||
280 | * @param mixed $type The file MIME type. |
||
281 | * @throws InvalidArgumentException If the MIME type argument is not a string. |
||
282 | * @return FileProperty Chainable |
||
283 | */ |
||
284 | public function setMimetype($type) |
||
300 | |||
301 | /** |
||
302 | * Retrieve the MIME type of the property value. |
||
303 | * |
||
304 | * @todo Refactor to support multilingual/multiple files. |
||
305 | * |
||
306 | * @return integer Returns the MIME type for the first value. |
||
307 | */ |
||
308 | public function getMimetype() |
||
327 | |||
328 | /** |
||
329 | * Extract the MIME type from the given file. |
||
330 | * |
||
331 | * @param string $file The file to check. |
||
332 | * @return integer|null Returns the file's MIME type, |
||
333 | * or NULL in case of an error or the file is missing. |
||
334 | */ |
||
335 | public function getMimetypeFor($file) |
||
353 | |||
354 | /** |
||
355 | * Set the maximium size accepted for an uploaded files. |
||
356 | * |
||
357 | * @param string|integer $size The maximum file size allowed, in bytes. |
||
358 | * @throws InvalidArgumentException If the size argument is not an integer. |
||
359 | * @return FileProperty Chainable |
||
360 | */ |
||
361 | public function setMaxFilesize($size) |
||
367 | |||
368 | /** |
||
369 | * Retrieve the maximum size accepted for uploaded files. |
||
370 | * |
||
371 | * If null or 0, then no limit. Defaults to 128 MB. |
||
372 | * |
||
373 | * @return integer |
||
374 | */ |
||
375 | public function getMaxFilesize() |
||
383 | |||
384 | /** |
||
385 | * Retrieve the maximum size (in bytes) allowed for an uploaded file |
||
386 | * as configured in {@link http://php.net/manual/en/ini.php `php.ini`}. |
||
387 | * |
||
388 | * @param string|null $iniDirective If $iniDirective is provided, then it is filled with |
||
389 | * the name of the PHP INI directive corresponding to the maximum size allowed. |
||
390 | * @return integer |
||
391 | */ |
||
392 | public function maxFilesizeAllowedByPhp(&$iniDirective = null) |
||
407 | |||
408 | /** |
||
409 | * @param integer $size The file size, in bytes. |
||
410 | * @throws InvalidArgumentException If the size argument is not an integer. |
||
411 | * @return FileProperty Chainable |
||
412 | */ |
||
413 | public function setFilesize($size) |
||
424 | |||
425 | /** |
||
426 | * Retrieve the size of the property value. |
||
427 | * |
||
428 | * @todo Refactor to support multilingual/multiple files. |
||
429 | * |
||
430 | * @return integer Returns the size in bytes for the first value. |
||
431 | */ |
||
432 | public function getFilesize() |
||
451 | |||
452 | /** |
||
453 | * Extract the size of the given file. |
||
454 | * |
||
455 | * @param string $file The file to check. |
||
456 | * @return integer|null Returns the file size in bytes, |
||
457 | * or NULL in case of an error or the file is missing. |
||
458 | */ |
||
459 | public function getFilesizeFor($file) |
||
476 | |||
477 | /** |
||
478 | * Convert number of bytes to largest human-readable unit. |
||
479 | * |
||
480 | * @param integer $bytes Number of bytes. |
||
481 | * @param integer $decimals Precision of number of decimal places. Default 0. |
||
482 | * @return string|null Returns the formatted number or NULL. |
||
483 | */ |
||
484 | public function formatFilesize($bytes, $decimals = 2) |
||
502 | |||
503 | /** |
||
504 | * @return array |
||
505 | */ |
||
506 | public function validationMethods() |
||
515 | |||
516 | /** |
||
517 | * Validates the MIME types for the property's value(s). |
||
518 | * |
||
519 | * @return boolean Returns TRUE if all values are valid. |
||
520 | * Otherwise, returns FALSE and reports issues. |
||
521 | */ |
||
522 | View Code Duplication | public function validateMimetypes() |
|
561 | |||
562 | /** |
||
563 | * Validates the file sizes for the property's value(s). |
||
564 | * |
||
565 | * @return boolean Returns TRUE if all values are valid. |
||
566 | * Otherwise, returns FALSE and reports issues. |
||
567 | */ |
||
568 | View Code Duplication | public function validateFilesizes() |
|
607 | |||
608 | /** |
||
609 | * Parse a multi-dimensional array of value(s) into a single level. |
||
610 | * |
||
611 | * This method flattens a value object that is "l10n" or "multiple". |
||
612 | * Empty or duplicate values are removed. |
||
613 | * |
||
614 | * @param mixed $value A multi-dimensional variable. |
||
615 | * @return string[] The array of values. |
||
616 | */ |
||
617 | public function parseValAsFileList($value) |
||
639 | |||
640 | /** |
||
641 | * Get the SQL type (Storage format) |
||
642 | * |
||
643 | * Stored as `VARCHAR` for max_length under 255 and `TEXT` for other, longer strings |
||
644 | * |
||
645 | * @see StorablePropertyTrait::sqlType() |
||
646 | * @return string The SQL type |
||
647 | */ |
||
648 | public function sqlType() |
||
657 | |||
658 | /** |
||
659 | * @see StorablePropertyTrait::sqlPdoType() |
||
660 | * @return integer |
||
661 | */ |
||
662 | public function sqlPdoType() |
||
666 | |||
667 | /** |
||
668 | * Process file uploads {@see AbstractProperty::save() parsing values}. |
||
669 | * |
||
670 | * @param mixed $val The value, at time of saving. |
||
671 | * @return mixed |
||
672 | */ |
||
673 | public function save($val) |
||
717 | |||
718 | /** |
||
719 | * Process and transfer any data URIs to the filesystem, |
||
720 | * and carry over any pre-processed file paths. |
||
721 | * |
||
722 | * @param mixed $values One or more data URIs, data entries, or processed file paths. |
||
723 | * @return string|string[] One or more paths to the processed uploaded files. |
||
724 | */ |
||
725 | protected function saveDataUploads($values) |
||
758 | |||
759 | /** |
||
760 | * Process and transfer any uploaded files to the filesystem. |
||
761 | * |
||
762 | * @param mixed $files One or more normalized $_FILE entries. |
||
763 | * @return string[] One or more paths to the processed uploaded files. |
||
764 | */ |
||
765 | protected function saveFileUploads($files) |
||
797 | |||
798 | /** |
||
799 | * Finalize any processed files. |
||
800 | * |
||
801 | * @param mixed $saved One or more values, at time of saving. |
||
802 | * @param mixed $default The default value to return. |
||
803 | * @return string|string[] One or more paths to the processed uploaded files. |
||
804 | */ |
||
805 | protected function parseSavedValues($saved, $default = null) |
||
821 | |||
822 | /** |
||
823 | * Upload to filesystem, from data URI. |
||
824 | * |
||
825 | * @param mixed $data A data URI. |
||
826 | * @throws Exception If data content decoding fails. |
||
827 | * @throws InvalidArgumentException If the input $data is invalid. |
||
828 | * @throws Exception If the upload fails or the $data is bad. |
||
829 | * @return string|null The file path to the uploaded data. |
||
830 | */ |
||
831 | public function dataUpload($data) |
||
908 | |||
909 | /** |
||
910 | * Upload to filesystem. |
||
911 | * |
||
912 | * @link https://github.com/slimphp/Slim/blob/3.12.1/Slim/Http/UploadedFile.php |
||
913 | * Adapted from slim/slim. |
||
914 | * |
||
915 | * @param array $file A single $_FILES entry. |
||
916 | * @throws InvalidArgumentException If the input $file is invalid. |
||
917 | * @throws Exception If the upload fails or the $file is bad. |
||
918 | * @return string|null The file path to the uploaded file. |
||
919 | */ |
||
920 | public function fileUpload(array $file) |
||
980 | |||
981 | /** |
||
982 | * Parse the uploaded file path. |
||
983 | * |
||
984 | * This method will create the file's directory path and will sanitize the file's name |
||
985 | * or generate a unique name if none provided (such as data URIs). |
||
986 | * |
||
987 | * @param string|null $filename Optional. The filename to save as. |
||
988 | * If NULL, a default filename will be generated. |
||
989 | * @return string |
||
990 | */ |
||
991 | public function uploadTarget($filename = null) |
||
1017 | |||
1018 | /** |
||
1019 | * Checks whether a file or directory exists. |
||
1020 | * |
||
1021 | * PHP built-in's `file_exists` is only case-insensitive on |
||
1022 | * a case-insensitive filesystem (such as Windows). This method allows |
||
1023 | * to have the same validation across different platforms / filesystems. |
||
1024 | * |
||
1025 | * @param string $file The full file to check. |
||
1026 | * @param boolean $caseInsensitive Case-insensitive by default. |
||
1027 | * @return boolean |
||
1028 | */ |
||
1029 | public function fileExists($file, $caseInsensitive = true) |
||
1057 | |||
1058 | /** |
||
1059 | * Sanitize a filename by removing characters from a blacklist and escaping dot. |
||
1060 | * |
||
1061 | * @param string $filename The filename to sanitize. |
||
1062 | * @throws Exception If the filename is invalid. |
||
1063 | * @return string The sanitized filename. |
||
1064 | */ |
||
1065 | public function sanitizeFilename($filename) |
||
1082 | |||
1083 | /** |
||
1084 | * Render the given file to the given pattern. |
||
1085 | * |
||
1086 | * This method does not rename the given path. |
||
1087 | * |
||
1088 | * @uses strtr() To replace tokens in the form `{{foobar}}`. |
||
1089 | * @param string $from The string being rendered. |
||
1090 | * @param string $to The pattern replacing $from. |
||
1091 | * @param array|callable $args Extra rename tokens. |
||
1092 | * @throws InvalidArgumentException If the given arguments are invalid. |
||
1093 | * @throws UnexpectedValueException If the renaming failed. |
||
1094 | * @return string Returns the rendered target. |
||
1095 | */ |
||
1096 | public function renderFileRenamePattern($from, $to, $args = null) |
||
1129 | |||
1130 | /** |
||
1131 | * Generate a new filename from the property. |
||
1132 | * |
||
1133 | * @param string|null $extension An extension to append to the generated filename. |
||
1134 | * @return string |
||
1135 | */ |
||
1136 | public function generateFilename($extension = null) |
||
1147 | |||
1148 | /** |
||
1149 | * Generate a unique filename. |
||
1150 | * |
||
1151 | * @param string|array $filename The filename to alter. |
||
1152 | * @throws InvalidArgumentException If the given filename is invalid. |
||
1153 | * @return string |
||
1154 | */ |
||
1155 | public function generateUniqueFilename($filename) |
||
1178 | |||
1179 | /** |
||
1180 | * Generate the file extension from the property value. |
||
1181 | * |
||
1182 | * @todo Refactor to support multilingual/multiple files. |
||
1183 | * |
||
1184 | * @return string Returns the file extension based on the MIME type for the first value. |
||
1185 | */ |
||
1186 | public function generateExtension() |
||
1192 | |||
1193 | /** |
||
1194 | * Generate a file extension from the given file path. |
||
1195 | * |
||
1196 | * @param string $file The file to parse. |
||
1197 | * @return string|null The extension based on the file's MIME type. |
||
1198 | */ |
||
1199 | public function generateExtensionFromFile($file) |
||
1226 | |||
1227 | /** |
||
1228 | * Generate a file extension from the given MIME type. |
||
1229 | * |
||
1230 | * @param string $type The MIME type to parse. |
||
1231 | * @return string|null The extension based on the MIME type. |
||
1232 | */ |
||
1233 | public function generateExtensionFromMimeType($type) |
||
1241 | |||
1242 | /** |
||
1243 | * Resolve the file extension from the given MIME type. |
||
1244 | * |
||
1245 | * This method should be overriden to provide available extensions. |
||
1246 | * |
||
1247 | * @param string $type The MIME type to resolve. |
||
1248 | * @return string|null The extension based on the MIME type. |
||
1249 | */ |
||
1250 | protected function resolveExtensionFromMimeType($type) |
||
1259 | |||
1260 | /** |
||
1261 | * @param mixed $fallback The fallback filename. |
||
1262 | * @return self |
||
1263 | */ |
||
1264 | public function setFallbackFilename($fallback) |
||
1269 | |||
1270 | /** |
||
1271 | * @return Translation|null |
||
1272 | */ |
||
1273 | public function getFallbackFilename() |
||
1281 | |||
1282 | /** |
||
1283 | * @return string |
||
1284 | */ |
||
1285 | public function getFilesystem() |
||
1289 | |||
1290 | /** |
||
1291 | * @param string $filesystem The file system. |
||
1292 | * @return self |
||
1293 | */ |
||
1294 | public function setFilesystem($filesystem) |
||
1300 | |||
1301 | /** |
||
1302 | * Inject dependencies from a DI Container. |
||
1303 | * |
||
1304 | * @param Container $container A dependencies container instance. |
||
1305 | * @return void |
||
1306 | */ |
||
1307 | protected function setDependencies(Container $container) |
||
1314 | |||
1315 | /** |
||
1316 | * Retrieve the base path to the storage directory. |
||
1317 | * |
||
1318 | * @return string |
||
1319 | */ |
||
1320 | protected function basePath() |
||
1328 | |||
1329 | /** |
||
1330 | * Build the path for a named route including the base path. |
||
1331 | * |
||
1332 | * The {@see self::basePath() base path} will be prepended to the given $path. |
||
1333 | * |
||
1334 | * If the given $path does not start with the {@see self::getUploadPath() upload path}, |
||
1335 | * it will be prepended. |
||
1336 | * |
||
1337 | * @param string $path The end path. |
||
1338 | * @return string |
||
1339 | */ |
||
1340 | protected function pathFor($path) |
||
1352 | |||
1353 | /** |
||
1354 | * Attempts to create the upload path. |
||
1355 | * |
||
1356 | * @throws Exception If the upload path is unavailable. |
||
1357 | * @return void |
||
1358 | */ |
||
1359 | protected function assertValidUploadPath() |
||
1380 | |||
1381 | /** |
||
1382 | * Converts a php.ini notation for size to an integer. |
||
1383 | * |
||
1384 | * @param mixed $size A php.ini notation for size. |
||
1385 | * @throws InvalidArgumentException If the given parameter is invalid. |
||
1386 | * @return integer Returns the size in bytes. |
||
1387 | */ |
||
1388 | protected function parseIniSize($size) |
||
1410 | |||
1411 | /** |
||
1412 | * Determine if the given MIME type is acceptable. |
||
1413 | * |
||
1414 | * @param string $type A MIME type. |
||
1415 | * @param string[] $accepted One or many acceptable MIME types. |
||
1416 | * Defaults to the property's "acceptedMimetypes". |
||
1417 | * @return boolean Returns TRUE if the MIME type is acceptable. |
||
1418 | * Otherwise, returns FALSE. |
||
1419 | */ |
||
1420 | protected function isAcceptedMimeType($type, array $accepted = null) |
||
1432 | |||
1433 | /** |
||
1434 | * Determine if the given file size is acceptable. |
||
1435 | * |
||
1436 | * @param integer $size Number of bytes. |
||
1437 | * @param integer $max The maximum number of bytes allowed. |
||
1438 | * Defaults to the property's "maxFilesize". |
||
1439 | * @return boolean Returns TRUE if the size is acceptable. |
||
1440 | * Otherwise, returns FALSE. |
||
1441 | */ |
||
1442 | protected function isAcceptedFilesize($size, $max = null) |
||
1454 | |||
1455 | /** |
||
1456 | * Determine if the given file path is an absolute path. |
||
1457 | * |
||
1458 | * Note: Adapted from symfony\filesystem. |
||
1459 | * |
||
1460 | * @see https://github.com/symfony/symfony/blob/v3.2.2/LICENSE |
||
1461 | * |
||
1462 | * @param string $file A file path. |
||
1463 | * @return boolean Returns TRUE if the given path is absolute. Otherwise, returns FALSE. |
||
1464 | */ |
||
1465 | protected function isAbsolutePath($file) |
||
1476 | |||
1477 | /** |
||
1478 | * Determine if the given value is a data URI. |
||
1479 | * |
||
1480 | * @param mixed $val The value to check. |
||
1481 | * @return boolean |
||
1482 | */ |
||
1483 | protected function isDataUri($val) |
||
1487 | |||
1488 | /** |
||
1489 | * Determine if the given value is a data array. |
||
1490 | * |
||
1491 | * @param mixed $val The value to check. |
||
1492 | * @return boolean |
||
1493 | */ |
||
1494 | protected function isDataArr($val) |
||
1498 | |||
1499 | /** |
||
1500 | * Retrieve the rename pattern tokens for the given file. |
||
1501 | * |
||
1502 | * @param string|array $path The string to be parsed or an associative array of information about the file. |
||
1503 | * @param array|callable $args Extra rename tokens. |
||
1504 | * @throws InvalidArgumentException If the given arguments are invalid. |
||
1505 | * @throws UnexpectedValueException If the given path is invalid. |
||
1506 | * @return string Returns the rendered target. |
||
1507 | */ |
||
1508 | private function renamePatternArgs($path, $args = null) |
||
1574 | |||
1575 | /** |
||
1576 | * Retrieve normalized file upload data for this property. |
||
1577 | * |
||
1578 | * @return array A tree of normalized $_FILE entries. |
||
1579 | */ |
||
1580 | public function getUploadedFiles() |
||
1591 | |||
1592 | /** |
||
1593 | * Parse a non-normalized, i.e. $_FILES superglobal, tree of uploaded file data. |
||
1594 | * |
||
1595 | * @link https://github.com/slimphp/Slim/blob/3.12.1/Slim/Http/UploadedFile.php |
||
1596 | * Adapted from slim/slim. |
||
1597 | * |
||
1598 | * @todo Add support for "dot" notation on $searchKey. |
||
1599 | * |
||
1600 | * @param array $uploadedFiles The non-normalized tree of uploaded file data. |
||
1601 | * @param callable $filterCallback If specified, the callback function to used to filter files. |
||
1602 | * @param mixed $searchKey If specified, then only top-level keys containing these values are returned. |
||
1603 | * @return array A tree of normalized $_FILE entries. |
||
1604 | */ |
||
1605 | public static function parseUploadedFiles(array $uploadedFiles, callable $filterCallback = null, $searchKey = null) |
||
1682 | |||
1683 | /** |
||
1684 | * Normalize a file path string so that it can be checked safely. |
||
1685 | * |
||
1686 | * Attempt to avoid invalid encoding bugs by transcoding the path. Then |
||
1687 | * remove any unnecessary path components including '.', '..' and ''. |
||
1688 | * |
||
1689 | * @link https://gist.github.com/thsutton/772287 |
||
1690 | * |
||
1691 | * @param string $path The path to normalise. |
||
1692 | * @param string $encoding The name of the path iconv() encoding. |
||
1693 | * @return string The path, normalised. |
||
1694 | */ |
||
1695 | public static function normalizePath($path, $encoding = 'UTF-8') |
||
1733 | } |
||
1734 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.