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 |
||
96 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
97 | { |
||
98 | $options = array(); |
||
99 | $options['sonata_field_description'] = $fieldDescription; |
||
100 | |||
101 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8 |
||
102 | if (in_array($type, array( |
||
103 | 'sonata_type_model', |
||
104 | 'Sonata\AdminBundle\Form\Type\ModelType', |
||
105 | 'sonata_type_model_list', |
||
106 | 'Sonata\AdminBundle\Form\Type\ModelTypeList', |
||
107 | 'sonata_type_model_hidden', |
||
108 | 'Sonata\AdminBundle\Form\Type\ModelHiddenType', |
||
109 | 'sonata_type_model_autocomplete', |
||
110 | 'Sonata\AdminBundle\Form\Type\ModelAutocompleteType', |
||
111 | ), true)) { |
||
112 | if ($fieldDescription->getOption('edit') === 'list') { |
||
113 | 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'); |
||
114 | } |
||
115 | |||
116 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
117 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
118 | |||
119 | if (in_array($type, array( |
||
120 | 'sonata_type_model_autocomplete', |
||
121 | 'Sonata\AdminBundle\Form\Type\ModelAutocompleteType', |
||
122 | ), true)) { |
||
123 | if (!$fieldDescription->getAssociationAdmin()) { |
||
124 | 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())); |
||
125 | } |
||
126 | } |
||
127 | } elseif (in_array($type, array( |
||
128 | 'sonata_type_admin', |
||
129 | 'Sonata\AdminBundle\Form\Type\AdminType', |
||
130 | ), true)) { |
||
131 | if (!$fieldDescription->getAssociationAdmin()) { |
||
132 | 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())); |
||
133 | } |
||
134 | |||
135 | if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) { |
||
136 | 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())); |
||
137 | } |
||
138 | |||
139 | // set sensitive default value to have a component working fine out of the box |
||
140 | $options['btn_add'] = false; |
||
141 | $options['delete'] = false; |
||
142 | |||
143 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
144 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
145 | } elseif (in_array($type, array( |
||
146 | 'sonata_type_collection', |
||
147 | 'Sonata\CoreBundle\Form\Type\CollectionType', |
||
148 | ), true)) { |
||
149 | if (!$fieldDescription->getAssociationAdmin()) { |
||
150 | 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())); |
||
151 | } |
||
152 | |||
153 | $options['type'] = 'sonata_type_admin'; |
||
154 | $options['modifiable'] = true; |
||
155 | $options['type_options'] = array( |
||
156 | 'sonata_field_description' => $fieldDescription, |
||
157 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | return $options; |
||
162 | } |
||
163 | |||
179 |