| Conditions | 8 |
| Paths | 10 |
| Total Lines | 56 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 21 | ||
| Bugs | 1 | Features | 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 |
||
| 24 | public function handle(): int |
||
| 25 | { |
||
| 26 | $name = (string) $this->argument('name'); |
||
| 27 | if (realpath(Hyde::path($name)) === realpath(Hyde::path(config('hyde.source_root', '')))) { |
||
| 28 | $this->error("The directory '$name' is already set as the project source root!"); |
||
| 29 | |||
| 30 | return 409; |
||
| 31 | } |
||
| 32 | $this->infoComment('Setting', $name, 'as the project source directory!'); |
||
| 33 | |||
| 34 | $directories = array_unique([ |
||
| 35 | \Hyde\Pages\HtmlPage::$sourceDirectory, |
||
| 36 | \Hyde\Pages\BladePage::$sourceDirectory, |
||
| 37 | \Hyde\Pages\MarkdownPage::$sourceDirectory, |
||
| 38 | \Hyde\Pages\MarkdownPost::$sourceDirectory, |
||
| 39 | \Hyde\Pages\DocumentationPage::$sourceDirectory, |
||
| 40 | ]); |
||
| 41 | |||
| 42 | if (Filesystem::isDirectory($name) && ! Filesystem::isEmptyDirectory($name)) { |
||
| 43 | foreach ($directories as $directory) { |
||
| 44 | $directory = "$name/".basename($directory); |
||
| 45 | if (self::isNonEmptyDirectory(Hyde::path($directory))) { |
||
| 46 | $this->error('Directory already exists!'); |
||
| 47 | |||
| 48 | return 409; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | $this->comment('Creating directory'); |
||
| 54 | Filesystem::ensureDirectoryExists($name); |
||
| 55 | |||
| 56 | $this->comment('Moving source directories'); |
||
| 57 | |||
| 58 | foreach ($directories as $directory) { |
||
| 59 | Filesystem::moveDirectory($directory, "$name/".basename($directory)); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->comment('Updating configuration file'); |
||
| 63 | |||
| 64 | $current = (string) config('hyde.source_root', ''); |
||
| 65 | $search = "'source_root' => '$current',"; |
||
| 66 | |||
| 67 | $config = Filesystem::getContents('config/hyde.php'); |
||
| 68 | if (str_contains($config, $search)) { |
||
| 69 | $config = str_replace($search, "'source_root' => '$name',", $config); |
||
| 70 | Filesystem::putContents('config/hyde.php', $config); |
||
| 71 | } else { |
||
| 72 | $this->error('Automatic configuration update failed, to finalize the change, please set the `source_root` setting to '."'$name'".' in `config/hyde.php`'); |
||
| 73 | } |
||
| 74 | |||
| 75 | // We could also check if there are any more page classes (from packages) and add a note that they may need manual attention |
||
| 76 | |||
| 77 | $this->info('All done!'); |
||
| 78 | |||
| 79 | return Command::SUCCESS; |
||
| 80 | } |
||
| 91 |