Conditions | 10 |
Paths | 10 |
Total Lines | 48 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | Features | 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 |
||
96 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
97 | { |
||
98 | $options = array(); |
||
99 | $options['sonata_field_description'] = $fieldDescription; |
||
100 | |||
101 | if (in_array($type, array('sonata_type_model', 'sonata_type_model_list', 'sonata_type_model_hidden', 'sonata_type_model_autocomplete'))) { |
||
102 | if ($fieldDescription->getOption('edit') == 'list') { |
||
103 | 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'); |
||
104 | } |
||
105 | |||
106 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
107 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
108 | |||
109 | if ($type == 'sonata_type_model_autocomplete') { |
||
110 | if (!$fieldDescription->getAssociationAdmin()) { |
||
111 | 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())); |
||
112 | } |
||
113 | } |
||
114 | } elseif ($type == 'sonata_type_admin') { |
||
115 | if (!$fieldDescription->getAssociationAdmin()) { |
||
116 | 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())); |
||
117 | } |
||
118 | |||
119 | if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE))) { |
||
120 | 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())); |
||
121 | } |
||
122 | |||
123 | // set sensitive default value to have a component working fine out of the box |
||
124 | $options['btn_add'] = false; |
||
125 | $options['delete'] = false; |
||
126 | |||
127 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
128 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
129 | } elseif ($type == 'sonata_type_collection') { |
||
130 | if (!$fieldDescription->getAssociationAdmin()) { |
||
131 | 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())); |
||
132 | } |
||
133 | |||
134 | $options['type'] = 'sonata_type_admin'; |
||
135 | $options['modifiable'] = true; |
||
136 | $options['type_options'] = array( |
||
137 | 'sonata_field_description' => $fieldDescription, |
||
138 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | return $options; |
||
143 | } |
||
144 | } |
||
145 |