Conditions | 13 |
Paths | 234 |
Total Lines | 62 |
Code Lines | 31 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 2 | Features | 1 |
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 |
||
81 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription) |
||
82 | { |
||
83 | if ($fieldDescription->getName() == '_action') { |
||
84 | $this->buildActionFieldDescription($fieldDescription); |
||
85 | } |
||
86 | |||
87 | $fieldDescription->setAdmin($admin); |
||
88 | |||
89 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
90 | $metadata = $admin->getModelManager()->getMetadata($admin->getClass()); |
||
91 | |||
92 | // TODO sort on parent associations or node name |
||
93 | $defaultSortable = true; |
||
94 | if ($metadata->hasAssociation($fieldDescription->getName()) |
||
95 | || $metadata->nodename === $fieldDescription->getName() |
||
96 | ) { |
||
97 | $defaultSortable = false; |
||
98 | } |
||
99 | |||
100 | // TODO get and set parent association mappings, see |
||
101 | // https://github.com/sonata-project/SonataDoctrinePhpcrAdminBundle/issues/106 |
||
102 | //$fieldDescription->setParentAssociationMappings($parentAssociationMappings); |
||
103 | |||
104 | // set the default field mapping |
||
105 | if (isset($metadata->mappings[$fieldDescription->getName()])) { |
||
106 | $fieldDescription->setFieldMapping($metadata->mappings[$fieldDescription->getName()]); |
||
107 | if ($fieldDescription->getOption('sortable') !== false) { |
||
108 | $fieldDescription->setOption('sortable', $fieldDescription->getOption('sortable', $defaultSortable)); |
||
109 | $fieldDescription->setOption('sort_parent_association_mappings', $fieldDescription->getOption('sort_parent_association_mappings', $fieldDescription->getParentAssociationMappings())); |
||
110 | $fieldDescription->setOption('sort_field_mapping', $fieldDescription->getOption('sort_field_mapping', $fieldDescription->getFieldMapping())); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | // set the default association mapping |
||
115 | if (isset($metadata->associationMappings[$fieldDescription->getName()])) { |
||
116 | $fieldDescription->setAssociationMapping($metadata->associationMappings[$fieldDescription->getName()]); |
||
117 | } |
||
118 | |||
119 | $fieldDescription->setOption('_sort_order', $fieldDescription->getOption('_sort_order', 'ASC')); |
||
120 | } |
||
121 | |||
122 | if (!$fieldDescription->getType()) { |
||
123 | throw new \RuntimeException(sprintf('Please define a type for field `%s` in `%s`', $fieldDescription->getName(), get_class($admin))); |
||
124 | } |
||
125 | |||
126 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
127 | $fieldDescription->setOption('label', $fieldDescription->getOption('label', $fieldDescription->getName())); |
||
128 | |||
129 | if (!$fieldDescription->getTemplate()) { |
||
130 | $fieldDescription->setTemplate($this->getTemplate($fieldDescription->getType())); |
||
131 | |||
132 | if ($fieldDescription->describesSingleValuedAssociation()) { |
||
|
|||
133 | $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:list_single.html.twig'); |
||
134 | } elseif ($fieldDescription->describesCollectionValuedAssociation()) { |
||
135 | $fieldDescription->setTemplate('SonataAdminBundle:CRUD/Association:list_collection.html.twig'); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if ($fieldDescription->describesAssociation()) { |
||
140 | $admin->attachAdminClass($fieldDescription); |
||
141 | } |
||
142 | } |
||
143 | |||
195 |
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: