| Total Complexity | 40 |
| Total Lines | 264 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EntityHydrator 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 EntityHydrator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | final class EntityHydrator extends DoctrineObject |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var \ReflectionClass|null |
||
| 28 | */ |
||
| 29 | private ?\ReflectionClass $reflectionClass = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @noinspection PhpMissingParamTypeInspection |
||
| 33 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 34 | * |
||
| 35 | * @param object $object |
||
| 36 | * @param mixed $collectionName |
||
| 37 | * @param string $target |
||
| 38 | * @param mixed $values |
||
| 39 | * |
||
| 40 | * @throws RuntimeException |
||
| 41 | * @throws InvalidArgumentException |
||
| 42 | * @throws \InvalidArgumentException |
||
| 43 | */ |
||
| 44 | protected function toMany($object, $collectionName, $target, $values) |
||
| 45 | { |
||
| 46 | $metadata = $this->objectManager->getClassMetadata(ltrim($target, '\\')); |
||
| 47 | $identifier = $metadata->getIdentifier(); |
||
| 48 | |||
| 49 | if (! is_array($values) && ! $values instanceof \Traversable) { |
||
| 50 | $values = (array) $values; |
||
| 51 | } |
||
| 52 | |||
| 53 | $collection = []; |
||
| 54 | |||
| 55 | // If the collection contains identifiers, fetch the objects from database |
||
| 56 | foreach ($values as $value) { |
||
| 57 | if ($value instanceof $target) { |
||
| 58 | // assumes modifications have already taken place in object |
||
| 59 | $collection[] = $value; |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (empty($value)) { |
||
| 64 | // assumes no id and retrieves new $target |
||
| 65 | $collection[] = $this->find($value, $target); |
||
| 66 | continue; |
||
| 67 | } |
||
| 68 | |||
| 69 | $find = $this->getFindCriteria($identifier, $value); |
||
| 70 | |||
| 71 | if (! empty($find) && $found = $this->find($find, $target)) { |
||
| 72 | $collection[] = is_array($value) ? $this->hydrate($value, $found) : $found; |
||
| 73 | } else { |
||
| 74 | $newTarget = $this->createTargetEntity($target); |
||
| 75 | |||
| 76 | $collection[] = is_array($value) ? $this->hydrate($value, $newTarget) : $newTarget; |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | $collection = array_filter( |
||
| 81 | $collection, |
||
| 82 | static fn($item) => null !== $item |
||
| 83 | ); |
||
| 84 | |||
| 85 | /** @var AbstractCollectionStrategy $collectionStrategy */ |
||
| 86 | $collectionStrategy = $this->getStrategy($collectionName); |
||
| 87 | $collectionStrategy->setObject($object); |
||
| 88 | |||
| 89 | $this->hydrateValue($collectionName, $collection, $values); |
||
|
|
|||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $className |
||
| 94 | * |
||
| 95 | * @return object |
||
| 96 | * |
||
| 97 | * @throws RuntimeException |
||
| 98 | */ |
||
| 99 | private function createTargetEntity(string $className): object |
||
| 100 | { |
||
| 101 | return $this->getReflectionClass($className)->newInstanceWithoutConstructor(); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Copied from parent to check for isInitialisedFieldName() |
||
| 106 | * |
||
| 107 | * @noinspection PhpMissingParamTypeInspection |
||
| 108 | * |
||
| 109 | * @param object $object |
||
| 110 | * |
||
| 111 | * @return array |
||
| 112 | * |
||
| 113 | * @throws RuntimeException |
||
| 114 | */ |
||
| 115 | public function extractByValue($object): array |
||
| 116 | { |
||
| 117 | $fieldNames = array_merge($this->metadata->getFieldNames(), $this->metadata->getAssociationNames()); |
||
| 118 | $methods = get_class_methods($object); |
||
| 119 | $filter = $object instanceof FilterProviderInterface |
||
| 120 | ? $object->getFilter() |
||
| 121 | : $this->filterComposite; |
||
| 122 | |||
| 123 | $data = []; |
||
| 124 | foreach ($fieldNames as $fieldName) { |
||
| 125 | if (!$this->isInitialisedFieldName($object, $fieldName)) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | if ($filter && !$filter->filter($fieldName)) { |
||
| 129 | continue; |
||
| 130 | } |
||
| 131 | |||
| 132 | $getter = 'get' . ucfirst($fieldName); |
||
| 133 | $isser = 'is' . ucfirst($fieldName); |
||
| 134 | |||
| 135 | $dataFieldName = $this->computeExtractFieldName($fieldName); |
||
| 136 | if (in_array($getter, $methods, true)) { |
||
| 137 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$getter(), $object); |
||
| 138 | } elseif (in_array($isser, $methods, true)) { |
||
| 139 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$isser(), $object); |
||
| 140 | } elseif ( |
||
| 141 | 0 === strpos($fieldName, 'is') |
||
| 142 | && in_array($fieldName, $methods, true) |
||
| 143 | && ctype_upper(substr($fieldName, 2, 1)) |
||
| 144 | ) { |
||
| 145 | $data[$dataFieldName] = $this->extractValue($fieldName, $object->$fieldName(), $object); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | return $data; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Check if the provided $fieldName is initialised for the given $object |
||
| 154 | * |
||
| 155 | * @param object $object |
||
| 156 | * @param string $fieldName |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | * |
||
| 160 | * @throws RuntimeException |
||
| 161 | */ |
||
| 162 | protected function isInitialisedFieldName(object $object, string $fieldName): bool |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param object $object |
||
| 182 | * @param string $fieldName |
||
| 183 | * |
||
| 184 | * @return \ReflectionProperty |
||
| 185 | * |
||
| 186 | * @throws RuntimeException |
||
| 187 | */ |
||
| 188 | private function getReflectionProperty(object $object, string $fieldName): \ReflectionProperty |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $className |
||
| 223 | * |
||
| 224 | * @return \ReflectionClass |
||
| 225 | * |
||
| 226 | * @throws RuntimeException |
||
| 227 | */ |
||
| 228 | private function getReflectionClass(string $className): \ReflectionClass |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param array $identifier |
||
| 253 | * @param mixed $value |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | protected function getFindCriteria(array $identifier, $value): array |
||
| 290 |