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