Complex classes like DefaultQueryCache 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 DefaultQueryCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
39 | class DefaultQueryCache implements QueryCache |
||
40 | { |
||
41 | /** |
||
42 | * @var \Doctrine\ORM\EntityManagerInterface |
||
43 | */ |
||
44 | private $em; |
||
45 | |||
46 | /** |
||
47 | * @var \Doctrine\ORM\UnitOfWork |
||
48 | */ |
||
49 | private $uow; |
||
50 | |||
51 | /** |
||
52 | * @var \Doctrine\ORM\Cache\Region |
||
53 | */ |
||
54 | private $region; |
||
55 | |||
56 | /** |
||
57 | * @var \Doctrine\ORM\Cache\QueryCacheValidator |
||
58 | */ |
||
59 | private $validator; |
||
60 | |||
61 | /** |
||
62 | * @var \Doctrine\ORM\Cache\Logging\CacheLogger |
||
63 | */ |
||
64 | protected $cacheLogger; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | private static $hints = [Query::HINT_CACHE_ENABLED => true]; |
||
70 | |||
71 | /** |
||
72 | * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager. |
||
73 | * @param \Doctrine\ORM\Cache\Region $region The query region. |
||
74 | */ |
||
75 | 85 | public function __construct(EntityManagerInterface $em, Region $region) |
|
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 51 | public function get(QueryCacheKey $key, ResultSetMapping $rsm, array $hints = []) |
|
90 | { |
||
91 | 51 | if ( ! ($key->cacheMode & Cache::MODE_GET)) { |
|
92 | 3 | return null; |
|
93 | } |
||
94 | |||
95 | 50 | $entry = $this->region->get($key); |
|
96 | |||
97 | 50 | if ( ! $entry instanceof QueryCacheEntry) { |
|
98 | 46 | return null; |
|
99 | } |
||
100 | |||
101 | 37 | if ( ! $this->validator->isValid($key, $entry)) { |
|
102 | 8 | $this->region->evict($key); |
|
103 | |||
104 | 8 | return null; |
|
105 | } |
||
106 | |||
107 | 32 | $result = []; |
|
108 | 32 | $entityName = reset($rsm->aliasMap); |
|
109 | 32 | $hasRelation = ( ! empty($rsm->relationMap)); |
|
110 | 32 | $persister = $this->uow->getEntityPersister($entityName); |
|
111 | 32 | $region = $persister->getCacheRegion(); |
|
112 | 32 | $regionName = $region->getName(); |
|
113 | |||
114 | 32 | $cm = $this->em->getClassMetadata($entityName); |
|
115 | |||
116 | 32 | $generateKeys = function (array $entry) use ($cm): EntityCacheKey { |
|
117 | 32 | return new EntityCacheKey($cm->rootEntityName, $entry['identifier']); |
|
|
|||
118 | 32 | }; |
|
119 | |||
120 | 32 | $cacheKeys = new CollectionCacheEntry(array_map($generateKeys, $entry->result)); |
|
121 | 32 | $entries = $region->getMultiple($cacheKeys); |
|
122 | |||
123 | // @TODO - move to cache hydration component |
||
124 | 32 | foreach ($entry->result as $index => $entry) { |
|
125 | 32 | $entityEntry = is_array($entries) && array_key_exists($index, $entries) ? $entries[$index] : null; |
|
126 | |||
127 | 32 | if ($entityEntry === null) { |
|
128 | 2 | if ($this->cacheLogger !== null) { |
|
129 | 1 | $this->cacheLogger->entityCacheMiss($regionName, $cacheKeys->identifiers[$index]); |
|
130 | } |
||
131 | |||
132 | 2 | return null; |
|
133 | } |
||
134 | |||
135 | 30 | if ($this->cacheLogger !== null) { |
|
136 | 28 | $this->cacheLogger->entityCacheHit($regionName, $cacheKeys->identifiers[$index]); |
|
137 | } |
||
138 | |||
139 | 30 | if ( ! $hasRelation) { |
|
140 | 22 | $result[$index] = $this->uow->createEntity($entityEntry->class, $entityEntry->resolveAssociationEntries($this->em), self::$hints); |
|
141 | |||
142 | 22 | continue; |
|
143 | } |
||
144 | |||
145 | 8 | $data = $entityEntry->data; |
|
146 | |||
147 | 8 | foreach ($entry['associations'] as $name => $assoc) { |
|
148 | 8 | $assocPersister = $this->uow->getEntityPersister($assoc['targetEntity']); |
|
149 | 8 | $assocRegion = $assocPersister->getCacheRegion(); |
|
150 | 8 | $assocMetadata = $this->em->getClassMetadata($assoc['targetEntity']); |
|
151 | |||
152 | 8 | if ($assoc['type'] & ClassMetadata::TO_ONE) { |
|
153 | |||
154 | 4 | if (($assocEntry = $assocRegion->get($assocKey = new EntityCacheKey($assocMetadata->rootEntityName, $assoc['identifier']))) === null) { |
|
155 | |||
156 | 1 | if ($this->cacheLogger !== null) { |
|
157 | 1 | $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKey); |
|
158 | } |
||
159 | |||
160 | 1 | $this->uow->hydrationComplete(); |
|
161 | |||
162 | 1 | return null; |
|
163 | } |
||
164 | |||
165 | 3 | $data[$name] = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints); |
|
166 | |||
167 | 3 | if ($this->cacheLogger !== null) { |
|
168 | 3 | $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKey); |
|
169 | } |
||
170 | |||
171 | 3 | continue; |
|
172 | } |
||
173 | |||
174 | 4 | if ( ! isset($assoc['list']) || empty($assoc['list'])) { |
|
175 | continue; |
||
176 | } |
||
177 | |||
178 | 4 | $generateKeys = function ($id) use ($assocMetadata): EntityCacheKey { |
|
179 | 4 | return new EntityCacheKey($assocMetadata->rootEntityName, $id); |
|
180 | 4 | }; |
|
181 | |||
182 | 4 | $collection = new PersistentCollection($this->em, $assocMetadata, new ArrayCollection()); |
|
183 | 4 | $assocKeys = new CollectionCacheEntry(array_map($generateKeys, $assoc['list'])); |
|
184 | 4 | $assocEntries = $assocRegion->getMultiple($assocKeys); |
|
185 | |||
186 | 4 | foreach ($assoc['list'] as $assocIndex => $assocId) { |
|
187 | 4 | $assocEntry = is_array($assocEntries) && array_key_exists($assocIndex, $assocEntries) ? $assocEntries[$assocIndex] : null; |
|
188 | |||
189 | 4 | if ($assocEntry === null) { |
|
190 | 1 | if ($this->cacheLogger !== null) { |
|
191 | 1 | $this->cacheLogger->entityCacheMiss($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); |
|
192 | } |
||
193 | |||
194 | 1 | $this->uow->hydrationComplete(); |
|
195 | |||
196 | 1 | return null; |
|
197 | } |
||
198 | |||
199 | 3 | $element = $this->uow->createEntity($assocEntry->class, $assocEntry->resolveAssociationEntries($this->em), self::$hints); |
|
200 | |||
201 | 3 | $collection->hydrateSet($assocIndex, $element); |
|
202 | |||
203 | 3 | if ($this->cacheLogger !== null) { |
|
204 | 3 | $this->cacheLogger->entityCacheHit($assocRegion->getName(), $assocKeys->identifiers[$assocIndex]); |
|
205 | } |
||
206 | } |
||
207 | |||
208 | 3 | $data[$name] = $collection; |
|
209 | |||
210 | 3 | $collection->setInitialized(true); |
|
211 | } |
||
212 | |||
213 | 6 | $result[$index] = $this->uow->createEntity($entityEntry->class, $data, self::$hints); |
|
214 | } |
||
215 | |||
216 | 28 | $this->uow->hydrationComplete(); |
|
217 | |||
218 | 28 | return $result; |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | 57 | public function put(QueryCacheKey $key, ResultSetMapping $rsm, $result, array $hints = []) |
|
321 | |||
322 | /** |
||
323 | * @param \Doctrine\ORM\Cache\QueryCacheKey $key |
||
324 | * @param array $assoc |
||
325 | * @param mixed $assocValue |
||
326 | * |
||
327 | * @return array|null |
||
328 | */ |
||
329 | 13 | private function storeAssociationCache(QueryCacheKey $key, array $assoc, $assocValue) |
|
377 | |||
378 | /** |
||
379 | * @param \Doctrine\ORM\Query\ResultSetMapping $rsm |
||
380 | * @param string $assocAlias |
||
381 | * @param object $entity |
||
382 | * |
||
383 | * @return array|object |
||
384 | */ |
||
385 | 15 | private function getAssociationValue(ResultSetMapping $rsm, $assocAlias, $entity) |
|
406 | |||
407 | /** |
||
408 | * @param mixed $value |
||
409 | * @param array $path |
||
410 | * |
||
411 | * @return array|object|null |
||
412 | */ |
||
413 | 15 | private function getAssociationPathValue($value, array $path) |
|
441 | |||
442 | /** |
||
443 | * {@inheritdoc} |
||
444 | */ |
||
445 | 48 | public function clear() |
|
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | 28 | public function getRegion() |
|
457 | } |
||
458 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: