| Conditions | 10 |
| Paths | 10 |
| Total Lines | 50 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 |
||
| 86 | public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription) |
||
| 87 | { |
||
| 88 | $options = array(); |
||
| 89 | $options['sonata_field_description'] = $fieldDescription; |
||
| 90 | |||
| 91 | if (in_array($type, array('sonata_type_model', 'sonata_type_model_list', 'sonata_type_model_hidden', 'sonata_type_model_autocomplete'))) { |
||
| 92 | |||
| 93 | if ($fieldDescription->getOption('edit') == 'list') { |
||
| 94 | 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'); |
||
| 95 | } |
||
| 96 | |||
| 97 | $options['class'] = $fieldDescription->getTargetEntity(); |
||
| 98 | $options['model_manager'] = $fieldDescription->getAdmin()->getModelManager(); |
||
| 99 | |||
| 100 | if ($type == 'sonata_type_model_autocomplete') { |
||
| 101 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 102 | 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())); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | } elseif ($type == 'sonata_type_admin') { |
||
| 107 | |||
| 108 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 109 | 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())); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!in_array($fieldDescription->getMappingType(), array(ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::MANY_TO_ONE ))) { |
||
| 113 | 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())); |
||
| 114 | } |
||
| 115 | |||
| 116 | $options['data_class'] = $fieldDescription->getAssociationAdmin()->getClass(); |
||
| 117 | $fieldDescription->setOption('edit', $fieldDescription->getOption('edit', 'admin')); |
||
| 118 | |||
| 119 | } elseif ($type == 'sonata_type_collection') { |
||
| 120 | |||
| 121 | if (!$fieldDescription->getAssociationAdmin()) { |
||
| 122 | 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())); |
||
| 123 | } |
||
| 124 | |||
| 125 | $options['type'] = 'sonata_type_admin'; |
||
| 126 | $options['modifiable'] = true; |
||
| 127 | $options['type_options'] = array( |
||
| 128 | 'sonata_field_description' => $fieldDescription, |
||
| 129 | 'data_class' => $fieldDescription->getAssociationAdmin()->getClass() |
||
| 130 | ); |
||
| 131 | |||
| 132 | } |
||
| 133 | |||
| 134 | return $options; |
||
| 135 | } |
||
| 136 | } |
||
| 137 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: