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:
1 | <?php |
||
35 | class OneToManyPersister extends AbstractCollectionPersister |
||
36 | { |
||
37 | /** |
||
38 | * {@inheritdoc} |
||
39 | */ |
||
40 | 2 | public function delete(PersistentCollection $collection) |
|
41 | { |
||
42 | // The only valid case here is when you have weak entities. In this |
||
43 | // scenario, you have @OneToMany with orphanRemoval=true, and replacing |
||
44 | // the entire collection with a new would trigger this operation. |
||
45 | 2 | $mapping = $collection->getMapping(); |
|
46 | |||
47 | 2 | if ( ! $mapping['orphanRemoval']) { |
|
48 | // Handling non-orphan removal should never happen, as @OneToMany |
||
49 | // can only be inverse side. For owning side one to many, it is |
||
50 | // required to have a join table, which would classify as a ManyToManyPersister. |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | 2 | $targetClass = $this->em->getClassMetadata($mapping['targetEntity']); |
|
55 | |||
56 | 2 | return $targetClass->isInheritanceTypeJoined() |
|
57 | 1 | ? $this->deleteJoinedEntityCollection($collection) |
|
58 | 2 | : $this->deleteEntityCollection($collection); |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 368 | public function update(PersistentCollection $collection) |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 2 | public function get(PersistentCollection $collection, $index) |
|
76 | { |
||
77 | 2 | $mapping = $collection->getMapping(); |
|
78 | |||
79 | 2 | if ( ! isset($mapping['indexBy'])) { |
|
80 | throw new \BadMethodCallException("Selecting a collection by index is only supported on indexed collections."); |
||
81 | } |
||
82 | |||
83 | 2 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
|
84 | |||
85 | 2 | return $persister->load( |
|
86 | [ |
||
87 | 2 | $mapping['mappedBy'] => $collection->getOwner(), |
|
88 | 2 | $mapping['indexBy'] => $index |
|
89 | ], |
||
90 | 2 | null, |
|
91 | 2 | $mapping, |
|
92 | 2 | [], |
|
93 | 2 | null, |
|
94 | 2 | 1 |
|
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 14 | View Code Duplication | public function count(PersistentCollection $collection) |
102 | { |
||
103 | 14 | $mapping = $collection->getMapping(); |
|
104 | 14 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
|
105 | |||
106 | // only works with single id identifier entities. Will throw an |
||
107 | // exception in Entity Persisters if that is not the case for the |
||
108 | // 'mappedBy' field. |
||
109 | 14 | $criteria = new Criteria(Criteria::expr()->eq($mapping['mappedBy'], $collection->getOwner())); |
|
110 | |||
111 | 14 | return $persister->count($criteria); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | 5 | View Code Duplication | public function slice(PersistentCollection $collection, $offset, $length = null) |
118 | { |
||
119 | 5 | $mapping = $collection->getMapping(); |
|
120 | 5 | $persister = $this->uow->getEntityPersister($mapping['targetEntity']); |
|
121 | |||
122 | 5 | return $persister->getOneToManyCollection($mapping, $collection->getOwner(), $offset, $length); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 4 | public function containsKey(PersistentCollection $collection, $key) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 10 | public function contains(PersistentCollection $collection, $element) |
|
168 | |||
169 | /** |
||
170 | * {@inheritdoc} |
||
171 | */ |
||
172 | 11 | public function removeElement(PersistentCollection $collection, $element) |
|
190 | |||
191 | /** |
||
192 | * {@inheritdoc} |
||
193 | */ |
||
194 | public function loadCriteria(PersistentCollection $collection, Criteria $criteria) |
||
198 | |||
199 | /** |
||
200 | * @param PersistentCollection $collection |
||
201 | * |
||
202 | * @return int |
||
203 | * |
||
204 | * @throws \Doctrine\DBAL\DBALException |
||
205 | */ |
||
206 | 1 | private function deleteEntityCollection(PersistentCollection $collection) |
|
225 | |||
226 | /** |
||
227 | * Delete Class Table Inheritance entities. |
||
228 | * A temporary table is needed to keep IDs to be deleted in both parent and child class' tables. |
||
229 | * |
||
230 | * Thanks Steve Ebersole (Hibernate) for idea on how to tackle reliably this scenario, we owe him a beer! =) |
||
231 | * |
||
232 | * @param PersistentCollection $collection |
||
233 | * |
||
234 | * @return int |
||
235 | * |
||
236 | * @throws \Doctrine\DBAL\DBALException |
||
237 | */ |
||
238 | 1 | private function deleteJoinedEntityCollection(PersistentCollection $collection) |
|
291 | } |
||
292 |
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: