Complex classes like ObjectMapping 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ObjectMapping, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | final class ObjectMapping implements MappingInterface |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var class-string |
||
| 33 | */ |
||
| 34 | private $className; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array<MappingInterface> |
||
| 38 | */ |
||
| 39 | private $fieldMappings = array(); |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Column|null |
||
| 43 | */ |
||
| 44 | private $column; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var CallDefinitionInterface|null |
||
| 48 | */ |
||
| 49 | private $factory; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var CallDefinitionInterface|null |
||
| 53 | */ |
||
| 54 | private $serializer; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $origin; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $id; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string|null |
||
| 68 | */ |
||
| 69 | private $referencedId; |
||
| 70 | |||
| 71 | /** @param class-string $className */ |
||
|
|
|||
| 72 | 17 | public function __construct( |
|
| 97 | |||
| 98 | 1 | public function getClassName(): string |
|
| 102 | |||
| 103 | 1 | public function getDBALColumn(): ?Column |
|
| 107 | |||
| 108 | 1 | public function getFieldMappings(): array |
|
| 112 | |||
| 113 | 1 | public function describeOrigin(): string |
|
| 117 | |||
| 118 | 2 | public function collectDBALColumns(): array |
|
| 138 | |||
| 139 | 1 | public function getFactory(): ?CallDefinitionInterface |
|
| 143 | |||
| 144 | 1 | public function getSerializer(): ?CallDefinitionInterface |
|
| 148 | |||
| 149 | 1 | public function getId(): ?string |
|
| 153 | |||
| 154 | 1 | public function getReferencedId(): ?string |
|
| 158 | |||
| 159 | 3 | public function resolveValue( |
|
| 160 | HydrationContextInterface $context, |
||
| 161 | array $dataFromAdditionalColumns |
||
| 162 | ) { |
||
| 163 | /** @var object|null $object */ |
||
| 164 | 3 | $object = null; |
|
| 165 | |||
| 166 | # During creation of an object, the class-name is on the top of that creation stack |
||
| 167 | 3 | $context->pushOnObjectHydrationStack($this->className); |
|
| 168 | |||
| 169 | 3 | $reflectionClass = new ReflectionClass($this->className); |
|
| 170 | |||
| 171 | 3 | if (!empty($this->referencedId)) { |
|
| 172 | $object = $context->getRegisteredValue($this->referencedId); |
||
| 173 | |||
| 174 | 3 | } elseif ($this->factory instanceof CallDefinitionInterface) { |
|
| 175 | /** @var array<string, string> $factoryData */ |
||
| 176 | 2 | $factoryData = $dataFromAdditionalColumns; |
|
| 177 | |||
| 178 | 2 | if ($this->column instanceof Column && !array_key_exists("", $factoryData)) { |
|
| 179 | /** @var Type $type */ |
||
| 180 | 1 | $type = $this->column->getType(); |
|
| 181 | |||
| 182 | /** @var string $columnName */ |
||
| 183 | 1 | $columnName = $this->column->getName(); |
|
| 184 | |||
| 185 | 1 | if (array_key_exists($columnName, $dataFromAdditionalColumns)) { |
|
| 186 | /** @var Connection $connection */ |
||
| 187 | $connection = $context->getEntityManager()->getConnection(); |
||
| 188 | |||
| 189 | if (!is_null($dataFromAdditionalColumns[$columnName])) { |
||
| 190 | $dataFromAdditionalColumns[$columnName] = $type->convertToPHPValue( |
||
| 191 | $dataFromAdditionalColumns[$columnName], |
||
| 192 | $connection->getDatabasePlatform() |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | $factoryData[""] = $dataFromAdditionalColumns[$columnName]; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | 2 | $object = $this->factory->execute( |
|
| 201 | 2 | $context, |
|
| 202 | $factoryData |
||
| 203 | ); |
||
| 204 | |||
| 205 | } else { |
||
| 206 | 1 | if ($reflectionClass->isInstantiable()) { |
|
| 207 | 1 | $object = $reflectionClass->newInstanceWithoutConstructor(); |
|
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | 3 | if (is_object($object)) { |
|
| 212 | // Replace the class-name with the created object on top of the hydration stack |
||
| 213 | 3 | $context->popFromObjectHydrationStack(); |
|
| 214 | 3 | $context->pushOnObjectHydrationStack($object); |
|
| 215 | |||
| 216 | 3 | if (!empty($this->id) && !$context->hasRegisteredValue($this->id)) { |
|
| 217 | 2 | $context->registerValue($this->id, $object); |
|
| 218 | } |
||
| 219 | |||
| 220 | 3 | foreach ($this->fieldMappings as $fieldName => $fieldMapping) { |
|
| 221 | /** @var MappingInterface $fieldMapping */ |
||
| 222 | |||
| 223 | /** @var mixed $fieldValue */ |
||
| 224 | 3 | $fieldValue = $fieldMapping->resolveValue( |
|
| 225 | 3 | $context, |
|
| 226 | $dataFromAdditionalColumns |
||
| 227 | ); |
||
| 228 | |||
| 229 | /** @var ReflectionClass $propertyReflectionClass */ |
||
| 230 | 3 | $propertyReflectionClass = $reflectionClass; |
|
| 231 | |||
| 232 | 3 | while (is_object($propertyReflectionClass) && !$propertyReflectionClass->hasProperty($fieldName)) { |
|
| 233 | $propertyReflectionClass = $propertyReflectionClass->getParentClass(); |
||
| 234 | } |
||
| 235 | |||
| 236 | 3 | if (!is_object($propertyReflectionClass)) { |
|
| 237 | throw new ReflectionException(sprintf( |
||
| 238 | "Property '%s' does not exist on class '%s' as defined at '%s'", |
||
| 239 | $fieldName, |
||
| 240 | $reflectionClass->getName(), |
||
| 241 | $reflectionClass->getFileName() |
||
| 242 | )); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** @var ReflectionProperty $reflectionProperty */ |
||
| 246 | 3 | $reflectionProperty = $propertyReflectionClass->getProperty($fieldName); |
|
| 247 | |||
| 248 | 3 | $reflectionProperty->setAccessible(true); |
|
| 249 | 3 | $reflectionProperty->setValue($object, $fieldValue); |
|
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | 3 | $context->popFromObjectHydrationStack(); |
|
| 254 | |||
| 255 | 3 | return $object; |
|
| 256 | } |
||
| 257 | |||
| 258 | 2 | public function revertValue( |
|
| 345 | |||
| 346 | 3 | public function assertValue( |
|
| 359 | |||
| 360 | 1 | public function wakeUpMapping(ContainerInterface $container): void |
|
| 370 | |||
| 371 | } |
||
| 372 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.