Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like DefaultCache 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 DefaultCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class DefaultCache implements Cache |
||
37 | { |
||
38 | /** |
||
39 | * @var \Doctrine\ORM\EntityManagerInterface |
||
40 | */ |
||
41 | private $em; |
||
42 | |||
43 | /** |
||
44 | * @var \Doctrine\ORM\UnitOfWork |
||
45 | */ |
||
46 | private $uow; |
||
47 | |||
48 | /** |
||
49 | * @var \Doctrine\ORM\Cache\CacheFactory |
||
50 | */ |
||
51 | private $cacheFactory; |
||
52 | |||
53 | /** |
||
54 | * @var \Doctrine\ORM\Cache\QueryCache[] |
||
55 | */ |
||
56 | private $queryCaches = []; |
||
57 | |||
58 | /** |
||
59 | * @var \Doctrine\ORM\Cache\QueryCache |
||
60 | */ |
||
61 | private $defaultQueryCache; |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 283 | public function __construct(EntityManagerInterface $em) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 25 | public function getEntityCacheRegion($className) |
|
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | */ |
||
93 | 6 | View Code Duplication | public function getCollectionCacheRegion($className, $association) |
94 | { |
||
95 | 6 | $metadata = $this->em->getClassMetadata($className); |
|
96 | 6 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
|
97 | |||
98 | 6 | if ( ! ($persister instanceof CachedPersister)) { |
|
99 | 1 | return null; |
|
100 | } |
||
101 | |||
102 | 6 | return $persister->getCacheRegion(); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 68 | public function containsEntity($className, $identifier) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 4 | public function evictEntity($className, $identifier) |
|
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | */ |
||
138 | 29 | View Code Duplication | public function evictEntityRegion($className) |
139 | { |
||
140 | 29 | $metadata = $this->em->getClassMetadata($className); |
|
141 | 29 | $persister = $this->uow->getEntityPersister($metadata->rootEntityName); |
|
142 | |||
143 | 29 | if ( ! ($persister instanceof CachedPersister)) { |
|
144 | 2 | return; |
|
145 | } |
||
146 | |||
147 | 29 | $persister->getCacheRegion()->evictAll(); |
|
148 | 29 | } |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 48 | public function evictEntityRegions() |
|
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 18 | View Code Duplication | public function containsCollection($className, $association, $ownerIdentifier) |
172 | { |
||
173 | 18 | $metadata = $this->em->getClassMetadata($className); |
|
174 | 18 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
|
175 | |||
176 | 18 | if ( ! ($persister instanceof CachedPersister)) { |
|
177 | 1 | return false; |
|
178 | } |
||
179 | |||
180 | 18 | return $persister->getCacheRegion()->contains($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | 2 | View Code Duplication | public function evictCollection($className, $association, $ownerIdentifier) |
187 | { |
||
188 | 2 | $metadata = $this->em->getClassMetadata($className); |
|
189 | 2 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
|
190 | |||
191 | 2 | if ( ! ($persister instanceof CachedPersister)) { |
|
192 | 1 | return; |
|
193 | } |
||
194 | |||
195 | 2 | $persister->getCacheRegion()->evict($this->buildCollectionCacheKey($metadata, $association, $ownerIdentifier)); |
|
196 | 2 | } |
|
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | 9 | View Code Duplication | public function evictCollectionRegion($className, $association) |
202 | { |
||
203 | 9 | $metadata = $this->em->getClassMetadata($className); |
|
204 | 9 | $persister = $this->uow->getCollectionPersister($metadata->getAssociationMapping($association)); |
|
205 | |||
206 | 9 | if ( ! ($persister instanceof CachedPersister)) { |
|
207 | 1 | return; |
|
208 | } |
||
209 | |||
210 | 9 | $persister->getCacheRegion()->evictAll(); |
|
211 | 9 | } |
|
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | 46 | public function evictCollectionRegions() |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 1 | public function containsQuery($regionName) |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 3 | public function evictQueryRegion($regionName = null) |
|
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | 47 | public function evictQueryRegions() |
|
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | 63 | public function getQueryCache($regionName = null) |
|
291 | |||
292 | /** |
||
293 | * @param \Doctrine\ORM\Mapping\ClassMetadata $metadata The entity metadata. |
||
294 | * @param mixed $identifier The entity identifier. |
||
295 | * |
||
296 | * @return \Doctrine\ORM\Cache\EntityCacheKey |
||
297 | */ |
||
298 | 69 | View Code Duplication | private function buildEntityCacheKey(ClassMetadata $metadata, $identifier) |
306 | |||
307 | /** |
||
308 | * @param \Doctrine\ORM\Mapping\ClassMetadata $metadata The entity metadata. |
||
309 | * @param string $association The field name that represents the association. |
||
310 | * @param mixed $ownerIdentifier The identifier of the owning entity. |
||
311 | * |
||
312 | * @return \Doctrine\ORM\Cache\CollectionCacheKey |
||
313 | */ |
||
314 | 19 | View Code Duplication | private function buildCollectionCacheKey(ClassMetadata $metadata, $association, $ownerIdentifier) |
322 | |||
323 | /** |
||
324 | * @param \Doctrine\ORM\Mapping\ClassMetadata $metadata The entity metadata. |
||
325 | * @param mixed $identifier The entity identifier. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 74 | private function toIdentifierArray(ClassMetadata $metadata, $identifier) |
|
341 | |||
342 | } |
||
343 |
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: