| Conditions | 10 |
| Paths | 10 |
| Total Lines | 80 |
| 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 |
||
| 94 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
| 95 | { |
||
| 96 | $options = []; |
||
| 97 | $options['sonata_field_description'] = $fieldDescription; |
||
| 98 | |||
| 99 | if ($this->checkFormClass($type, [ |
||
| 100 | ModelType::class, |
||
| 101 | ModelTypeList::class, |
||
| 102 | ModelListType::class, |
||
| 103 | ModelHiddenType::class, |
||
| 104 | ModelAutocompleteType::class, |
||
| 105 | ])) { |
||
| 106 | if ('list' === $fieldDescription->getOption('edit')) { |
||
| 107 | throw new \LogicException( |
||
| 108 | 'The ``Sonata\AdminBundle\Form\Type\ModelType`` type does not accept an ``edit`` option anymore,' |
||
| 109 | .' please review the UPGRADE-2.1.md file from the SonataAdminBundle' |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | $options['class'] = $fieldDescription->getTargetModel(); |
||
|
|
|||
| 114 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
| 115 | |||
| 116 | if ($this->checkFormClass($type, [ModelAutocompleteType::class])) { |
||
| 117 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 118 | throw new \RuntimeException(sprintf( |
||
| 119 | 'The current field `%s` is not linked to an admin.' |
||
| 120 | .' Please create one for the target entity: `%s`', |
||
| 121 | $fieldDescription->getName(), |
||
| 122 | $fieldDescription->getTargetModel() |
||
| 123 | )); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } elseif ($this->checkFormClass($type, [AdminType::class])) { |
||
| 127 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 128 | throw new \RuntimeException(sprintf( |
||
| 129 | 'The current field `%s` is not linked to an admin.' |
||
| 130 | .' Please create one for the target entity : `%s`', |
||
| 131 | $fieldDescription->getName(), |
||
| 132 | $fieldDescription->getTargetModel() |
||
| 133 | )); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (!\in_array($fieldDescription->getMappingType(), [ClassMetadata::ONE, ClassMetadata::MANY], true)) { |
||
| 137 | throw new \RuntimeException(sprintf( |
||
| 138 | 'You are trying to add `%s` field `%s` which is not One-To-One or Many-To-One.' |
||
| 139 | .' Maybe you want `%s` instead?', |
||
| 140 | AdminType::class, |
||
| 141 | $fieldDescription->getName(), |
||
| 142 | CollectionType::class |
||
| 143 | )); |
||
| 144 | } |
||
| 145 | |||
| 146 | // set sensitive default value to have a component working fine out of the box |
||
| 147 | $options['btn_add'] = false; |
||
| 148 | $options['delete'] = false; |
||
| 149 | |||
| 150 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
| 151 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
| 152 | } elseif ($this->checkFormClass($type, [ |
||
| 153 | CollectionType::class, |
||
| 154 | ])) { |
||
| 155 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 156 | throw new \RuntimeException(sprintf( |
||
| 157 | 'The current field `%s` is not linked to an admin.' |
||
| 158 | .' Please create one for the target entity : `%s`', |
||
| 159 | $fieldDescription->getName(), |
||
| 160 | $fieldDescription->getTargetModel() |
||
| 161 | )); |
||
| 162 | } |
||
| 163 | |||
| 164 | $options['type'] = AdminType::class; |
||
| 165 | $options['modifiable'] = true; |
||
| 166 | $options['type_options'] = [ |
||
| 167 | 'sonata_field_description' => $fieldDescription, |
||
| 168 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass(), |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $options; |
||
| 173 | } |
||
| 174 | |||
| 188 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: