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