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 | 200 | 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 | 200 | $primers = array_filter($primers); |
|
| 106 | |||
| 107 | 200 | if ( ! empty($primers)) { |
|
| 108 | 23 | $query['eagerCursor'] = true; |
|
| 109 | } |
||
| 110 | |||
| 111 | 200 | if ( ! empty($query['eagerCursor'])) { |
|
| 112 | 24 | $query['useIdentifierKeys'] = false; |
|
| 113 | } |
||
| 114 | |||
| 115 | 200 | parent::__construct($collection, $query, $options); |
|
| 116 | 200 | $this->dm = $dm; |
|
| 117 | 200 | $this->class = $class; |
|
| 118 | 200 | $this->hydrate = $hydrate; |
|
| 119 | 200 | $this->primers = $primers; |
|
| 120 | 200 | $this->requireIndexes = $requireIndexes; |
|
| 121 | |||
| 122 | 200 | $this->setReadOnly($readOnly); |
|
| 123 | 200 | $this->setRefresh($refresh); |
|
| 124 | |||
| 125 | 200 | if (isset($query['slaveOkay'])) { |
|
| 126 | 4 | $this->unitOfWorkHints[self::HINT_SLAVE_OKAY] = $query['slaveOkay']; |
|
| 127 | } |
||
| 128 | |||
| 129 | 200 | 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 | 200 | } |
|
| 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 | 200 | public function setReadOnly($readOnly) |
|
| 174 | { |
||
| 175 | 200 | $this->unitOfWorkHints[Query::HINT_READ_ONLY] = (boolean) $readOnly; |
|
| 176 | 200 | } |
|
| 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 | 200 | 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() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Check if this query is indexed. |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | * |
||
| 216 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
| 217 | */ |
||
| 218 | 8 | public function isIndexed() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Gets an array of the unindexed fields in this query. |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | * |
||
| 238 | * @deprecated method was deprecated in 1.2 and will be removed in 2.0 |
||
| 239 | */ |
||
| 240 | 6 | public function getUnindexedFields() |
|
| 255 | |||
| 256 | /** |
||
| 257 | * Execute the query and returns the results. |
||
| 258 | * |
||
| 259 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 260 | * @return mixed |
||
| 261 | */ |
||
| 262 | 154 | public function execute() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Prepare the Cursor returned by {@link Query::execute()}. |
||
| 318 | * |
||
| 319 | * This method will wrap the base Cursor with an ODM Cursor or EagerCursor, |
||
| 320 | * and set the hydrate option and UnitOfWork hints. This occurs in addition |
||
| 321 | * to any preparation done by the base Query class. |
||
| 322 | * |
||
| 323 | * @see \Doctrine\MongoDB\Cursor::prepareCursor() |
||
| 324 | * @param BaseCursor $cursor |
||
| 325 | * @return CursorInterface |
||
| 326 | */ |
||
| 327 | 133 | protected function prepareCursor(BaseCursor $cursor) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Return whether queries on this document should require indexes. |
||
| 348 | * |
||
| 349 | * @return boolean |
||
| 350 | */ |
||
| 351 | 154 | private function isIndexRequired() |
|
| 355 | } |
||
| 356 |
If you suppress an error, we recommend checking for the error condition explicitly: