Conditions | 9 |
Paths | 22 |
Total Lines | 63 |
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 |
||
68 | public function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription): void |
||
69 | { |
||
70 | // set default values |
||
71 | $fieldDescription->setAdmin($admin); |
||
72 | |||
73 | if ($admin->getModelManager()->hasMetadata($admin->getClass())) { |
||
74 | list($metadata, $lastPropertyName, $parentAssociationMappings) = $admin->getModelManager() |
||
75 | ->getParentMetadataForProperty($admin->getClass(), $fieldDescription->getName()); |
||
76 | |||
77 | // set the default field mapping |
||
78 | if (isset($metadata->fieldMappings[$lastPropertyName])) { |
||
79 | $fieldDescription->setOption( |
||
80 | 'field_mapping', |
||
81 | $fieldDescription->getOption( |
||
82 | 'field_mapping', |
||
83 | $fieldMapping = $metadata->fieldMappings[$lastPropertyName] |
||
84 | ) |
||
85 | ); |
||
86 | |||
87 | if ('string' === $fieldMapping['type']) { |
||
88 | $fieldDescription->setOption('global_search', $fieldDescription->getOption('global_search', true)); // always search on string field only |
||
89 | } |
||
90 | |||
91 | if (!empty($embeddedClasses = $metadata->embeddedClasses) |
||
92 | && isset($fieldMapping['declaredField']) |
||
93 | && \array_key_exists($fieldMapping['declaredField'], $embeddedClasses) |
||
94 | ) { |
||
95 | $fieldDescription->setOption( |
||
96 | 'field_name', |
||
97 | $fieldMapping['fieldName'] |
||
98 | ); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | // set the default association mapping |
||
103 | if (isset($metadata->associationMappings[$lastPropertyName])) { |
||
104 | $fieldDescription->setOption( |
||
105 | 'association_mapping', |
||
106 | $fieldDescription->getOption( |
||
107 | 'association_mapping', |
||
108 | $metadata->associationMappings[$lastPropertyName] |
||
109 | ) |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | $fieldDescription->setOption( |
||
114 | 'parent_association_mappings', |
||
115 | $fieldDescription->getOption('parent_association_mappings', $parentAssociationMappings) |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | $fieldDescription->setOption('code', $fieldDescription->getOption('code', $fieldDescription->getName())); |
||
120 | $fieldDescription->setOption('name', $fieldDescription->getOption('name', $fieldDescription->getName())); |
||
121 | |||
122 | if (\in_array($fieldDescription->getMappingType(), [ |
||
123 | ClassMetadata::ONE_TO_MANY, |
||
124 | ClassMetadata::MANY_TO_MANY, |
||
125 | ClassMetadata::MANY_TO_ONE, |
||
126 | ClassMetadata::ONE_TO_ONE, |
||
127 | ], true)) { |
||
128 | $admin->attachAdminClass($fieldDescription); |
||
129 | } |
||
130 | } |
||
131 | |||
216 |
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: