| Conditions | 15 |
| Paths | 210 |
| Total Lines | 83 |
| 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 |
||
| 74 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
| 75 | { |
||
| 76 | if ('_action' === $fieldDescription->getName() || 'actions' === $fieldDescription->getType()) { |
||
| 77 | $this->buildActionFieldDescription($fieldDescription); |
||
| 78 | } |
||
| 79 | |||
| 80 | $fieldDescription->setAdmin($admin); |
||
| 81 | |||
| 82 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
| 83 | list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager()->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName()); |
||
| 84 | $fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
| 85 | |||
| 86 | // set the default field mapping |
||
| 87 | if (isset($metadata->fieldMappings[$lastPropertyName])) { |
||
| 88 | $fieldDescription->setFieldMapping($metadata->fieldMappings[$lastPropertyName]); |
||
| 89 | if (false !== $fieldDescription->getOption('sortable')) { |
||
| 90 | $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', true)); |
||
| 91 | $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings())); |
||
| 92 | $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping())); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | // set the default association mapping |
||
| 97 | if (isset($metadata->associationMappings[$lastPropertyName])) { |
||
| 98 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$lastPropertyName]); |
||
| 99 | } |
||
| 100 | |||
| 101 | $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC')); |
||
| 102 | } |
||
| 103 | |||
| 104 | if (!$fieldDescription->getType()) { |
||
| 105 | throw new \RuntimeException(sprintf( |
||
| 106 | 'Please define a type for field `%s` in `%s`', |
||
| 107 | $fieldDescription->getName(), |
||
| 108 | \get_class($admin) |
||
| 109 | )); |
||
| 110 | } |
||
| 111 | |||
| 112 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
| 113 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
| 114 | |||
| 115 | if (!$fieldDescription->getTemplate()) { |
||
|
|
|||
| 116 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
| 117 | |||
| 118 | if (!$fieldDescription->getTemplate()) { |
||
| 119 | switch ($fieldDescription->getMappingType()) { |
||
| 120 | case ClassMetadata::MANY_TO_ONE: |
||
| 121 | $fieldDescription->setTemplate( |
||
| 122 | '@SonataAdmin/CRUD/Association/list_many_to_one.html.twig' |
||
| 123 | ); |
||
| 124 | |||
| 125 | break; |
||
| 126 | case ClassMetadata::ONE_TO_ONE: |
||
| 127 | $fieldDescription->setTemplate( |
||
| 128 | '@SonataAdmin/CRUD/Association/list_one_to_one.html.twig' |
||
| 129 | ); |
||
| 130 | |||
| 131 | break; |
||
| 132 | case ClassMetadata::ONE_TO_MANY: |
||
| 133 | $fieldDescription->setTemplate( |
||
| 134 | '@SonataAdmin/CRUD/Association/list_one_to_many.html.twig' |
||
| 135 | ); |
||
| 136 | |||
| 137 | break; |
||
| 138 | case ClassMetadata::MANY_TO_MANY: |
||
| 139 | $fieldDescription->setTemplate( |
||
| 140 | '@SonataAdmin/CRUD/Association/list_many_to_many.html.twig' |
||
| 141 | ); |
||
| 142 | |||
| 143 | break; |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if (\in_array($fieldDescription->getMappingType(), [ |
||
| 149 | ClassMetadata::MANY_TO_ONE, |
||
| 150 | ClassMetadata::ONE_TO_ONE, |
||
| 151 | ClassMetadata::ONE_TO_MANY, |
||
| 152 | ClassMetadata::MANY_TO_MANY, |
||
| 153 | ], true)) { |
||
| 154 | $admin->attachAdminClass($fieldDescription); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 217 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: