Conditions | 20 |
Paths | 1281 |
Total Lines | 84 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
61 | public function add($name, ?string $type = null, array $options = [], array $fieldDescriptionOptions = []): self |
||
62 | { |
||
63 | if (!$this->shouldApply()) { |
||
64 | return $this; |
||
65 | } |
||
66 | |||
67 | if ($name instanceof FormBuilderInterface) { |
||
68 | $fieldName = $name->getName(); |
||
69 | } else { |
||
70 | $fieldName = $name; |
||
71 | } |
||
72 | |||
73 | // "Dot" notation is not allowed as form name, but can be used as property path to access nested data. |
||
74 | if (!$name instanceof FormBuilderInterface && !isset($options['property_path'])) { |
||
75 | $options['property_path'] = $fieldName; |
||
76 | |||
77 | // fix the form name |
||
78 | $fieldName = $this->sanitizeFieldName($fieldName); |
||
79 | } |
||
80 | |||
81 | // change `collection` to `sonata_type_native_collection` form type to |
||
82 | // avoid BC break problems |
||
83 | if ('collection' === $type || SymfonyCollectionType::class === $type) { |
||
84 | $type = CollectionType::class; |
||
85 | } |
||
86 | |||
87 | $label = $fieldName; |
||
88 | |||
89 | $group = $this->addFieldToCurrentGroup($label); |
||
90 | |||
91 | // Try to autodetect type |
||
92 | if ($name instanceof FormBuilderInterface && null === $type) { |
||
93 | $fieldDescriptionOptions['type'] = \get_class($name->getType()->getInnerType()); |
||
94 | } |
||
95 | |||
96 | if (!isset($fieldDescriptionOptions['type']) && \is_string($type)) { |
||
97 | $fieldDescriptionOptions['type'] = $type; |
||
98 | } |
||
99 | |||
100 | if ($group['translation_domain'] && !isset($fieldDescriptionOptions['translation_domain'])) { |
||
101 | $fieldDescriptionOptions['translation_domain'] = $group['translation_domain']; |
||
102 | } |
||
103 | |||
104 | $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance( |
||
105 | $this->admin->getClass(), |
||
106 | $name instanceof FormBuilderInterface ? $name->getName() : $name, |
||
107 | $fieldDescriptionOptions |
||
108 | ); |
||
109 | |||
110 | // Note that the builder var is actually the formContractor: |
||
111 | $this->builder->fixFieldDescription($this->admin, $fieldDescription); |
||
112 | |||
113 | if ($fieldName !== $name) { |
||
114 | $fieldDescription->setName($fieldName); |
||
115 | } |
||
116 | |||
117 | if ($name instanceof FormBuilderInterface) { |
||
118 | $type = null; |
||
119 | $options = []; |
||
120 | } else { |
||
121 | $name = $fieldDescription->getName(); |
||
122 | |||
123 | // Note that the builder var is actually the formContractor: |
||
124 | $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription) ?? [], $options); |
||
125 | |||
126 | // be compatible with mopa if not installed, avoid generating an exception for invalid option |
||
127 | // force the default to false ... |
||
128 | if (!isset($options['label_render'])) { |
||
129 | $options['label_render'] = false; |
||
130 | } |
||
131 | |||
132 | if (!isset($options['label'])) { |
||
133 | $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($name, 'form', 'label'); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | $this->admin->addFormFieldDescription($fieldName, $fieldDescription); |
||
138 | |||
139 | if (!isset($fieldDescriptionOptions['role']) || $this->admin->isGranted($fieldDescriptionOptions['role'])) { |
||
140 | $this->formBuilder->add($name, $type, $options); |
||
141 | } |
||
142 | |||
143 | return $this; |
||
144 | } |
||
145 | |||
260 |