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 |
||
40 | class Query extends \Doctrine\MongoDB\Query\Query |
||
41 | { |
||
42 | const HINT_REFRESH = 1; |
||
43 | const HINT_SLAVE_OKAY = 2; |
||
44 | const HINT_READ_PREFERENCE = 3; |
||
45 | const HINT_READ_PREFERENCE_TAGS = 4; |
||
46 | |||
47 | /** |
||
48 | * The DocumentManager instance. |
||
49 | * |
||
50 | * @var DocumentManager |
||
51 | */ |
||
52 | private $dm; |
||
53 | |||
54 | /** |
||
55 | * The ClassMetadata instance. |
||
56 | * |
||
57 | * @var ClassMetadata |
||
58 | */ |
||
59 | private $class; |
||
60 | |||
61 | /** |
||
62 | * Whether to hydrate results as document class instances. |
||
63 | * |
||
64 | * @var boolean |
||
65 | */ |
||
66 | private $hydrate = true; |
||
67 | |||
68 | /** |
||
69 | * Array of primer Closure instances. |
||
70 | * |
||
71 | * @var array |
||
72 | */ |
||
73 | private $primers = array(); |
||
74 | |||
75 | /** |
||
76 | * Whether or not to require indexes. |
||
77 | * |
||
78 | * @var boolean |
||
79 | */ |
||
80 | private $requireIndexes; |
||
81 | |||
82 | /** |
||
83 | * Hints for UnitOfWork behavior. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $unitOfWorkHints = array(); |
||
88 | |||
89 | /** |
||
90 | * Constructor. |
||
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 |
||
101 | */ |
||
102 | 169 | public function __construct(DocumentManager $dm, ClassMetadata $class, Collection $collection, array $query = array(), array $options = array(), $hydrate = true, $refresh = false, array $primers = array(), $requireIndexes = null) |
|
103 | { |
||
104 | 169 | $primers = array_filter($primers); |
|
105 | |||
106 | 169 | if ( ! empty($primers)) { |
|
107 | 17 | $query['eagerCursor'] = true; |
|
108 | 17 | } |
|
109 | |||
110 | 169 | if ( ! empty($query['eagerCursor'])) { |
|
111 | 17 | $query['useIdentifierKeys'] = false; |
|
112 | 17 | } |
|
113 | |||
114 | 169 | parent::__construct($collection, $query, $options); |
|
115 | 169 | $this->dm = $dm; |
|
116 | 169 | $this->class = $class; |
|
117 | 169 | $this->hydrate = $hydrate; |
|
118 | 169 | $this->primers = $primers; |
|
119 | 169 | $this->requireIndexes = $requireIndexes; |
|
120 | |||
121 | 169 | $this->setRefresh($refresh); |
|
122 | |||
123 | 169 | if (isset($query['slaveOkay'])) { |
|
124 | 4 | $this->unitOfWorkHints[self::HINT_SLAVE_OKAY] = $query['slaveOkay']; |
|
125 | 4 | } |
|
126 | |||
127 | 169 | if (isset($query['readPreference'])) { |
|
128 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE] = $query['readPreference']; |
||
129 | $this->unitOfWorkHints[self::HINT_READ_PREFERENCE_TAGS] = $query['readPreferenceTags']; |
||
130 | } |
||
131 | 169 | } |
|
132 | |||
133 | /** |
||
134 | * Gets the DocumentManager instance. |
||
135 | * |
||
136 | * @return DocumentManager $dm |
||
137 | */ |
||
138 | public function getDocumentManager() |
||
142 | |||
143 | /** |
||
144 | * Gets the ClassMetadata instance. |
||
145 | * |
||
146 | * @return ClassMetadata $class |
||
147 | */ |
||
148 | public function getClass() |
||
152 | |||
153 | /** |
||
154 | * Sets whether or not to hydrate the documents to objects. |
||
155 | * |
||
156 | * @param boolean $hydrate |
||
157 | */ |
||
158 | public function setHydrate($hydrate) |
||
162 | |||
163 | /** |
||
164 | * Set whether to refresh hydrated documents that are already in the |
||
165 | * identity map. |
||
166 | * |
||
167 | * This option has no effect if hydration is disabled. |
||
168 | * |
||
169 | * @param boolean $refresh |
||
170 | */ |
||
171 | 169 | public function setRefresh($refresh) |
|
175 | |||
176 | /** |
||
177 | * Gets the fields involved in this query. |
||
178 | * |
||
179 | * @return array $fields An array of fields names used in this query. |
||
180 | */ |
||
181 | 22 | public function getFieldsInQuery() |
|
189 | |||
190 | /** |
||
191 | * Check if this query is indexed. |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | 8 | public function isIndexed() |
|
205 | |||
206 | /** |
||
207 | * Gets an array of the unindexed fields in this query. |
||
208 | * |
||
209 | * @return array |
||
210 | */ |
||
211 | 6 | public function getUnindexedFields() |
|
222 | |||
223 | /** |
||
224 | * Execute the query and returns the results. |
||
225 | * |
||
226 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
227 | * @return mixed |
||
228 | */ |
||
229 | 131 | public function execute() |
|
282 | |||
283 | /** |
||
284 | * Prepare the Cursor returned by {@link Query::execute()}. |
||
285 | * |
||
286 | * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, |
||
287 | * and set the hydrate option and UnitOfWork hints. This occurs in addition |
||
288 | * to any preparation done by the base Query class. |
||
289 | * |
||
290 | * @see \Doctrine\MongoDB\Cursor::prepareCursor() |
||
291 | * @param BaseCursor $cursor |
||
292 | * @return CursorInterface |
||
293 | */ |
||
294 | 112 | protected function prepareCursor(BaseCursor $cursor) |
|
312 | |||
313 | /** |
||
314 | * Return whether queries on this document should require indexes. |
||
315 | * |
||
316 | * @return boolean |
||
317 | */ |
||
318 | 131 | private function isIndexRequired() |
|
322 | } |
||
323 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: