Conditions | 38 |
Paths | 58 |
Total Lines | 180 |
Lines | 11 |
Ratio | 6.11 % |
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 |
||
125 | public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) { |
||
126 | $outputType = $input->getOption('output'); |
||
127 | $shortView = $input->getOption('short'); |
||
128 | |||
129 | if (\count($mounts) === 0) { |
||
130 | if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
||
131 | $output->writeln('[]'); |
||
132 | } else { |
||
133 | if ($userId === self::ALL) { |
||
134 | $output->writeln("<info>No mounts configured</info>"); |
||
135 | } elseif ($userId) { |
||
136 | $output->writeln("<info>No mounts configured by $userId</info>"); |
||
137 | } else { |
||
138 | $output->writeln("<info>No admin mounts configured</info>"); |
||
139 | } |
||
140 | } |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | if ($shortView) { |
||
145 | $headers = ['Mount ID', 'Mount Point', 'Type']; |
||
146 | } else { |
||
147 | $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; |
||
148 | |||
149 | if (!$userId || $userId === self::ALL) { |
||
150 | $headers[] = 'Applicable Users'; |
||
151 | $headers[] = 'Applicable Groups'; |
||
152 | } |
||
153 | |||
154 | if ($userId === self::ALL) { |
||
155 | $headers[] = 'Type'; |
||
156 | } |
||
157 | |||
158 | if (!$input->getOption('show-password')) { |
||
159 | $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; |
||
160 | foreach ($mounts as $mount) { |
||
161 | $config = $mount->getBackendOptions(); |
||
162 | foreach ($config as $key => $value) { |
||
163 | if (\in_array($key, $hideKeys)) { |
||
164 | $mount->setBackendOption($key, '***'); |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
||
172 | $keys = \array_map(function ($header) { |
||
173 | return \strtolower(\str_replace(' ', '_', $header)); |
||
174 | }, $headers); |
||
175 | |||
176 | if ($shortView) { |
||
177 | $pairs = \array_map(function (IStorageConfig $config) use ($keys, $userId) { |
||
178 | $values = [ |
||
179 | $config->getId(), |
||
180 | $config->getMountPoint(), |
||
181 | $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal' |
||
182 | ]; |
||
183 | |||
184 | return \array_combine($keys, $values); |
||
185 | }, $mounts); |
||
186 | } else { |
||
187 | $pairs = \array_map(function (IStorageConfig $config) use ($keys, $userId) { |
||
188 | $values = [ |
||
189 | $config->getId(), |
||
190 | $config->getMountPoint(), |
||
191 | $config->getBackend()->getStorageClass(), |
||
192 | $config->getAuthMechanism()->getIdentifier(), |
||
193 | $config->getBackendOptions(), |
||
194 | $config->getMountOptions() |
||
195 | ]; |
||
196 | if (!$userId || $userId === self::ALL) { |
||
197 | $values[] = $config->getApplicableUsers(); |
||
198 | $values[] = $config->getApplicableGroups(); |
||
199 | } |
||
200 | View Code Duplication | if ($userId === self::ALL) { |
|
201 | $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; |
||
202 | } |
||
203 | |||
204 | return \array_combine($keys, $values); |
||
205 | }, $mounts); |
||
206 | } |
||
207 | |||
208 | View Code Duplication | if ($outputType === self::OUTPUT_FORMAT_JSON) { |
|
209 | $output->writeln(\json_encode(\array_values($pairs))); |
||
210 | } else { |
||
211 | $output->writeln(\json_encode(\array_values($pairs), JSON_PRETTY_PRINT)); |
||
212 | } |
||
213 | } else { |
||
214 | |||
215 | // default output style |
||
216 | $full = $input->getOption('full'); |
||
217 | $defaultMountOptions = [ |
||
218 | 'encrypt' => true, |
||
219 | 'previews' => true, |
||
220 | 'filesystem_check_changes' => 1, |
||
221 | 'enable_sharing' => false, |
||
222 | 'encoding_compatibility' => false |
||
223 | ]; |
||
224 | $countInvalid = 0; |
||
225 | // In case adding array elements, add them only after the first two (Mount ID / Mount Point) |
||
226 | // and before the last one entry (Type). Necessary for option -s |
||
227 | $rows = \array_map(function (IStorageConfig $config) use ($shortView, $userId, $defaultMountOptions, $full, &$countInvalid) { |
||
228 | if ($config->getBackend() instanceof InvalidBackend || $config->getAuthMechanism() instanceof InvalidAuth) { |
||
229 | $countInvalid++; |
||
230 | } |
||
231 | $storageConfig = $config->getBackendOptions(); |
||
232 | $keys = \array_keys($storageConfig); |
||
233 | $values = \array_values($storageConfig); |
||
234 | |||
235 | if (!$full) { |
||
236 | $values = \array_map(function ($value) { |
||
237 | if (\is_string($value) && \strlen($value) > 32) { |
||
238 | return \substr($value, 0, 6) . '...' . \substr($value, -6, 6); |
||
239 | } else { |
||
240 | return $value; |
||
241 | } |
||
242 | }, $values); |
||
243 | } |
||
244 | |||
245 | $configStrings = \array_map(function ($key, $value) { |
||
246 | return $key . ': ' . \json_encode($value); |
||
247 | }, $keys, $values); |
||
248 | $configString = \implode(', ', $configStrings); |
||
249 | |||
250 | $mountOptions = $config->getMountOptions(); |
||
251 | // hide defaults |
||
252 | foreach ($mountOptions as $key => $value) { |
||
253 | if ($value === $defaultMountOptions[$key]) { |
||
254 | unset($mountOptions[$key]); |
||
255 | } |
||
256 | } |
||
257 | $keys = \array_keys($mountOptions); |
||
258 | $values = \array_values($mountOptions); |
||
259 | |||
260 | $optionsStrings = \array_map(function ($key, $value) { |
||
261 | return $key . ': ' . \json_encode($value); |
||
262 | }, $keys, $values); |
||
263 | $optionsString = \implode(', ', $optionsStrings); |
||
264 | |||
265 | $values = [ |
||
266 | $config->getId(), |
||
267 | $config->getMountPoint(), |
||
268 | $config->getBackend()->getText(), |
||
269 | $config->getAuthMechanism()->getText(), |
||
270 | $configString, |
||
271 | $optionsString |
||
272 | ]; |
||
273 | |||
274 | if (!$userId || $userId === self::ALL) { |
||
275 | $applicableUsers = \implode(', ', $config->getApplicableUsers()); |
||
276 | $applicableGroups = \implode(', ', $config->getApplicableGroups()); |
||
277 | if ($applicableUsers === '' && $applicableGroups === '') { |
||
278 | $applicableUsers = 'All'; |
||
279 | } |
||
280 | $values[] = $applicableUsers; |
||
281 | $values[] = $applicableGroups; |
||
282 | } |
||
283 | // This MUST stay the last entry |
||
284 | View Code Duplication | if ($shortView || $userId === self::ALL) { |
|
285 | $values[] = $config->getType() === IStorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; |
||
286 | } |
||
287 | |||
288 | return $values; |
||
289 | }, $mounts); |
||
290 | |||
291 | $table = new Table($output); |
||
292 | $table->setHeaders($headers); |
||
293 | $table->setRows($this->getColumns($shortView, $rows)); |
||
294 | $table->render(); |
||
295 | |||
296 | if ($countInvalid > 0) { |
||
297 | $output->writeln( |
||
298 | "<error>Number of invalid storages found: $countInvalid.\n" . |
||
299 | "The listed configuration details are likely incomplete.\n" . |
||
300 | "Please make sure that all related apps that provide these storages are enabled or delete these.</error>" |
||
301 | ); |
||
302 | } |
||
303 | } |
||
304 | } |
||
305 | |||
345 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.