| Conditions | 18 |
| Paths | 97 |
| Total Lines | 69 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 56 | public function createValueField($data, $form, $fieldName = 'value') |
||
| 57 | { |
||
| 58 | if ($data->getDefinition() == null) { |
||
| 59 | return false; |
||
| 60 | } |
||
| 61 | |||
| 62 | $attribute = $data; |
||
| 63 | $definition = $attribute->getDefinition(); |
||
| 64 | |||
| 65 | $type = $definition->getType(); |
||
| 66 | $options = $definition->getOptions()->toArray(); |
||
| 67 | $value = $data->getValue(); |
||
| 68 | |||
| 69 | $params = [ |
||
| 70 | 'attr' => $form->getConfig()->getOptions()['attr'], |
||
| 71 | ]; |
||
| 72 | |||
| 73 | if ($type == 'textarea' && !$this->getOption('allow_expanded')) { |
||
| 74 | $type = 'text'; |
||
| 75 | } |
||
| 76 | |||
| 77 | if ($type == 'choice' || $type == 'checkbox' || $type == 'radio') { |
||
| 78 | if (($type == 'checkbox' || $type == 'radio') && $this->getOption('allow_expanded')) { |
||
| 79 | $params['expanded'] = true; |
||
| 80 | } |
||
| 81 | |||
| 82 | if ($this->getOption('all_multiple')) { |
||
| 83 | $params['multiple'] = true; |
||
| 84 | } else { |
||
| 85 | if ($type == 'radio') { |
||
| 86 | $params['multiple'] = false; |
||
| 87 | } elseif ($type == 'checkbox') { |
||
| 88 | if (!is_array($value)) { |
||
| 89 | $value = [ |
||
| 90 | $value => $value, |
||
| 91 | ]; |
||
| 92 | } |
||
| 93 | |||
| 94 | $params['multiple'] = true; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | $params['choices'] = []; |
||
| 99 | |||
| 100 | foreach ($options as $option) { |
||
| 101 | $params['choices'][$option->getValue()] = $option->getName(); |
||
| 102 | } |
||
| 103 | |||
| 104 | $type = 'choice'; |
||
| 105 | } elseif (is_array($value)) { |
||
| 106 | $value = null; |
||
| 107 | } |
||
| 108 | |||
| 109 | $params['required'] = $definition->getRequired(); |
||
| 110 | |||
| 111 | $params['label'] = $definition->getName(); |
||
| 112 | |||
| 113 | if ($definition->getUnit() != "") { |
||
| 114 | $params['label_attr']['unit'] = $definition->getUnit(); |
||
| 115 | } |
||
| 116 | |||
| 117 | if ($definition->getDescription() != "") { |
||
| 118 | $params['label_attr']['help'] = $definition->getDescription(); |
||
| 119 | } |
||
| 120 | |||
| 121 | $params['auto_initialize'] = false; |
||
| 122 | |||
| 123 | $form->add($this->factory->createNamed($fieldName, $type, $value, $params)); |
||
| 124 | } |
||
| 125 | } |
||
| 126 |