Conditions | 16 |
Paths | 20 |
Total Lines | 86 |
Code Lines | 54 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
Bugs | 1 | Features | 1 |
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); |
||
137 | protected function generateFromModel(string $name): void |
||
138 | { |
||
139 | $composer = FrameworkComposer::create($this->frameworks); |
||
140 | $model = $name::getFormularium(); |
||
141 | $this->info("Starting $name..."); |
||
142 | |||
143 | $generator = new FrontendGenerator($composer, $model, $this->parser); |
||
144 | $collection = $generator->generate(); |
||
145 | |||
146 | if (!$collection->count()) { |
||
147 | $this->info('Nothing generated.'); |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @var string $match |
||
153 | */ |
||
154 | $match = $this->option('overwrite-match'); |
||
155 | if (!empty($match)) { |
||
156 | if (!is_string($match)) { |
||
157 | $this->error('--overwrite-match must be a string'); |
||
158 | throw new Exception(''); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $basepath = base_path('resources/js/components/'); |
||
163 | $writtenFiles = $this->writeFiles( |
||
164 | $collection, |
||
165 | $basepath, |
||
166 | function (GeneratedItem $i) use ($match) { |
||
167 | if ((bool)$this->option('overwrite') === true) { |
||
168 | return true; |
||
169 | } |
||
170 | if ((bool)$this->option('overwrite-graphql') === true) { |
||
171 | if ( |
||
172 | StringUtil::endsWith($i->filename, '.graphql') |
||
173 | ) { |
||
174 | return true; |
||
175 | } elseif (StringUtil::endsWith($i->filename, 'model.js')) { |
||
176 | return true; |
||
177 | } |
||
178 | } |
||
179 | if ($match && mb_strpos($i->filename, $match) !== false) { |
||
180 | return true; |
||
181 | } |
||
182 | return false; |
||
183 | } |
||
184 | ); |
||
185 | $this->info('Files generated.'); |
||
186 | |||
187 | if ($this->option('prettier') !== null ?: $this->modelariumOptions->getOption('frontend', 'prettier')) { |
||
188 | $this->info('Running prettier on generated files.'); |
||
189 | $useYarn = file_exists(base_path('yarn.lock')); |
||
190 | if ($useYarn) { |
||
191 | $command = "cd $basepath && npx prettier --write "; |
||
192 | } else { |
||
193 | $command = "cd $basepath && yarn prettier --write "; |
||
194 | } |
||
195 | |||
196 | // this runs all prettier commands in parallel. |
||
197 | $run = array_reduce( |
||
198 | $writtenFiles, |
||
199 | function ($carry, $f) use ($command) { |
||
200 | return $carry . '(' . $command . $f . ') & '; |
||
201 | } |
||
202 | ); |
||
203 | shell_exec($run . ' wait'); |
||
204 | } |
||
205 | |||
206 | if ($this->option('eslint') !== null ?: $this->modelariumOptions->getOption('frontend', 'eslint')) { |
||
207 | $this->info('Running eslint on generated files.'); |
||
208 | $useYarn = file_exists(base_path('yarn.lock')); |
||
209 | if ($useYarn) { |
||
210 | $command = "cd $basepath && npx eslint --fix "; |
||
211 | } else { |
||
212 | $command = "cd $basepath && yarn eslint --fix "; |
||
213 | } |
||
214 | |||
215 | // this runs all prettier commands in parallel. |
||
216 | $run = array_reduce( |
||
217 | $writtenFiles, |
||
218 | function ($carry, $f) use ($command) { |
||
219 | return $carry . '(' . $command . $f . ') & '; |
||
220 | } |
||
221 | ); |
||
222 | shell_exec($run . ' wait'); |
||
223 | } |
||
226 |