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 = []; |
||
106 | $options['sonata_field_description'] = $fieldDescription; |
||
107 | if ($this->checkFormClass($type, [ |
||
108 | 'Sonata\AdminBundle\Form\Type\ModelType', |
||
109 | 'Sonata\AdminBundle\Form\Type\ModelTypeList', |
||
110 | 'Sonata\AdminBundle\Form\Type\ModelListType', |
||
111 | 'Sonata\AdminBundle\Form\Type\ModelHiddenType', |
||
112 | 'Sonata\AdminBundle\Form\Type\ModelAutocompleteType', |
||
113 | ])) { |
||
114 | if ($fieldDescription->getOption('edit') === 'list') { |
||
115 | throw new \LogicException( |
||
116 | 'The `sonata_type_model` type does not accept an `edit` option anymore,' |
||
117 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
122 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
123 | |||
124 | if ($this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\ModelAutocompleteType'])) { |
||
125 | if (!$fieldDescription->getAssociationAdmin()) { |
||
126 | throw new \RuntimeException(sprintf( |
||
127 | 'The current field `%s` is not linked to an admin.' |
||
128 | .' Please create one for the target entity: `%s`', |
||
129 | $fieldDescription->getName(), |
||
130 | $fieldDescription->getTargetEntity() |
||
131 | )); |
||
132 | } |
||
133 | } |
||
134 | <<<<<<< HEAD |
||
135 | } elseif ($this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\AdminType'])) { |
||
136 | ======= |
||
137 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8 |
||
138 | } elseif ($this->checkFormType($type, ['sonata_type_admin']) || $this->checkFormClass($type, ['Sonata\AdminBundle\Form\Type\AdminType'])) { |
||
139 | >>>>>>> origin/3.x |
||
140 | if (!$fieldDescription->getAssociationAdmin()) { |
||
141 | throw new \RuntimeException(sprintf( |
||
142 | 'The current field `%s` is not linked to an admin.' |
||
143 | .' Please create one for the target entity : `%s`', |
||
144 | $fieldDescription->getName(), |
||
145 | $fieldDescription->getTargetEntity() |
||
146 | )); |
||
147 | } |
||
148 | |||
149 | if (!in_array($fieldDescription->getMappingType(), [ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE])) { |
||
150 | throw new \RuntimeException(sprintf( |
||
151 | 'You are trying to add `sonata_type_admin` field `%s` which is not One-To-One or Many-To-One.' |
||
152 | .' Maybe you want `sonata_type_collection` instead?', |
||
153 | $fieldDescription->getName() |
||
154 | )); |
||
155 | } |
||
156 | |||
157 | // set sensitive default value to have a component working fine out of the box |
||
158 | $options['btn_add'] = false; |
||
159 | $options['delete'] = false; |
||
160 | |||
161 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
162 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
163 | <<<<<<< HEAD |
||
164 | } elseif ($this->checkFormClass($type, ['Sonata\CoreBundle\Form\Type\CollectionType'])) { |
||
165 | ======= |
||
166 | // NEXT_MAJOR: Check only against FQCNs when dropping support for Symfony <2.8 |
||
167 | } elseif ($this->checkFormType($type, ['sonata_type_collection']) || $this->checkFormClass($type, ['Sonata\CoreBundle\Form\Type\CollectionType'])) { |
||
168 | >>>>>>> origin/3.x |
||
169 | if (!$fieldDescription->getAssociationAdmin()) { |
||
170 | throw new \RuntimeException(sprintf( |
||
171 | 'The current field `%s` is not linked to an admin.' |
||
172 | .' Please create one for the target entity : `%s`', |
||
173 | $fieldDescription->getName(), |
||
174 | $fieldDescription->getTargetEntity() |
||
175 | )); |
||
176 | } |
||
177 | |||
178 | $options['type'] = 'sonata_type_admin'; |
||
179 | $options['modifiable'] = true; |
||
233 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.