Conditions | 18 |
Paths | 20 |
Total Lines | 82 |
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 |
||
82 | public function execute(InputInterface $input, OutputInterface $output) { |
||
83 | $pathOption = $input->getOption('path'); |
||
84 | $userName = $input->getOption('user'); |
||
85 | |||
86 | if (!$pathOption && !$userName) { |
||
87 | $output->writeln('<info>This operation might take quite some time.</info>'); |
||
88 | } |
||
89 | |||
90 | if ($pathOption && $userName) { |
||
91 | $output->writeln('<error>Please use either path or user exclusively</error>'); |
||
92 | $this->exitStatus = self::EXIT_INVALID_ARGS; |
||
93 | } |
||
94 | |||
95 | $walkFunction = function (Node $node) use ($input, $output) { |
||
96 | $path = $node->getInternalPath(); |
||
97 | $currentChecksums = $node->getChecksum(); |
||
98 | $owner = $node->getOwner()->getUID(); |
||
99 | $storage = $node->getStorage(); |
||
100 | |||
101 | if ($storage->instanceOfStorage(ISharedStorage::class) || $storage->instanceOfStorage(FailedStorage::class)) { |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | if (!self::fileExistsOnDisk($node)) { |
||
106 | $output->writeln("Skipping $owner/$path => File is in file-cache but doesn't exist on storage/disk", OutputInterface::VERBOSITY_VERBOSE); |
||
107 | return; |
||
108 | } |
||
109 | |||
110 | if (!$node->isReadable()) { |
||
111 | $output->writeln("Skipping $owner/$path => File not readable", OutputInterface::VERBOSITY_VERBOSE); |
||
112 | return; |
||
113 | } |
||
114 | |||
115 | // Files without calculated checksum can't cause checksum errors |
||
116 | if (empty($currentChecksums)) { |
||
117 | $output->writeln("Skipping $owner/$path => No Checksum", OutputInterface::VERBOSITY_VERBOSE); |
||
118 | return; |
||
119 | } |
||
120 | |||
121 | $output->writeln("Checking $owner/$path => $currentChecksums", OutputInterface::VERBOSITY_VERBOSE); |
||
122 | $actualChecksums = self::calculateActualChecksums($path, $node->getStorage()); |
||
123 | if ($actualChecksums !== $currentChecksums) { |
||
124 | $output->writeln( |
||
125 | "<info>Mismatch for $owner/$path:\n Filecache:\t$currentChecksums\n Actual:\t$actualChecksums</info>" |
||
126 | ); |
||
127 | |||
128 | $this->exitStatus = self::EXIT_CHECKSUM_ERRORS; |
||
129 | |||
130 | if ($input->getOption('repair')) { |
||
131 | $output->writeln("<info>Repairing $path</info>"); |
||
132 | $this->updateChecksumsForNode($node, $actualChecksums); |
||
133 | $this->exitStatus = self::EXIT_NO_ERRORS; |
||
134 | } |
||
135 | } |
||
136 | }; |
||
137 | |||
138 | $scanUserFunction = function (IUser $user) use ($input, $output, $walkFunction) { |
||
139 | $userFolder = $this->rootFolder->getUserFolder($user->getUID())->getParent(); |
||
140 | $this->walkNodes($userFolder->getDirectoryListing(), $walkFunction); |
||
141 | }; |
||
142 | |||
143 | if ($userName && $this->userManager->userExists($userName)) { |
||
144 | $scanUserFunction($this->userManager->get($userName)); |
||
145 | } elseif ($userName && !$this->userManager->userExists($userName)) { |
||
146 | $output->writeln("<error>User \"$userName\" does not exist</error>"); |
||
147 | $this->exitStatus = self::EXIT_INVALID_ARGS; |
||
148 | } elseif ($input->getOption('path')) { |
||
149 | try { |
||
150 | $node = $this->rootFolder->get($input->getOption('path')); |
||
151 | } catch (NotFoundException $ex) { |
||
152 | $output->writeln("<error>Path \"{$ex->getMessage()}\" not found.</error>"); |
||
153 | $this->exitStatus = self::EXIT_INVALID_ARGS; |
||
154 | return $this->exitStatus; |
||
155 | } |
||
156 | |||
157 | $this->walkNodes([$node], $walkFunction); |
||
158 | } else { |
||
159 | $this->userManager->callForAllUsers($scanUserFunction); |
||
160 | } |
||
161 | |||
162 | return $this->exitStatus; |
||
163 | } |
||
164 | |||
222 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: