| Conditions | 12 |
| Paths | 21 |
| Total Lines | 52 |
| Code Lines | 31 |
| 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 |
||
| 34 | public function validate(mixed $value, object $rule, ValidationContext $context): Result |
||
| 35 | { |
||
| 36 | if (!$rule instanceof TemplateRule) { |
||
| 37 | throw new UnexpectedRuleException(TemplateRule::class, $rule); |
||
| 38 | } |
||
| 39 | $result = new Result(); |
||
| 40 | |||
| 41 | if ($value === 'default') { |
||
| 42 | return $result; |
||
| 43 | } |
||
| 44 | $command = $context->getDataSet(); |
||
| 45 | if (!$command instanceof ObjectDataSet || !$command->getObject() instanceof GeneratorCommandInterface) { |
||
| 46 | // TODO |
||
| 47 | throw new RuntimeException('Unsupported dataset class'); |
||
| 48 | } |
||
| 49 | $selectedGenerator = null; |
||
| 50 | foreach ($this->gii->getGenerators() as $generator) { |
||
| 51 | if ($generator::getCommandClass() === $command->getObject()::class) { |
||
| 52 | $selectedGenerator = $generator; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | // TODO |
||
| 56 | if ($selectedGenerator === null) { |
||
| 57 | throw new RuntimeException('Unknown generator'); |
||
| 58 | } |
||
| 59 | $templates = $this->parametersProvider->getTemplates($selectedGenerator::getId()); |
||
| 60 | |||
| 61 | if ($templates === []) { |
||
| 62 | return $result; |
||
| 63 | } |
||
| 64 | if (!isset($templates[$value])) { |
||
| 65 | $result->addError( |
||
| 66 | message: 'Template "{template}" does not exist. Known templates: {templates}', |
||
| 67 | parameters: [ |
||
| 68 | 'template' => $value, |
||
| 69 | 'templates' => implode(', ', array_keys($templates)), |
||
| 70 | ], |
||
| 71 | ); |
||
| 72 | return $result; |
||
| 73 | } |
||
| 74 | |||
| 75 | $templatePath = $templates[$value]; |
||
| 76 | foreach ($selectedGenerator->getRequiredTemplates() as $template) { |
||
| 77 | if (!is_file($this->aliases->get($templatePath . '/' . $template))) { |
||
| 78 | $result->addError( |
||
| 79 | message: 'Unable to find the required code template file "{template}".', |
||
| 80 | parameters: ['template' => $template], |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | return $result; |
||
| 86 | } |
||
| 88 |