| Conditions | 12 |
| Paths | 21 |
| Total Lines | 80 |
| Code Lines | 51 |
| 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 declare(strict_types=1); |
||
| 135 | protected function generateFromModel(string $name): void |
||
| 136 | { |
||
| 137 | $generator = CodeGeneratorFactory::factory($this->generatorName); |
||
| 138 | $model = $name::getFormularium(); |
||
| 139 | $this->info("Starting $name..."); |
||
| 140 | |||
| 141 | $code = $generator->type($model); |
||
| 142 | |||
| 143 | if (!$code) { |
||
| 144 | $this->info('Nothing generated.'); |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | $basepath = $this->option('path') ?: base_path('resources/' . $generator->getName()); |
||
| 149 | if (!is_dir($basepath)) { |
||
| 150 | \Safe\mkdir($basepath, 0777, true); |
||
| 151 | } |
||
| 152 | |||
| 153 | $collection = new GeneratedCollection(); |
||
| 154 | $collection->push( |
||
| 155 | new GeneratedItem( |
||
| 156 | GeneratedItem::TYPE_FRONTEND, |
||
| 157 | $code, |
||
| 158 | $generator->typeFilename($model) |
||
| 159 | ) |
||
| 160 | ); |
||
| 161 | |||
| 162 | if (!$collection->count()) { |
||
| 163 | $this->info('Nothing generated.'); |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | $writtenFiles = $this->writeFiles( |
||
| 168 | $collection, |
||
| 169 | $basepath, |
||
| 170 | function (GeneratedItem $i) { |
||
| 171 | if ((bool)$this->option('overwrite') === true) { |
||
| 172 | return true; |
||
| 173 | } |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | ); |
||
| 177 | $this->info('Files generated.'); |
||
| 178 | |||
| 179 | if ($this->option('prettier') !== null ?: $this->modelariumOptions->getOption('frontend', 'prettier')) { |
||
| 180 | $this->info('Running prettier on generated files.'); |
||
| 181 | $useYarn = file_exists(base_path('yarn.lock')); |
||
| 182 | if ($useYarn) { |
||
| 183 | $command = "cd $basepath && npx prettier --write "; |
||
| 184 | } else { |
||
| 185 | $command = "cd $basepath && yarn prettier --write "; |
||
| 186 | } |
||
| 187 | |||
| 188 | // this runs all prettier commands in parallel. |
||
| 189 | $run = array_reduce( |
||
| 190 | $writtenFiles, |
||
| 191 | function ($carry, $f) use ($command) { |
||
| 192 | return $carry . '(' . $command . $f . ') & '; |
||
| 193 | } |
||
| 194 | ); |
||
| 195 | shell_exec($run . ' wait'); |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($this->option('eslint') !== null ?: $this->modelariumOptions->getOption('frontend', 'eslint')) { |
||
| 199 | $this->info('Running eslint on generated files.'); |
||
| 200 | $useYarn = file_exists(base_path('yarn.lock')); |
||
| 201 | if ($useYarn) { |
||
| 202 | $command = "cd $basepath && npx eslint --fix "; |
||
| 203 | } else { |
||
| 204 | $command = "cd $basepath && yarn eslint --fix "; |
||
| 205 | } |
||
| 206 | |||
| 207 | // this runs all prettier commands in parallel. |
||
| 208 | $run = array_reduce( |
||
| 209 | $writtenFiles, |
||
| 210 | function ($carry, $f) use ($command) { |
||
| 211 | return $carry . '(' . $command . $f . ') & '; |
||
| 212 | } |
||
| 213 | ); |
||
| 214 | shell_exec($run . ' wait'); |
||
| 215 | } |
||
| 218 |