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 |
||
32 | class Expr extends \Doctrine\MongoDB\Query\Expr |
||
33 | { |
||
34 | /** |
||
35 | * The DocumentManager instance for this query |
||
36 | * |
||
37 | * @var DocumentManager |
||
38 | */ |
||
39 | private $dm; |
||
40 | |||
41 | /** |
||
42 | * The ClassMetadata instance for the document being queried |
||
43 | * |
||
44 | * @var ClassMetadata |
||
45 | */ |
||
46 | private $class; |
||
47 | |||
48 | /** |
||
49 | * @param DocumentManager $dm |
||
50 | */ |
||
51 | 223 | public function __construct(DocumentManager $dm) |
|
55 | |||
56 | /** |
||
57 | * Sets ClassMetadata for document being queried. |
||
58 | * |
||
59 | * @param ClassMetadata $class |
||
60 | */ |
||
61 | 221 | public function setClassMetadata(ClassMetadata $class) |
|
65 | |||
66 | /** |
||
67 | * Checks that the value of the current field is a reference to the supplied document. |
||
68 | * |
||
69 | * @param object $document |
||
70 | * @return Expr |
||
71 | */ |
||
72 | 13 | public function references($document) |
|
73 | { |
||
74 | 13 | if ($this->currentField) { |
|
75 | 13 | $mapping = $this->getReferenceMapping(); |
|
76 | 11 | $dbRef = $this->dm->createDBRef($document, $mapping); |
|
77 | 11 | $storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
|
78 | |||
79 | 11 | View Code Duplication | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_ID) { |
|
|||
80 | 5 | $this->query[$mapping['name']] = $dbRef; |
|
81 | } else { |
||
82 | 8 | $keys = array('ref' => true, 'id' => true, 'db' => true); |
|
83 | |||
84 | 8 | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
|
85 | 3 | unset($keys['db']); |
|
86 | } |
||
87 | |||
88 | 8 | if (isset($mapping['targetDocument'])) { |
|
89 | 4 | unset($keys['ref'], $keys['db']); |
|
90 | } |
||
91 | |||
92 | 8 | foreach ($keys as $key => $value) { |
|
93 | 11 | $this->query[$this->currentField . '.$' . $key] = $dbRef['$' . $key]; |
|
94 | } |
||
95 | } |
||
96 | } else { |
||
97 | $dbRef = $this->dm->createDBRef($document); |
||
98 | $this->query = $dbRef; |
||
99 | } |
||
100 | |||
101 | 11 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * Checks that the current field includes a reference to the supplied document. |
||
106 | * |
||
107 | * @param object $document |
||
108 | * @return Expr |
||
109 | */ |
||
110 | 7 | public function includesReferenceTo($document) |
|
111 | { |
||
112 | 7 | if ($this->currentField) { |
|
113 | 7 | $mapping = $this->getReferenceMapping(); |
|
114 | 5 | $dbRef = $this->dm->createDBRef($document, $mapping); |
|
115 | 5 | $storeAs = array_key_exists('storeAs', $mapping) ? $mapping['storeAs'] : null; |
|
116 | |||
117 | 5 | View Code Duplication | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_ID) { |
118 | 3 | $this->query[$mapping['name']] = $dbRef; |
|
119 | } else { |
||
120 | 4 | $keys = array('ref' => true, 'id' => true, 'db' => true); |
|
121 | |||
122 | 4 | if ($storeAs === ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF) { |
|
123 | 2 | unset($keys['db']); |
|
124 | } |
||
125 | |||
126 | 4 | if (isset($mapping['targetDocument'])) { |
|
127 | 2 | unset($keys['ref'], $keys['db']); |
|
128 | } |
||
129 | |||
130 | 4 | foreach ($keys as $key => $value) { |
|
131 | 5 | $this->query[$this->currentField]['$elemMatch']['$' . $key] = $dbRef['$' . $key]; |
|
132 | } |
||
133 | } |
||
134 | } else { |
||
135 | $dbRef = $this->dm->createDBRef($document); |
||
136 | $this->query['$elemMatch'] = $dbRef; |
||
137 | } |
||
138 | |||
139 | 5 | return $this; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Gets prepared query part of expression. |
||
144 | * |
||
145 | * @return array |
||
146 | */ |
||
147 | 209 | public function getQuery() |
|
153 | |||
154 | /** |
||
155 | * Gets prepared newObj part of expression. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | 191 | public function getNewObj() |
|
165 | |||
166 | /** |
||
167 | * Gets reference mapping for current field from current class or its descendants. |
||
168 | * |
||
169 | * @return array |
||
170 | * @throws MappingException |
||
171 | */ |
||
172 | 20 | private function getReferenceMapping() |
|
198 | } |
||
199 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.