| Total Complexity | 40 |
| Total Lines | 205 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TypedPropertiesDriver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TypedPropertiesDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class TypedPropertiesDriver implements DriverInterface |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var DriverInterface |
||
| 27 | */ |
||
| 28 | protected $delegate; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var ParserInterface |
||
| 32 | */ |
||
| 33 | protected $typeParser; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string[] |
||
| 37 | */ |
||
| 38 | private $allowList; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string[] $allowList |
||
| 42 | */ |
||
| 43 | public function __construct(DriverInterface $delegate, ?ParserInterface $typeParser = null, array $allowList = []) |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * In order to deserialize non-discriminated unions, each possible type is attempted in turn. |
||
| 52 | * Therefore, the types must be ordered from most specific to least specific, so that the most specific type is attempted first. |
||
| 53 | * |
||
| 54 | * ReflectionUnionType::getTypes() does not return types in that order, so we need to reorder them. |
||
| 55 | * |
||
| 56 | * This method reorders the types in the following order: |
||
| 57 | * - primitives in speficity order: null, true, false, int, float, bool, string |
||
| 58 | * - classes and interaces in order of most number of required properties |
||
| 59 | */ |
||
| 60 | private function reorderTypes(array $type): array |
||
| 100 | } |
||
| 101 | |||
| 102 | private function allowsNull(array $type) |
||
| 103 | { |
||
| 104 | $allowsNull = false; |
||
| 105 | if ('union' === $type['name']) { |
||
| 106 | foreach ($type['params'] as $param) { |
||
| 107 | if ('NULL' === $param['name']) { |
||
| 108 | $allowsNull = true; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } elseif ('NULL' === $type['name']) { |
||
| 112 | $allowsNull = true; |
||
| 113 | } |
||
| 114 | |||
| 115 | return $allowsNull; |
||
| 116 | } |
||
| 117 | |||
| 118 | private function getDefaultWhiteList(): array |
||
| 119 | { |
||
| 120 | return [ |
||
| 121 | 'int', |
||
| 122 | 'float', |
||
| 123 | 'bool', |
||
| 124 | 'boolean', |
||
| 125 | 'string', |
||
| 126 | 'double', |
||
| 127 | 'iterable', |
||
| 128 | 'resource', |
||
| 129 | ]; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @return SerializerClassMetadata|null |
||
| 134 | */ |
||
| 135 | public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata |
||
| 136 | { |
||
| 137 | $classMetadata = $this->delegate->loadMetadataForClass($class); |
||
| 138 | |||
| 139 | if (null === $classMetadata) { |
||
| 140 | return null; |
||
| 141 | } |
||
| 142 | |||
| 143 | \assert($classMetadata instanceof SerializerClassMetadata); |
||
| 144 | |||
| 145 | // We base our scan on the internal driver's property list so that we |
||
| 146 | // respect any internal allow/blocklist like in the AnnotationDriver |
||
| 147 | foreach ($classMetadata->propertyMetadata as $propertyMetadata) { |
||
| 148 | // If the inner driver provides a type, don't guess anymore. |
||
| 149 | if ($propertyMetadata->type) { |
||
| 150 | continue; |
||
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | $reflectionType = $this->getReflectionType($propertyMetadata); |
||
| 155 | |||
| 156 | if ($this->shouldTypeHint($reflectionType)) { |
||
| 157 | $type = $reflectionType->getName(); |
||
|
|
|||
| 158 | |||
| 159 | $propertyMetadata->setType($this->typeParser->parse($type)); |
||
| 160 | } elseif ($this->shouldTypeHintUnion($reflectionType)) { |
||
| 161 | $propertyMetadata->setType($this->reorderTypes([ |
||
| 162 | 'name' => 'union', |
||
| 163 | 'params' => array_map(fn (string $type) => $this->typeParser->parse($type), $reflectionType->getTypes()), |
||
| 164 | ])); |
||
| 165 | } |
||
| 166 | } catch (ReflectionException $e) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | return $classMetadata; |
||
| 172 | } |
||
| 173 | |||
| 174 | private function getReflectionType(PropertyMetadata $propertyMetadata): ?ReflectionType |
||
| 175 | { |
||
| 176 | if ($this->isNotSupportedVirtualProperty($propertyMetadata)) { |
||
| 177 | return null; |
||
| 178 | } |
||
| 179 | |||
| 180 | if ($propertyMetadata instanceof VirtualPropertyMetadata) { |
||
| 181 | return (new ReflectionMethod($propertyMetadata->class, $propertyMetadata->getter)) |
||
| 182 | ->getReturnType(); |
||
| 183 | } |
||
| 184 | |||
| 185 | return (new ReflectionProperty($propertyMetadata->class, $propertyMetadata->name)) |
||
| 186 | ->getType(); |
||
| 187 | } |
||
| 188 | |||
| 189 | private function isNotSupportedVirtualProperty(PropertyMetadata $propertyMetadata): bool |
||
| 190 | { |
||
| 191 | return $propertyMetadata instanceof StaticPropertyMetadata |
||
| 192 | || $propertyMetadata instanceof ExpressionPropertyMetadata; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * @phpstan-assert-if-true \ReflectionNamedType $reflectionType |
||
| 197 | */ |
||
| 198 | private function shouldTypeHint(?ReflectionType $reflectionType): bool |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @phpstan-assert-if-true \ReflectionUnionType $reflectionType |
||
| 214 | */ |
||
| 215 | private function shouldTypeHintUnion(?ReflectionType $reflectionType) |
||
| 228 | } |
||
| 229 | } |
||
| 230 |