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