| Conditions | 8 |
| Paths | 12 |
| Total Lines | 67 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 76 | public function create($fieldName, array $configuration = []) |
||
| 77 | { |
||
| 78 | $resolver = new OptionsResolver(); |
||
| 79 | $resolver->setDefaults([ |
||
| 80 | 'type' => 'string', |
||
| 81 | 'options' => [], |
||
| 82 | ]); |
||
| 83 | // set allowed fields type from tagged services |
||
| 84 | $resolver->setAllowedValues('type', array_keys($this->fieldsMapping)); |
||
| 85 | $resolver->setAllowedTypes('type', 'string'); |
||
| 86 | $resolver->setAllowedTypes('options', 'array'); |
||
| 87 | |||
| 88 | // resolve options |
||
| 89 | $configuration = $resolver->resolve($configuration); |
||
| 90 | |||
| 91 | // for collection of fields, we resolve the configuration of each item |
||
| 92 | if ($configuration['type'] == 'collection') { |
||
| 93 | $items = []; |
||
| 94 | |||
| 95 | foreach ($configuration['options'] as $itemFieldName => $itemFieldConfiguration) { |
||
| 96 | |||
| 97 | // configuration should be an array |
||
| 98 | if (!$itemFieldConfiguration) { |
||
| 99 | $itemFieldConfiguration = []; |
||
| 100 | } |
||
| 101 | // type should exists |
||
| 102 | if (!array_key_exists('type', $configuration)) { |
||
| 103 | throw new Exception("Missing type configuration for field {$itemFieldName}"); |
||
| 104 | } |
||
| 105 | // create collection item |
||
| 106 | $items[] = $this->create($itemFieldName, $itemFieldConfiguration); |
||
| 107 | } |
||
| 108 | // add created item to the field options |
||
| 109 | $configuration['options'] = [ |
||
| 110 | 'fields' => $items, |
||
| 111 | ]; |
||
| 112 | } |
||
| 113 | // instanciate field |
||
| 114 | $fieldClass = $this->getFieldMapping($configuration['type']); |
||
| 115 | $field = new $fieldClass(); |
||
| 116 | |||
| 117 | if (!($field instanceof FieldInterface)) { |
||
| 118 | throw new Exception("Field class {$fieldClass} must implements " . FieldInterface::class); |
||
| 119 | } |
||
| 120 | $field->setName($fieldName); |
||
| 121 | $field->setConfiguration($this->configuration); |
||
| 122 | |||
| 123 | if ($field instanceof TranslatableFieldInterface) { |
||
| 124 | $field->setTranslator($this->translator); |
||
| 125 | } |
||
| 126 | if ($field instanceof TwigFieldInterface) { |
||
| 127 | $field->setTwig($this->twig); |
||
| 128 | } |
||
| 129 | // clear revolver from previous default configuration |
||
| 130 | $resolver->clear(); |
||
| 131 | |||
| 132 | // configure field default options |
||
| 133 | $field->configureOptions($resolver); |
||
| 134 | // resolve options |
||
| 135 | $options = $resolver->resolve($configuration['options']); |
||
| 136 | |||
| 137 | // set options and value |
||
| 138 | $field->setOptions($options); |
||
| 139 | |||
| 140 | |||
| 141 | return $field; |
||
| 142 | } |
||
| 143 | |||
| 161 |