Conditions | 10 |
Paths | 13 |
Total Lines | 47 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
49 | public function getReadableEnumValue($enumValue, $enumType = null) |
||
50 | { |
||
51 | if (!empty($this->registeredEnumTypes) && is_array($this->registeredEnumTypes)) { |
||
52 | if (is_null($enumValue)) { |
||
53 | return $enumValue; |
||
54 | } |
||
55 | // If ENUM type was set, e.g. {{ player.position|readable_enum('BasketballPositionType') }} |
||
56 | if (!empty($enumType)) { |
||
57 | if (!isset($this->registeredEnumTypes[$enumType])) { |
||
58 | throw new EnumTypeIsNotRegisteredException(sprintf('ENUM type "%s" is not registered.', $enumType)); |
||
59 | } |
||
60 | |||
61 | /** @var $enumTypeClass \Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType */ |
||
62 | $enumTypeClass = $this->registeredEnumTypes[$enumType]; |
||
63 | |||
64 | return $enumTypeClass::getReadableValue($enumValue); |
||
65 | } else { |
||
66 | // If ENUM type wasn't set, e.g. {{ player.position|readable_enum }} |
||
67 | $occurrences = []; |
||
68 | // Check if value exists in registered ENUM types |
||
69 | foreach ($this->registeredEnumTypes as $registeredEnumType) { |
||
70 | if ($registeredEnumType::isValueExist($enumValue)) { |
||
71 | $occurrences[] = $registeredEnumType; |
||
72 | } |
||
73 | } |
||
74 | |||
75 | // If found only one occurrence, then we know exactly which ENUM type |
||
76 | if (1 == count($occurrences)) { |
||
77 | $enumTypeClass = array_pop($occurrences); |
||
78 | |||
79 | return $enumTypeClass::getReadableValue($enumValue); |
||
80 | } elseif (1 < count($occurrences)) { |
||
81 | throw new ValueIsFoundInFewRegisteredEnumTypesException(sprintf( |
||
82 | 'Value "%s" is found in few registered ENUM types. You should manually set the appropriate one', |
||
83 | $enumValue |
||
84 | )); |
||
85 | } else { |
||
86 | throw new ValueIsNotFoundInAnyRegisteredEnumTypeException(sprintf( |
||
87 | 'Value "%s" wasn\'t found in any registered ENUM type.', |
||
88 | $enumValue |
||
89 | )); |
||
90 | } |
||
91 | } |
||
92 | } else { |
||
93 | throw new NoRegisteredEnumTypesException('There are no registered ENUM types.'); |
||
94 | } |
||
95 | } |
||
96 | } |
||
97 |