| Conditions | 8 |
| Paths | 7 |
| Total Lines | 54 |
| 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 |
||
| 48 | public function resolve( |
||
| 49 | FieldDescriptionInterface $fieldDescription, |
||
| 50 | ModelManagerInterface $modelManager |
||
| 51 | ): ?DataTransformerInterface { |
||
| 52 | $dataTransformer = $fieldDescription->getOption('data_transformer'); |
||
| 53 | |||
| 54 | // allow override predefined transformers for 'date', 'boolean' and 'choice' field types |
||
| 55 | if ($dataTransformer instanceof DataTransformerInterface) { |
||
| 56 | return $dataTransformer; |
||
| 57 | } |
||
| 58 | |||
| 59 | $fieldType = (string) $fieldDescription->getType(); |
||
| 60 | |||
| 61 | // allow override predefined transformers on a global level |
||
| 62 | if (\array_key_exists($fieldType, $this->globalCustomTransformers)) { |
||
| 63 | return $this->globalCustomTransformers[$fieldType]; |
||
| 64 | } |
||
| 65 | |||
| 66 | // Handle date type has setter expect a DateTime object |
||
| 67 | if (TemplateRegistry::TYPE_DATE === $fieldType) { |
||
| 68 | $this->globalCustomTransformers[$fieldType] = new DateTimeToStringTransformer( |
||
| 69 | null, |
||
| 70 | $this->getOutputTimezone($fieldDescription), |
||
| 71 | 'Y-m-d' |
||
| 72 | ); |
||
| 73 | |||
| 74 | return $this->globalCustomTransformers[$fieldType]; |
||
| 75 | } |
||
| 76 | |||
| 77 | // Handle datetime type has setter expect a DateTime object |
||
| 78 | if (TemplateRegistry::TYPE_DATETIME === $fieldType) { |
||
| 79 | $this->globalCustomTransformers[$fieldType] = new DateTimeToStringTransformer( |
||
| 80 | null, |
||
| 81 | $this->getOutputTimezone($fieldDescription), |
||
| 82 | 'Y-m-d H:i:s' |
||
| 83 | ); |
||
| 84 | |||
| 85 | return $this->globalCustomTransformers[$fieldType]; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Handle entity choice association type, transforming the value into entity |
||
| 89 | if (TemplateRegistry::TYPE_CHOICE === $fieldType) { |
||
| 90 | $className = $fieldDescription->getOption('class'); |
||
| 91 | |||
| 92 | if (null !== $className && |
||
| 93 | // NEXT_MAJOR: Replace this call with "$className === $fieldDescription->getTargetModel()". |
||
| 94 | $this->hasFieldDescriptionAssociationWithClass($fieldDescription, $className) |
||
| 95 | ) { |
||
| 96 | return new ModelToIdTransformer($modelManager, $className); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return null; |
||
| 101 | } |
||
| 102 | |||
| 127 |
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: