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