Conditions | 15 |
Paths | 473 |
Total Lines | 83 |
Code Lines | 56 |
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 |
||
55 | protected function execute(InputInterface $input, OutputInterface $output) |
||
56 | { |
||
57 | $name = (string) $input->getOption('name'); |
||
58 | $prefix = $input->getOption('prefix'); |
||
59 | $force = $input->getOption('force'); |
||
60 | $open = $input->getOption('open'); |
||
61 | $editor = $input->getOption('editor'); |
||
62 | |||
63 | try { |
||
64 | // ask |
||
65 | if (empty($name)) { |
||
66 | $name = $this->io->ask('What is the name of the page file?', 'new-page.md'); |
||
67 | $prefix = $this->io->confirm('Add date prefix to the filename?', false); |
||
68 | $open = $this->io->confirm('Do you want open the created file with your editor?', false); |
||
69 | if ($open && !$this->getBuilder()->getConfig()->has('editor')) { |
||
70 | $editor = $this->io->ask('Which editor?'); |
||
71 | } |
||
72 | } |
||
73 | // parse given path name |
||
74 | $nameParts = pathinfo($name); |
||
75 | $dirname = trim($nameParts['dirname'], '.'); |
||
76 | $basename = $nameParts['basename']; |
||
77 | $extension = $nameParts['extension']; |
||
78 | $title = substr($basename, 0, -\strlen(".$extension")); |
||
79 | $filename = $basename; |
||
80 | if (!\in_array($extension, (array) $this->getBuilder()->getConfig()->get('pages.ext'))) { |
||
81 | $title = $filename; |
||
82 | $filename = "$basename.md"; // force a valid extension |
||
83 | } |
||
84 | $title = ucfirst(str_replace('-', ' ', $title)); |
||
85 | $date = date('Y-m-d'); |
||
86 | // date prefix? |
||
87 | $datePrefix = ''; |
||
88 | if ($prefix) { |
||
89 | $datePrefix = sprintf('%s-', $date); |
||
90 | } |
||
91 | // path |
||
92 | $fileRelativePath = sprintf( |
||
93 | '%s%s%s%s%s', |
||
94 | (string) $this->getBuilder()->getConfig()->get('pages.dir'), |
||
95 | DIRECTORY_SEPARATOR, |
||
96 | empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, |
||
97 | $datePrefix, |
||
98 | $filename |
||
99 | ); |
||
100 | $filePath = Util::joinFile($this->getPath(), $fileRelativePath); |
||
101 | |||
102 | // ask to override existing file? |
||
103 | if (Util\File::getFS()->exists($filePath) && !$force) { |
||
104 | $output->writeln(sprintf('<comment>The file "%s" already exists.</comment>', $fileRelativePath)); |
||
105 | if (!$this->io->confirm('Do you want to override it?', false)) { |
||
106 | return 0; |
||
107 | } |
||
108 | } |
||
109 | |||
110 | // creates a new file |
||
111 | $model = $this->findModel(sprintf('%s%s', empty($dirname) ? '' : $dirname . DIRECTORY_SEPARATOR, $filename)); |
||
112 | $fileContent = str_replace( |
||
113 | ['%title%', '%date%'], |
||
114 | [$title, $date], |
||
115 | $model['content'] |
||
116 | ); |
||
117 | Util\File::getFS()->dumpFile($filePath, $fileContent); |
||
118 | $output->writeln(sprintf('<info>File "%s" created (with model "%s").</info>', $fileRelativePath, $model['name'])); |
||
119 | |||
120 | // open editor? |
||
121 | if ($open) { |
||
122 | if ($editor === null) { |
||
123 | if (!$this->getBuilder()->getConfig()->has('editor')) { |
||
124 | $output->writeln('<comment>No editor configured.</comment>'); |
||
125 | |||
126 | return 0; |
||
127 | } |
||
128 | $editor = (string) $this->getBuilder()->getConfig()->get('editor'); |
||
129 | } |
||
130 | $output->writeln(sprintf('<info>Opening file with %s...</info>', ucfirst($editor))); |
||
131 | $this->openEditor($filePath, $editor); |
||
132 | } |
||
133 | } catch (\Exception $e) { |
||
134 | throw new RuntimeException(sprintf($e->getMessage())); |
||
135 | } |
||
136 | |||
137 | return 0; |
||
138 | } |
||
169 |