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