| Conditions | 7 |
| Paths | 11 |
| Total Lines | 82 |
| Code Lines | 52 |
| 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 declare(strict_types=1); |
||
| 161 | private function removeFolders(Collection $foldersToRemove, Path $downloadPath): bool |
||
| 162 | { |
||
| 163 | $nbFoldersToRemove = $foldersToRemove->count(); |
||
| 164 | if (empty($nbFoldersToRemove)) { |
||
| 165 | return false; |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->ui->writeln(PHP_EOL); |
||
| 169 | |||
| 170 | $confirmationDefault = true; |
||
| 171 | |||
| 172 | // If there's less than 10 folders, we can display them |
||
| 173 | if ($nbFoldersToRemove <= 10) { |
||
| 174 | $this->ui->writeln( |
||
| 175 | sprintf( |
||
| 176 | '%sThe script is about to remove the following folders from <info>%s</info>:', |
||
| 177 | $this->ui->indent(), |
||
| 178 | (string) $downloadPath |
||
| 179 | ) |
||
| 180 | ); |
||
| 181 | $this->ui->listing( |
||
| 182 | $foldersToRemove |
||
| 183 | ->map(function (\SplFileInfo $folder) use ($downloadPath) { |
||
| 184 | return sprintf( |
||
| 185 | '<info>%s</info>', |
||
| 186 | str_replace((string) $downloadPath.DIRECTORY_SEPARATOR, '', $folder->getRealPath()) |
||
| 187 | ); |
||
| 188 | }) |
||
| 189 | ->toArray(), |
||
| 190 | 3 |
||
| 191 | ); |
||
| 192 | } else { |
||
| 193 | $confirmationDefault = false; |
||
| 194 | |||
| 195 | $this->ui->write( |
||
| 196 | sprintf( |
||
| 197 | '%sThe script is about to remove <question> %s </question> folders from <info>%s</info>. ', |
||
| 198 | $this->ui->indent(), |
||
| 199 | $nbFoldersToRemove, |
||
| 200 | (string) $downloadPath |
||
| 201 | ) |
||
| 202 | ); |
||
| 203 | } |
||
| 204 | |||
| 205 | $foldersWereRemoved = false; |
||
| 206 | |||
| 207 | $this->ui->write($this->ui->indent()); |
||
| 208 | if ($this->skip() || !$this->ui->confirm($confirmationDefault)) { |
||
| 209 | return $foldersWereRemoved; |
||
| 210 | } |
||
| 211 | |||
| 212 | $errors = []; |
||
| 213 | foreach ($foldersToRemove as $folderToRemove) { |
||
| 214 | $relativeFolderPath = $folderToRemove->getRelativePathname(); |
||
| 215 | |||
| 216 | try { |
||
| 217 | (new Filesystem())->remove($folderToRemove->getRealPath()); |
||
| 218 | |||
| 219 | $foldersWereRemoved = true; |
||
| 220 | |||
| 221 | $this->ui->writeln( |
||
| 222 | sprintf( |
||
| 223 | '%s* The folder <info>%s</info> has been removed.', |
||
| 224 | $this->ui->indent(2), |
||
| 225 | $relativeFolderPath |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | } catch (\Exception $e) { |
||
| 229 | $this->ui->logError( |
||
| 230 | sprintf( |
||
| 231 | '%s* <error>The folder %s could not be removed.</error>', |
||
| 232 | $this->ui->indent(2), |
||
| 233 | $relativeFolderPath |
||
| 234 | ), |
||
| 235 | $errors |
||
| 236 | ); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | $this->ui->displayErrors($errors, 'the removal of folders', 'info', 1); |
||
| 240 | |||
| 241 | return $foldersWereRemoved; |
||
| 242 | } |
||
| 243 | } |
||
| 244 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.