| Conditions | 11 |
| Paths | 55 |
| Total Lines | 52 |
| 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 |
||
| 77 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
| 78 | { |
||
| 79 | $fieldDescription->setAdmin($admin); |
||
| 80 | |||
| 81 | $metadata = null; |
||
| 82 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
| 83 | /** @var ClassMetadata $metadata */ |
||
| 84 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
| 85 | |||
| 86 | // set the default field mapping |
||
| 87 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
| 88 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
| 89 | } |
||
| 90 | |||
| 91 | // set the default association mapping |
||
| 92 | if ($metadata->hasAssociation($fieldDescription->getName())) { |
||
| 93 | $fieldDescription->setAssociationMapping($metadata->getAssociation($fieldDescription->getName())); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!$fieldDescription->getType()) { |
||
| 98 | throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), \get_class($admin))); |
||
| 99 | } |
||
| 100 | |||
| 101 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
| 102 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
| 103 | |||
| 104 | if (!$fieldDescription->getTemplate()) { |
||
|
|
|||
| 105 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
| 106 | |||
| 107 | if (ClassMetadata::MANY_TO_ONE === $fieldDescription->getMappingType()) { |
||
| 108 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_one.html.twig'); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (ClassMetadata::MANY_TO_MANY === $fieldDescription->getMappingType()) { |
||
| 112 | $fieldDescription->setTemplate('@SonataAdmin/CRUD/Association/show_many_to_many.html.twig'); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $mappingTypes = [ |
||
| 117 | ClassMetadata::MANY_TO_ONE, |
||
| 118 | ClassMetadata::MANY_TO_MANY, |
||
| 119 | 'children', |
||
| 120 | 'child', |
||
| 121 | 'parent', |
||
| 122 | 'referrers', |
||
| 123 | ]; |
||
| 124 | |||
| 125 | if ($metadata && $metadata->hasAssociation($fieldDescription->getName()) && \in_array($fieldDescription->getMappingType(), $mappingTypes, true)) { |
||
| 126 | $admin->attachAdminClass($fieldDescription); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 144 |
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: