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