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 |
||
37 | class Query extends \Doctrine\MongoDB\Query\Query |
||
38 | { |
||
39 | const HINT_REFRESH = 1; |
||
40 | const HINT_SLAVE_OKAY = 2; |
||
41 | const HINT_READ_PREFERENCE = 3; |
||
42 | const HINT_READ_PREFERENCE_TAGS = 4; |
||
43 | const HINT_READ_ONLY = 5; |
||
44 | |||
45 | /** |
||
46 | * The DocumentManager instance. |
||
47 | * |
||
48 | * @var DocumentManager |
||
49 | */ |
||
50 | private $dm; |
||
51 | |||
52 | /** |
||
53 | * The ClassMetadata instance. |
||
54 | * |
||
55 | * @var ClassMetadata |
||
56 | */ |
||
57 | private $class; |
||
58 | |||
59 | /** |
||
60 | * Whether to hydrate results as document class instances. |
||
61 | * |
||
62 | * @var boolean |
||
63 | */ |
||
64 | private $hydrate = true; |
||
65 | |||
66 | /** |
||
67 | * Array of primer Closure instances. |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | private $primers = array(); |
||
72 | |||
73 | /** |
||
74 | * Whether or not to require indexes. |
||
75 | * |
||
76 | * @var boolean |
||
77 | */ |
||
78 | private $requireIndexes; |
||
79 | |||
80 | /** |
||
81 | * Hints for UnitOfWork behavior. |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | private $unitOfWorkHints = array(); |
||
86 | |||
87 | /** |
||
88 | * Constructor. |
||
89 | * |
||
90 | * Please note that $requireIndexes was deprecated in 1.2 and will be removed in 2.0 |
||
91 | * |
||
92 | * @param DocumentManager $dm |
||
93 | * @param ClassMetadata $class |
||
94 | * @param Collection $collection |
||
95 | * @param array $query |
||
96 | * @param array $options |
||
97 | * @param boolean $hydrate |
||
98 | * @param boolean $refresh |
||
99 | * @param array $primers |
||
100 | * @param null $requireIndexes deprecated |
||
101 | * @param boolean $readOnly |
||
102 | */ |
||
103 | 191 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = array(), array $options = array(), $hydrate = true, $refresh = false, array $primers = array(), $requireIndexes = null, $readOnly = false) |
|
104 | { |
||
105 | 191 | $primers = array_filter($primers); |
|
106 | |||
107 | 191 | if ( ! empty($primers)) { |
|
108 | 19 | $query['eagerCursor'] = true; |
|
109 | } |
||
110 | |||
111 | 191 | if ( ! empty($query['eagerCursor'])) { |
|
112 | 19 | $query['useIdentifierKeys'] = false; |
|
113 | } |
||
114 | |||
115 | 191 | parent::__construct($collection, $query, $options); |
|
116 | 191 | $this->dm = $dm; |
|
117 | 191 | $this->class = $class; |
|
118 | 191 | $this->hydrate = $hydrate; |
|
119 | 191 | $this->primers = $primers; |
|
120 | 191 | $this->requireIndexes = $requireIndexes; |
|
121 | |||
122 | 191 | $this->setReadOnly($readOnly); |
|
123 | 191 | $this->setRefresh($refresh); |
|
124 | |||
125 | 191 | if (isset($query['slaveOkay'])) { |
|
126 | 4 | $this->unitOfWorkHints[self::HINT_SLAVE_OKAY] = $query['slaveOkay']; |
|
127 | } |
||
128 | |||
129 | 191 | if (isset($query['readPreference'])) { |
|
130 | 3 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE] = $query['readPreference']; |
|
131 | 3 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE_TAGS] = $query['readPreferenceTags']; |
|
132 | } |
||
133 | 191 | } |
|
134 | |||
135 | /** |
||
136 | * Gets the DocumentManager instance. |
||
137 | * |
||
138 | * @return DocumentManager $dm |
||
139 | */ |
||
140 | public function getDocumentManager() |
||
141 | { |
||
142 | return $this->dm; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Gets the ClassMetadata instance. |
||
147 | * |
||
148 | * @return ClassMetadata $class |
||
149 | */ |
||
150 | public function getClass() |
||
151 | { |
||
152 | return $this->class; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Sets whether or not to hydrate the documents to objects. |
||
157 | * |
||
158 | * @param boolean $hydrate |
||
159 | */ |
||
160 | public function setHydrate($hydrate) |
||
161 | { |
||
162 | $this->hydrate = (boolean) $hydrate; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Set whether documents should be registered in UnitOfWork. If document would |
||
167 | * already be managed it will be left intact and new instance returned. |
||
168 | * |
||
169 | * This option has no effect if hydration is disabled. |
||
170 | * |
||
171 | * @param boolean $readOnly |
||
172 | */ |
||
173 | 191 | public function setReadOnly($readOnly) |
|
174 | { |
||
175 | 191 | $this->unitOfWorkHints[Query::HINT_READ_ONLY] = (boolean) $readOnly; |
|
176 | 191 | } |
|
177 | |||
178 | /** |
||
179 | * Set whether to refresh hydrated documents that are already in the |
||
180 | * identity map. |
||
181 | * |
||
182 | * This option has no effect if hydration is disabled. |
||
183 | * |
||
184 | * @param boolean $refresh |
||
185 | */ |
||
186 | 191 | public function setRefresh($refresh) |
|
190 | |||
191 | /** |
||
192 | * Gets the fields involved in this query. |
||
193 | * |
||
194 | * @return array $fields An array of fields names used in this query. |
||
195 | * |
||
196 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
197 | */ |
||
198 | 22 | public function getFieldsInQuery() |
|
206 | |||
207 | /** |
||
208 | * Check if this query is indexed. |
||
209 | * |
||
210 | * @return bool |
||
211 | * |
||
212 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
213 | */ |
||
214 | 8 | public function isIndexed() |
|
224 | |||
225 | /** |
||
226 | * Gets an array of the unindexed fields in this query. |
||
227 | * |
||
228 | * @return array |
||
229 | * |
||
230 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
231 | */ |
||
232 | 6 | public function getUnindexedFields() |
|
243 | |||
244 | /** |
||
245 | * Execute the query and returns the results. |
||
246 | * |
||
247 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
248 | * @return mixed |
||
249 | */ |
||
250 | 151 | public function execute() |
|
303 | |||
304 | /** |
||
305 | * Prepare the Cursor returned by {@link Query::execute()}. |
||
306 | * |
||
307 | * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, |
||
308 | * and set the hydrate option and UnitOfWork hints. This occurs in addition |
||
309 | * to any preparation done by the base Query class. |
||
310 | * |
||
311 | * @see \Doctrine\MongoDB\Cursor::prepareCursor() |
||
312 | * @param BaseCursor $cursor |
||
313 | * @return CursorInterface |
||
314 | */ |
||
315 | 130 | protected function prepareCursor(BaseCursor $cursor) |
|
333 | |||
334 | /** |
||
335 | * Return whether queries on this document should require indexes. |
||
336 | * |
||
337 | * @return boolean |
||
338 | */ |
||
339 | 151 | private function isIndexRequired() |
|
343 | } |
||
344 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.