| Conditions | 6 |
| Paths | 12 |
| Total Lines | 51 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 25 | public function handle(): int |
||
| 26 | { |
||
| 27 | $this->title('Creating a new post!'); |
||
| 28 | |||
| 29 | $this->line( |
||
| 30 | $this->argument('title') |
||
| 31 | ? '<info>Selected title: '.$this->argument('title')."</info>\n" |
||
| 32 | : 'Please enter the title of the post, it will be used to generate the slug.' |
||
| 33 | ); |
||
| 34 | |||
| 35 | $title = $this->argument('title') |
||
| 36 | ?? $this->ask('What is the title of the post?') |
||
| 37 | ?? 'My New Post'; |
||
| 38 | |||
| 39 | $this->line('Tip: You can just hit return to use the defaults.'); |
||
| 40 | $description = $this->ask('Write a short post excerpt/description'); |
||
| 41 | $author = $this->ask('What is your (the author\'s) name?'); |
||
| 42 | $category = $this->ask('What is the primary category of the post?'); |
||
| 43 | |||
| 44 | $this->info('Creating a post with the following details:'); |
||
| 45 | $creator = new CreatesNewMarkdownPostFile( |
||
| 46 | $title, |
||
| 47 | $description, |
||
| 48 | $category, |
||
| 49 | $author |
||
| 50 | ); |
||
| 51 | |||
| 52 | foreach ($creator->toArray() as $key => $value) { |
||
| 53 | $this->line(sprintf('%s: %s', ucwords($key), $value)); |
||
| 54 | } |
||
| 55 | $this->line("Identifier: {$creator->getIdentifier()}"); |
||
| 56 | |||
| 57 | if (! $this->confirm('Do you wish to continue?', true)) { |
||
| 58 | $this->info('Aborting.'); |
||
| 59 | |||
| 60 | return 130; |
||
| 61 | } |
||
| 62 | |||
| 63 | try { |
||
| 64 | $path = $creator->save($this->option('force')); |
||
|
|
|||
| 65 | $this->info("Post created! File is saved to $path"); |
||
| 66 | |||
| 67 | return Command::SUCCESS; |
||
| 68 | } catch (Exception $exception) { |
||
| 69 | $this->error('Something went wrong when trying to save the file!'); |
||
| 70 | $this->warn($exception->getMessage()); |
||
| 71 | if ($exception->getCode() === 409) { |
||
| 72 | $this->comment('If you want to overwrite the file supply the --force flag.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | return (int) $exception->getCode(); |
||
| 76 | } |
||
| 79 |