Conditions | 16 |
Paths | 31 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
76 | public static function normalizeFiles($files): array |
||
77 | { |
||
78 | if (empty($files)) { |
||
79 | return []; |
||
80 | } |
||
81 | |||
82 | if (is_object($files)) { |
||
83 | if (is_subclass_of($files, self::PSR7_UPLOADED_FILE_CLASS)) { |
||
|
|||
84 | return [self::extractFromUploadedFileInterface($files)]; |
||
85 | } |
||
86 | if (get_class($files) == self::SYMFONY_UPLOADED_FILE_CLASS) { |
||
87 | return [self::extractFromSymfonyFile($files)]; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | // If caller passed in an array of objects (Either PSR7 or Symfony) |
||
92 | if (is_array($files) && is_object(reset($files))) { |
||
93 | if (is_subclass_of(reset($files), self::PSR7_UPLOADED_FILE_CLASS)) { |
||
94 | $result = []; |
||
95 | foreach ($files as $file) { |
||
96 | $result[] = self::extractFromUploadedFileInterface($file); |
||
97 | } |
||
98 | |||
99 | return $result; |
||
100 | } |
||
101 | |||
102 | if (get_class(reset($files)) == self::SYMFONY_UPLOADED_FILE_CLASS) { |
||
103 | $result = []; |
||
104 | foreach ($files as $file) { |
||
105 | $result[] = self::extractFromSymfonyFile($file); |
||
106 | } |
||
107 | |||
108 | return $result; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | // The caller passed $_FILES['some_field_name'] |
||
113 | if (isset($files['name'])) { |
||
114 | // we have a single file |
||
115 | if (! is_array($files['name'])) { |
||
116 | return [$files]; |
||
117 | } else { |
||
118 | // we have list of files, which PHP messes up |
||
119 | return Helper::remapFilesArray($files); |
||
120 | } |
||
121 | } else { |
||
122 | // The caller passed $_FILES |
||
123 | $keys = array_keys($files); |
||
124 | if (isset($keys[0]) && isset($files[$keys[0]]['name'])) { |
||
125 | if (! is_array($files[$keys[0]]['name'])) { |
||
126 | // $files is in the correct format already, even in the |
||
127 | // case it contains a single element. |
||
128 | return $files; |
||
129 | } else { |
||
130 | // we have list of files, which PHP messes up |
||
131 | return Helper::remapFilesArray($files[$keys[0]]); |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | // If we got here, the $file argument is wrong |
||
137 | return []; |
||
138 | } |
||
139 | } |
||
140 |