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