Conditions | 14 |
Paths | 10 |
Total Lines | 85 |
Code Lines | 56 |
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 |
||
94 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
95 | { |
||
96 | $options = []; |
||
97 | $options['sonata_field_description'] = $fieldDescription; |
||
98 | |||
99 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8 |
||
100 | if (in_array($type, [ |
||
101 | 'sonata_type_model', |
||
102 | 'sonata_type_model_list', |
||
103 | 'sonata_type_model_hidden', |
||
104 | 'sonata_type_model_autocomplete', |
||
105 | ], true) || $this->checkFormClass($type, [ |
||
106 | ModelType::class, |
||
107 | ModelTypeList::class, |
||
108 | ModelListType::class, |
||
109 | ModelHiddenType::class, |
||
110 | ModelAutocompleteType::class, |
||
111 | ])) { |
||
112 | if ('list' === $fieldDescription->getOption('edit')) { |
||
113 | throw new \LogicException( |
||
114 | 'The ``Sonata\AdminBundle\Form\Type\ModelType`` type does not accept an ``edit`` option anymore,' |
||
115 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
120 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
121 | |||
122 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8 |
||
123 | if ('sonata_type_model_autocomplete' === $type || $this->checkFormClass($type, [ModelAutocompleteType::class])) { |
||
124 | if (!$fieldDescription->getAssociationAdmin()) { |
||
125 | throw new \RuntimeException(sprintf( |
||
126 | 'The current field `%s` is not linked to an admin.' |
||
127 | .' Please create one for the target entity: `%s`', |
||
128 | $fieldDescription->getName(), |
||
129 | $fieldDescription->getTargetEntity() |
||
130 | )); |
||
131 | } |
||
132 | } |
||
133 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8 |
||
134 | } elseif ('sonata_type_admin' === $type || $this->checkFormClass($type, [AdminType::class])) { |
||
135 | if (!$fieldDescription->getAssociationAdmin()) { |
||
136 | throw new \RuntimeException(sprintf( |
||
137 | 'The current field `%s` is not linked to an admin.' |
||
138 | .' Please create one for the target entity : `%s`', |
||
139 | $fieldDescription->getName(), |
||
140 | $fieldDescription->getTargetEntity() |
||
141 | )); |
||
142 | } |
||
143 | |||
144 | if (!in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE, ClassMetadataInfo::MANY])) { |
||
145 | throw new \RuntimeException(sprintf( |
||
146 | 'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One.' |
||
147 | .' Maybe you want `sonata_type_collection` instead?', |
||
148 | $fieldDescription->getName() |
||
149 | )); |
||
150 | } |
||
151 | |||
152 | // set sensitive default value to have a component working fine out of the box |
||
153 | $options['btn_add'] = false; |
||
154 | $options['delete'] = false; |
||
155 | |||
156 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
157 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
158 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony 2.8 |
||
159 | } elseif ('sonata_type_collection' === $type || $this->checkFormClass($type, [CollectionType::class])) { |
||
160 | if (!$fieldDescription->getAssociationAdmin()) { |
||
161 | throw new \RuntimeException(sprintf( |
||
162 | 'The current field `%s` is not linked to an admin.' |
||
163 | .' Please create one for the target entity : `%s`', |
||
164 | $fieldDescription->getName(), |
||
165 | $fieldDescription->getTargetEntity() |
||
166 | )); |
||
167 | } |
||
168 | |||
169 | $options['type'] = AdminType::class; |
||
170 | $options['modifiable'] = true; |
||
171 | $options['type_options'] = [ |
||
172 | 'sonata_field_description' => $fieldDescription, |
||
173 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
174 | ]; |
||
175 | } |
||
176 | |||
177 | return $options; |
||
178 | } |
||
179 | |||
193 |