| Conditions | 13 |
| Paths | 29 |
| Total Lines | 61 |
| Code Lines | 42 |
| 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 |
||
| 53 | public function execute(InputInterface $input, OutputInterface $output): int { |
||
| 54 | $fileInput = $input->getArgument('file'); |
||
| 55 | $inputIsId = is_numeric($fileInput); |
||
| 56 | $force = $input->getOption('force'); |
||
| 57 | $node = $this->fileUtils->getNode($fileInput); |
||
| 58 | |||
| 59 | if (!$node) { |
||
| 60 | $output->writeln("<error>file $fileInput not found</error>"); |
||
| 61 | return 1; |
||
| 62 | } |
||
| 63 | |||
| 64 | $deleteConfirmed = $force; |
||
| 65 | if (!$deleteConfirmed) { |
||
| 66 | /** @var QuestionHelper $helper */ |
||
| 67 | $helper = $this->getHelper('question'); |
||
| 68 | $storage = $node->getStorage(); |
||
| 69 | if (!$inputIsId && $storage->instanceOfStorage(SharedStorage::class) && $node->getInternalPath() === '') { |
||
| 70 | /** @var SharedStorage $storage */ |
||
| 71 | [,$user] = explode('/', $fileInput, 3); |
||
| 72 | $question = new ConfirmationQuestion("<info>$fileInput</info> in a shared file, do you want to unshare the file from <info>$user</info> instead of deleting the source file? [Y/n] ", true); |
||
| 73 | if ($helper->ask($input, $output, $question)) { |
||
| 74 | $storage->unshareStorage(); |
||
| 75 | return 0; |
||
| 76 | } else { |
||
| 77 | $node = $storage->getShare()->getNode(); |
||
| 78 | $output->writeln(""); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $filesByUsers = $this->fileUtils->getFilesByUser($node); |
||
| 83 | if (count($filesByUsers) > 1) { |
||
| 84 | $output->writeln("Warning: the provided file is accessible by more than one user"); |
||
| 85 | $output->writeln(" all of the following users will lose access to the file when deleted:"); |
||
| 86 | $output->writeln(""); |
||
| 87 | foreach ($filesByUsers as $user => $filesByUser) { |
||
| 88 | $output->writeln($user . ":"); |
||
| 89 | foreach($filesByUser as $file) { |
||
| 90 | $output->writeln(" - " . $file->getPath()); |
||
| 91 | } |
||
| 92 | } |
||
| 93 | $output->writeln(""); |
||
| 94 | } |
||
| 95 | |||
| 96 | if ($node instanceof Folder) { |
||
| 97 | $maybeContents = " and all it's contents"; |
||
| 98 | } else { |
||
| 99 | $maybeContents = ""; |
||
| 100 | } |
||
| 101 | $question = new ConfirmationQuestion("Delete " . $node->getPath() . $maybeContents . "? [y/N] ", false); |
||
| 102 | $deleteConfirmed = $helper->ask($input, $output, $question); |
||
| 103 | } |
||
| 104 | |||
| 105 | if ($deleteConfirmed) { |
||
| 106 | if ($node->isDeletable()) { |
||
| 107 | $node->delete(); |
||
| 108 | } else { |
||
| 109 | $output->writeln("<error>File cannot be deleted, insufficient permissions.</error>"); |
||
| 110 | } |
||
| 111 | } |
||
| 112 | |||
| 113 | return 0; |
||
| 114 | } |
||
| 117 |