Conditions | 10 |
Paths | 59 |
Total Lines | 61 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
47 | protected function execute(InputInterface $input, OutputInterface $output) |
||
48 | { |
||
49 | $name = (string) $input->getArgument('name'); |
||
50 | $force = $input->getOption('force'); |
||
51 | $open = $input->getOption('open'); |
||
52 | $prefix = $input->getOption('prefix'); |
||
53 | |||
54 | try { |
||
55 | $nameParts = pathinfo($name); |
||
56 | $dirname = $nameParts['dirname']; |
||
57 | $filename = $nameParts['filename']; |
||
58 | $date = date('Y-m-d'); |
||
59 | $title = $filename; |
||
60 | // date prefix? |
||
61 | $datePrefix = ''; |
||
62 | if ($prefix) { |
||
63 | $datePrefix = sprintf('%s-', $date); |
||
64 | } |
||
65 | // path |
||
66 | $fileRelativePath = sprintf( |
||
67 | '%s/%s%s%s.md', |
||
68 | $this->getBuilder($output)->getConfig()->get('content.dir'), |
||
|
|||
69 | !$dirname ?: $dirname.'/', |
||
70 | $datePrefix, |
||
71 | $filename |
||
72 | ); |
||
73 | $filePath = $this->getPath().'/'.$fileRelativePath; |
||
74 | |||
75 | // file already exists? |
||
76 | if ($this->fs->exists($filePath) && !$force) { |
||
77 | $helper = $this->getHelper('question'); |
||
78 | $question = new ConfirmationQuestion( |
||
79 | sprintf('This page already exists. Do you want to override it? [y/n]', $this->getpath()), |
||
80 | false |
||
81 | ); |
||
82 | if (!$helper->ask($input, $output, $question)) { |
||
83 | return; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // create new file |
||
88 | $fileContent = str_replace( |
||
89 | ['%title%', '%date%'], |
||
90 | [$title, $date], |
||
91 | $this->findModel(sprintf('%s%s', !$dirname ?: $dirname.'/', $filename)) |
||
92 | ); |
||
93 | $this->fs->dumpFile($filePath, $fileContent); |
||
94 | $output->writeln(sprintf('File "%s" created.', $fileRelativePath)); |
||
95 | |||
96 | // open editor? |
||
97 | if ($open) { |
||
98 | if (!$this->hasEditor($output)) { |
||
99 | $output->writeln('<comment>No editor configured.</comment>'); |
||
100 | } |
||
101 | $this->openEditor($output, $filePath); |
||
102 | } |
||
103 | } catch (\Exception $e) { |
||
104 | throw new \Exception(sprintf($e->getMessage())); |
||
105 | } |
||
106 | |||
107 | return 0; |
||
108 | } |
||
176 |