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 | * The filesystem to use while uploading a file. |
||
106 | * |
||
107 | * @var string |
||
108 | */ |
||
109 | private $filesystem = self::DEFAULT_FILESYSTEM; |
||
110 | |||
111 | /** |
||
112 | * Holds a list of all normalized paths. |
||
113 | * |
||
114 | * @var string[] |
||
115 | */ |
||
116 | protected static $normalizePathCache = []; |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | public function type() |
||
125 | |||
126 | /** |
||
127 | * Set whether uploaded files should be publicly available. |
||
128 | * |
||
129 | * @param boolean $public Whether uploaded files should be accessible (TRUE) or not (FALSE) from the web root. |
||
130 | * @return self |
||
131 | */ |
||
132 | public function setPublicAccess($public) |
||
138 | |||
139 | /** |
||
140 | * Determine if uploaded files should be publicly available. |
||
141 | * |
||
142 | * @return boolean |
||
143 | */ |
||
144 | public function getPublicAccess() |
||
148 | |||
149 | /** |
||
150 | * Set the destination (directory) where uploaded files are stored. |
||
151 | * |
||
152 | * The path must be relative to the {@see self::basePath()}, |
||
153 | * |
||
154 | * @param string $path The destination directory, relative to project's root. |
||
155 | * @throws InvalidArgumentException If the path is not a string. |
||
156 | * @return self |
||
157 | */ |
||
158 | public function setUploadPath($path) |
||
171 | |||
172 | /** |
||
173 | * Retrieve the destination for the uploaded file(s). |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getUploadPath() |
||
181 | |||
182 | /** |
||
183 | * Set whether existing destinations should be overwritten. |
||
184 | * |
||
185 | * @param boolean $overwrite Whether existing destinations should be overwritten (TRUE) or not (FALSE). |
||
186 | * @return self |
||
187 | */ |
||
188 | public function setOverwrite($overwrite) |
||
194 | |||
195 | /** |
||
196 | * Determine if existing destinations should be overwritten. |
||
197 | * |
||
198 | * @return boolean |
||
199 | */ |
||
200 | public function getOverwrite() |
||
204 | |||
205 | /** |
||
206 | * @param string[] $mimetypes The accepted mimetypes. |
||
207 | * @return self |
||
208 | */ |
||
209 | public function setAcceptedMimetypes(array $mimetypes) |
||
215 | |||
216 | /** |
||
217 | * @return string[] |
||
218 | */ |
||
219 | public function getAcceptedMimetypes() |
||
223 | |||
224 | /** |
||
225 | * Set the MIME type. |
||
226 | * |
||
227 | * @param mixed $type The file MIME type. |
||
228 | * @throws InvalidArgumentException If the MIME type argument is not a string. |
||
229 | * @return FileProperty Chainable |
||
230 | */ |
||
231 | public function setMimetype($type) |
||
249 | |||
250 | /** |
||
251 | * Retrieve the MIME type. |
||
252 | * |
||
253 | * @return string |
||
254 | */ |
||
255 | public function getMimetype() |
||
269 | |||
270 | /** |
||
271 | * Alias of {@see self::getMimetype()}. |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function mimetype() |
||
279 | |||
280 | /** |
||
281 | * Extract the MIME type from the given file. |
||
282 | * |
||
283 | * @uses finfo |
||
284 | * @param string $file The file to check. |
||
285 | * @return string|false Returns the given file's MIME type or FALSE if an error occurred. |
||
286 | */ |
||
287 | public function getMimetypeFor($file) |
||
293 | |||
294 | /** |
||
295 | * Alias of {@see self::getMimetypeFor()}. |
||
296 | * |
||
297 | * @param string $file The file to check. |
||
298 | * @return string|false |
||
299 | */ |
||
300 | public function mimetypeFor($file) |
||
304 | |||
305 | /** |
||
306 | * Set the maximium size accepted for an uploaded files. |
||
307 | * |
||
308 | * @param string|integer $size The maximum file size allowed, in bytes. |
||
309 | * @throws InvalidArgumentException If the size argument is not an integer. |
||
310 | * @return FileProperty Chainable |
||
311 | */ |
||
312 | public function setMaxFilesize($size) |
||
318 | |||
319 | /** |
||
320 | * Retrieve the maximum size accepted for uploaded files. |
||
321 | * |
||
322 | * If null or 0, then no limit. Defaults to 128 MB. |
||
323 | * |
||
324 | * @return integer |
||
325 | */ |
||
326 | public function getMaxFilesize() |
||
334 | |||
335 | /** |
||
336 | * Retrieve the maximum size (in bytes) allowed for an uploaded file |
||
337 | * as configured in {@link http://php.net/manual/en/ini.php `php.ini`}. |
||
338 | * |
||
339 | * @param string|null $iniDirective If $iniDirective is provided, then it is filled with |
||
340 | * the name of the PHP INI directive corresponding to the maximum size allowed. |
||
341 | * @return integer |
||
342 | */ |
||
343 | public function maxFilesizeAllowedByPhp(&$iniDirective = null) |
||
358 | |||
359 | /** |
||
360 | * @param integer $size The file size, in bytes. |
||
361 | * @throws InvalidArgumentException If the size argument is not an integer. |
||
362 | * @return FileProperty Chainable |
||
363 | */ |
||
364 | public function setFilesize($size) |
||
375 | |||
376 | /** |
||
377 | * @return integer |
||
378 | */ |
||
379 | public function getFilesize() |
||
392 | |||
393 | /** |
||
394 | * Alias of {@see self::getFilesize()}. |
||
395 | * |
||
396 | * @return integer |
||
397 | */ |
||
398 | public function filesize() |
||
402 | |||
403 | /** |
||
404 | * @return array |
||
405 | */ |
||
406 | public function validationMethods() |
||
415 | |||
416 | /** |
||
417 | * @return boolean |
||
418 | */ |
||
419 | public function validateAcceptedMimetypes() |
||
449 | |||
450 | /** |
||
451 | * @return boolean |
||
452 | */ |
||
453 | public function validateMaxFilesize() |
||
469 | |||
470 | /** |
||
471 | * Get the SQL type (Storage format) |
||
472 | * |
||
473 | * Stored as `VARCHAR` for max_length under 255 and `TEXT` for other, longer strings |
||
474 | * |
||
475 | * @see StorablePropertyTrait::sqlType() |
||
476 | * @return string The SQL type |
||
477 | */ |
||
478 | public function sqlType() |
||
487 | |||
488 | /** |
||
489 | * @see StorablePropertyTrait::sqlPdoType() |
||
490 | * @return integer |
||
491 | */ |
||
492 | public function sqlPdoType() |
||
496 | |||
497 | /** |
||
498 | * Process file uploads {@see AbstractProperty::save() parsing values}. |
||
499 | * |
||
500 | * @param mixed $val The value, at time of saving. |
||
501 | * @return mixed |
||
502 | */ |
||
503 | public function save($val) |
||
547 | |||
548 | /** |
||
549 | * Process and transfer any data URIs to the filesystem, |
||
550 | * and carry over any pre-processed file paths. |
||
551 | * |
||
552 | * @param mixed $values One or more data URIs, data entries, or processed file paths. |
||
553 | * @return string|string[] One or more paths to the processed uploaded files. |
||
554 | */ |
||
555 | protected function saveDataUploads($values) |
||
576 | |||
577 | /** |
||
578 | * Process and transfer any uploaded files to the filesystem. |
||
579 | * |
||
580 | * @param mixed $files One or more normalized $_FILE entries. |
||
581 | * @return string[] One or more paths to the processed uploaded files. |
||
582 | */ |
||
583 | protected function saveFileUploads($files) |
||
602 | |||
603 | /** |
||
604 | * Finalize any processed files. |
||
605 | * |
||
606 | * @param mixed $saved One or more values, at time of saving. |
||
607 | * @param mixed $default The default value to return. |
||
608 | * @return string|string[] One or more paths to the processed uploaded files. |
||
609 | */ |
||
610 | protected function parseSavedValues($saved, $default = null) |
||
626 | |||
627 | /** |
||
628 | * Upload to filesystem, from data URI. |
||
629 | * |
||
630 | * @param mixed $data A data URI. |
||
631 | * @throws Exception If data content decoding fails. |
||
632 | * @throws InvalidArgumentException If the $data is invalid. |
||
633 | * @return string|null The file path to the uploaded data. |
||
634 | */ |
||
635 | public function dataUpload($data) |
||
695 | |||
696 | /** |
||
697 | * Upload to filesystem. |
||
698 | * |
||
699 | * @link https://github.com/slimphp/Slim/blob/3.12.1/Slim/Http/UploadedFile.php |
||
700 | * Adapted from slim/slim. |
||
701 | * |
||
702 | * @param array $file A single $_FILES entry. |
||
703 | * @throws InvalidArgumentException If the $file is invalid. |
||
704 | * @return string|null The file path to the uploaded file. |
||
705 | */ |
||
706 | public function fileUpload(array $file) |
||
769 | |||
770 | /** |
||
771 | * @param string $filename Optional. The filename to save. If unset, a default filename will be generated. |
||
772 | * @throws Exception If the target path is not writeable. |
||
773 | * @return string |
||
774 | */ |
||
775 | public function uploadTarget($filename = null) |
||
810 | |||
811 | /** |
||
812 | * Checks whether a file or directory exists. |
||
813 | * |
||
814 | * PHP built-in's `file_exists` is only case-insensitive on case-insensitive filesystem (such as Windows) |
||
815 | * This method allows to have the same validation across different platforms / filesystem. |
||
816 | * |
||
817 | * @param string $file The full file to check. |
||
818 | * @param boolean $caseInsensitive Case-insensitive by default. |
||
819 | * @return boolean |
||
820 | */ |
||
821 | public function fileExists($file, $caseInsensitive = true) |
||
847 | |||
848 | /** |
||
849 | * Sanitize a filename by removing characters from a blacklist and escaping dot. |
||
850 | * |
||
851 | * @param string $filename The filename to sanitize. |
||
852 | * @return string The sanitized filename. |
||
853 | */ |
||
854 | public function sanitizeFilename($filename) |
||
865 | |||
866 | /** |
||
867 | * Render the given file to the given pattern. |
||
868 | * |
||
869 | * This method does not rename the given path. |
||
870 | * |
||
871 | * @uses strtr() To replace tokens in the form `{{foobar}}`. |
||
872 | * @param string $from The string being rendered. |
||
873 | * @param string $to The pattern replacing $from. |
||
874 | * @param array|callable $args Extra rename tokens. |
||
875 | * @throws InvalidArgumentException If the given arguments are invalid. |
||
876 | * @throws UnexpectedValueException If the renaming failed. |
||
877 | * @return string Returns the rendered target. |
||
878 | */ |
||
879 | public function renderFileRenamePattern($from, $to, $args = null) |
||
912 | |||
913 | /** |
||
914 | * Generate a new filename from the property. |
||
915 | * |
||
916 | * @return string |
||
917 | */ |
||
918 | public function generateFilename() |
||
929 | |||
930 | /** |
||
931 | * Generate a unique filename. |
||
932 | * |
||
933 | * @param string|array $filename The filename to alter. |
||
934 | * @throws InvalidArgumentException If the given filename is invalid. |
||
935 | * @return string |
||
936 | */ |
||
937 | public function generateUniqueFilename($filename) |
||
960 | |||
961 | /** |
||
962 | * Generate the file extension from the property's value. |
||
963 | * |
||
964 | * @param string $file The file to parse. |
||
965 | * @return string The extension based on the MIME type. |
||
966 | */ |
||
967 | public function generateExtension($file = null) |
||
992 | |||
993 | /** |
||
994 | * @return string |
||
995 | */ |
||
996 | public function getFilesystem() |
||
1000 | |||
1001 | /** |
||
1002 | * @param string $filesystem The file system. |
||
1003 | * @return self |
||
1004 | */ |
||
1005 | public function setFilesystem($filesystem) |
||
1011 | |||
1012 | /** |
||
1013 | * Inject dependencies from a DI Container. |
||
1014 | * |
||
1015 | * @param Container $container A dependencies container instance. |
||
1016 | * @return void |
||
1017 | */ |
||
1018 | protected function setDependencies(Container $container) |
||
1025 | /** |
||
1026 | * Retrieve the path to the storage directory. |
||
1027 | * |
||
1028 | * @return string |
||
1029 | */ |
||
1030 | protected function basePath() |
||
1038 | |||
1039 | /** |
||
1040 | * Converts a php.ini notation for size to an integer. |
||
1041 | * |
||
1042 | * @param mixed $size A php.ini notation for size. |
||
1043 | * @throws InvalidArgumentException If the given parameter is invalid. |
||
1044 | * @return integer Returns the size in bytes. |
||
1045 | */ |
||
1046 | protected function parseIniSize($size) |
||
1068 | |||
1069 | /** |
||
1070 | * Determine if the given file path is am absolute path. |
||
1071 | * |
||
1072 | * Note: Adapted from symfony\filesystem. |
||
1073 | * |
||
1074 | * @see https://github.com/symfony/symfony/blob/v3.2.2/LICENSE |
||
1075 | * |
||
1076 | * @param string $file A file path. |
||
1077 | * @return boolean Returns TRUE if the given path is absolute. Otherwise, returns FALSE. |
||
1078 | */ |
||
1079 | protected function isAbsolutePath($file) |
||
1088 | |||
1089 | /** |
||
1090 | * Determine if the given value is a data URI. |
||
1091 | * |
||
1092 | * @param mixed $val The value to check. |
||
1093 | * @return boolean |
||
1094 | */ |
||
1095 | protected function isDataUri($val) |
||
1099 | |||
1100 | /** |
||
1101 | * Determine if the given value is a data array. |
||
1102 | * |
||
1103 | * @param mixed $val The value to check. |
||
1104 | * @return boolean |
||
1105 | */ |
||
1106 | protected function isDataArr($val) |
||
1110 | |||
1111 | /** |
||
1112 | * Retrieve the rename pattern tokens for the given file. |
||
1113 | * |
||
1114 | * @param string|array $path The string to be parsed or an associative array of information about the file. |
||
1115 | * @param array|callable $args Extra rename tokens. |
||
1116 | * @throws InvalidArgumentException If the given arguments are invalid. |
||
1117 | * @throws UnexpectedValueException If the given path is invalid. |
||
1118 | * @return string Returns the rendered target. |
||
1119 | */ |
||
1120 | private function renamePatternArgs($path, $args = null) |
||
1185 | |||
1186 | /** |
||
1187 | * Retrieve normalized file upload data for this property. |
||
1188 | * |
||
1189 | * @return array A tree of normalized $_FILE entries. |
||
1190 | */ |
||
1191 | public function getUploadedFiles() |
||
1202 | |||
1203 | /** |
||
1204 | * Parse a non-normalized, i.e. $_FILES superglobal, tree of uploaded file data. |
||
1205 | * |
||
1206 | * @link https://github.com/slimphp/Slim/blob/3.12.1/Slim/Http/UploadedFile.php |
||
1207 | * Adapted from slim/slim. |
||
1208 | * |
||
1209 | * @todo Add support for "dot" notation on $searchKey. |
||
1210 | * |
||
1211 | * @param array $uploadedFiles The non-normalized tree of uploaded file data. |
||
1212 | * @param callable $filterCallback If specified, the callback function to used to filter files. |
||
1213 | * @param mixed $searchKey If specified, then only top-level keys containing these values are returned. |
||
1214 | * @return array A tree of normalized $_FILE entries. |
||
1215 | */ |
||
1216 | public static function parseUploadedFiles(array $uploadedFiles, callable $filterCallback = null, $searchKey = null) |
||
1293 | |||
1294 | /** |
||
1295 | * Normalize a file path string so that it can be checked safely. |
||
1296 | * |
||
1297 | * Attempt to avoid invalid encoding bugs by transcoding the path. Then |
||
1298 | * remove any unnecessary path components including '.', '..' and ''. |
||
1299 | * |
||
1300 | * @link https://gist.github.com/thsutton/772287 |
||
1301 | * |
||
1302 | * @param string $path The path to normalise. |
||
1303 | * @param string $encoding The name of the path iconv() encoding. |
||
1304 | * @return string The path, normalised. |
||
1305 | */ |
||
1306 | public static function normalizePath($path, $encoding = 'UTF-8') |
||
1344 | } |
||
1345 |
This method has been deprecated.