| Conditions | 31 |
| Paths | 28 |
| Total Lines | 143 |
| Code Lines | 104 |
| Lines | 11 |
| Ratio | 7.69 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 118 | public function listMounts($userId, array $mounts, InputInterface $input, OutputInterface $output) { |
||
| 119 | $outputType = $input->getOption('output'); |
||
| 120 | if (count($mounts) === 0) { |
||
| 121 | if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
||
| 122 | $output->writeln('[]'); |
||
| 123 | } else { |
||
| 124 | if ($userId === self::ALL) { |
||
| 125 | $output->writeln("<info>No mounts configured</info>"); |
||
| 126 | } else if ($userId) { |
||
| 127 | $output->writeln("<info>No mounts configured by $userId</info>"); |
||
| 128 | } else { |
||
| 129 | $output->writeln("<info>No admin mounts configured</info>"); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | $headers = ['Mount ID', 'Mount Point', 'Storage', 'Authentication Type', 'Configuration', 'Options']; |
||
| 136 | |||
| 137 | if (!$userId || $userId === self::ALL) { |
||
| 138 | $headers[] = 'Applicable Users'; |
||
| 139 | $headers[] = 'Applicable Groups'; |
||
| 140 | } |
||
| 141 | if ($userId === self::ALL) { |
||
| 142 | $headers[] = 'Type'; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!$input->getOption('show-password')) { |
||
| 146 | $hideKeys = ['password', 'refresh_token', 'token', 'client_secret', 'public_key', 'private_key']; |
||
| 147 | foreach ($mounts as $mount) { |
||
| 148 | $config = $mount->getBackendOptions(); |
||
| 149 | foreach ($config as $key => $value) { |
||
| 150 | if (in_array($key, $hideKeys)) { |
||
| 151 | $mount->setBackendOption($key, '***'); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | if ($outputType === self::OUTPUT_FORMAT_JSON || $outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
||
| 158 | $keys = array_map(function ($header) { |
||
| 159 | return strtolower(str_replace(' ', '_', $header)); |
||
| 160 | }, $headers); |
||
| 161 | |||
| 162 | $pairs = array_map(function (StorageConfig $config) use ($keys, $userId) { |
||
| 163 | $values = [ |
||
| 164 | $config->getId(), |
||
| 165 | $config->getMountPoint(), |
||
| 166 | $config->getBackend()->getStorageClass(), |
||
| 167 | $config->getAuthMechanism()->getIdentifier(), |
||
| 168 | $config->getBackendOptions(), |
||
| 169 | $config->getMountOptions() |
||
| 170 | ]; |
||
| 171 | if (!$userId || $userId === self::ALL) { |
||
| 172 | $values[] = $config->getApplicableUsers(); |
||
| 173 | $values[] = $config->getApplicableGroups(); |
||
| 174 | } |
||
| 175 | View Code Duplication | if ($userId === self::ALL) { |
|
| 176 | $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'admin' : 'personal'; |
||
| 177 | } |
||
| 178 | |||
| 179 | return array_combine($keys, $values); |
||
| 180 | }, $mounts); |
||
| 181 | View Code Duplication | if ($outputType === self::OUTPUT_FORMAT_JSON) { |
|
| 182 | $output->writeln(json_encode(array_values($pairs))); |
||
| 183 | } else { |
||
| 184 | $output->writeln(json_encode(array_values($pairs), JSON_PRETTY_PRINT)); |
||
| 185 | } |
||
| 186 | } else { |
||
| 187 | $full = $input->getOption('full'); |
||
| 188 | $defaultMountOptions = [ |
||
| 189 | 'encrypt' => true, |
||
| 190 | 'previews' => true, |
||
| 191 | 'filesystem_check_changes' => 1, |
||
| 192 | 'enable_sharing' => false, |
||
| 193 | 'encoding_compatibility' => false |
||
| 194 | ]; |
||
| 195 | $rows = array_map(function (StorageConfig $config) use ($userId, $defaultMountOptions, $full) { |
||
| 196 | $storageConfig = $config->getBackendOptions(); |
||
| 197 | $keys = array_keys($storageConfig); |
||
| 198 | $values = array_values($storageConfig); |
||
| 199 | |||
| 200 | if (!$full) { |
||
| 201 | $values = array_map(function ($value) { |
||
| 202 | if (is_string($value) && strlen($value) > 32) { |
||
| 203 | return substr($value, 0, 6) . '...' . substr($value, -6, 6); |
||
| 204 | } else { |
||
| 205 | return $value; |
||
| 206 | } |
||
| 207 | }, $values); |
||
| 208 | } |
||
| 209 | |||
| 210 | $configStrings = array_map(function ($key, $value) { |
||
| 211 | return $key . ': ' . json_encode($value); |
||
| 212 | }, $keys, $values); |
||
| 213 | $configString = implode(', ', $configStrings); |
||
| 214 | |||
| 215 | $mountOptions = $config->getMountOptions(); |
||
| 216 | // hide defaults |
||
| 217 | foreach ($mountOptions as $key => $value) { |
||
| 218 | if ($value === $defaultMountOptions[$key]) { |
||
| 219 | unset($mountOptions[$key]); |
||
| 220 | } |
||
| 221 | } |
||
| 222 | $keys = array_keys($mountOptions); |
||
| 223 | $values = array_values($mountOptions); |
||
| 224 | |||
| 225 | $optionsStrings = array_map(function ($key, $value) { |
||
| 226 | return $key . ': ' . json_encode($value); |
||
| 227 | }, $keys, $values); |
||
| 228 | $optionsString = implode(', ', $optionsStrings); |
||
| 229 | |||
| 230 | $values = [ |
||
| 231 | $config->getId(), |
||
| 232 | $config->getMountPoint(), |
||
| 233 | $config->getBackend()->getText(), |
||
| 234 | $config->getAuthMechanism()->getText(), |
||
| 235 | $configString, |
||
| 236 | $optionsString |
||
| 237 | ]; |
||
| 238 | |||
| 239 | if (!$userId || $userId === self::ALL) { |
||
| 240 | $applicableUsers = implode(', ', $config->getApplicableUsers()); |
||
| 241 | $applicableGroups = implode(', ', $config->getApplicableGroups()); |
||
| 242 | if ($applicableUsers === '' && $applicableGroups === '') { |
||
| 243 | $applicableUsers = 'All'; |
||
| 244 | } |
||
| 245 | $values[] = $applicableUsers; |
||
| 246 | $values[] = $applicableGroups; |
||
| 247 | } |
||
| 248 | View Code Duplication | if ($userId === self::ALL) { |
|
| 249 | $values[] = $config->getType() === StorageConfig::MOUNT_TYPE_ADMIN ? 'Admin' : 'Personal'; |
||
| 250 | } |
||
| 251 | |||
| 252 | return $values; |
||
| 253 | }, $mounts); |
||
| 254 | |||
| 255 | $table = new Table($output); |
||
| 256 | $table->setHeaders($headers); |
||
| 257 | $table->setRows($rows); |
||
| 258 | $table->render(); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 275 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.