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 |
||
| 16 | final class Query |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The Doctine MongoDB connection. |
||
| 20 | * |
||
| 21 | * @var Connection |
||
| 22 | */ |
||
| 23 | private $connection; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The query/database operations formatter. |
||
| 27 | * |
||
| 28 | * @var Formatter |
||
| 29 | */ |
||
| 30 | private $formatter; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Constructor. |
||
| 34 | * |
||
| 35 | * @param Connection $connection |
||
| 36 | * @param Formatter $formatter |
||
| 37 | */ |
||
| 38 | public function __construct(Connection $connection, Formatter $formatter) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Creates a builder object for querying MongoDB based on the provided metadata. |
||
| 46 | * |
||
| 47 | * @param EntityMetadata $metadata |
||
| 48 | * @return QueryBuilder |
||
| 49 | */ |
||
| 50 | public function createQueryBuilder(EntityMetadata $metadata) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Executes a delete for the provided metadata and criteria. |
||
| 57 | * |
||
| 58 | * @param EntityMetadata $metadata |
||
| 59 | * @param Store $store |
||
| 60 | * @param array $criteria |
||
| 61 | * @return array|bool |
||
| 62 | */ |
||
| 63 | View Code Duplication | public function executeDelete(EntityMetadata $metadata, Store $store, array $criteria) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Finds records from the database based on the provided metadata and criteria. |
||
| 76 | * |
||
| 77 | * @param EntityMetadata $metadata The model metadata that the database should query against. |
||
| 78 | * @param Store $store The store. |
||
| 79 | * @param array $criteria The query criteria. |
||
| 80 | * @param array $fields Fields to include/exclude. |
||
| 81 | * @param array $sort The sort criteria. |
||
| 82 | * @param int $offset The starting offset, aka the number of Models to skip. |
||
| 83 | * @param int $limit The number of Models to limit. |
||
| 84 | * @return \Doctrine\MongoDB\Cursor |
||
| 85 | */ |
||
| 86 | public function executeFind(EntityMetadata $metadata, Store $store, array $criteria, array $fields = [], array $sort = [], $offset = 0, $limit = 0) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Executes an insert for the provided metadata. |
||
| 105 | * |
||
| 106 | * @param EntityMetadata $metadata |
||
| 107 | * @param array $toInsert |
||
| 108 | * @return array|bool |
||
| 109 | */ |
||
| 110 | public function executeInsert(EntityMetadata $metadata, array $toInsert) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Updates a record from the database based on the provided metadata and criteria. |
||
| 122 | * |
||
| 123 | * @param EntityMetadata $metadata The model metadata that the database should query against. |
||
| 124 | * @param Store $store The store. |
||
| 125 | * @param array $criteria The query criteria. |
||
| 126 | * @param array $toUpdate The data to update. |
||
| 127 | * @return array|bool |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function executeUpdate(EntityMetadata $metadata, Store $store, array $criteria, array $toUpdate) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @return Formatter |
||
| 143 | */ |
||
| 144 | public function getFormatter() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Gets standard database retrieval criteria for an inverse relationship. |
||
| 151 | * |
||
| 152 | * @param EntityMetadata $owner |
||
| 153 | * @param EntityMetadata $related |
||
| 154 | * @param string|array $identifiers |
||
| 155 | * @param string $inverseField |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | public function getInverseCriteria(EntityMetadata $owner, EntityMetadata $related, $identifiers, $inverseField) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Gets the MongoDB Collection object for a Model. |
||
| 179 | * |
||
| 180 | * @param EntityMetadata $metadata |
||
| 181 | * @return \Doctrine\MongoDB\Collection |
||
| 182 | */ |
||
| 183 | public function getModelCollection(EntityMetadata $metadata) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Gets standard database retrieval criteria for an entity and the provided identifiers. |
||
| 193 | * |
||
| 194 | * @param EntityMetadata $metadata The entity to retrieve database records for. |
||
| 195 | * @param string|array|null $identifiers The IDs to query. |
||
| 196 | * @return array |
||
| 197 | */ |
||
| 198 | public function getRetrieveCritiera(EntityMetadata $metadata, $identifiers = null) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Appends projection fields to a Query Builder. |
||
| 215 | * |
||
| 216 | * @param QueryBuilder $builder |
||
| 217 | * @param array $fields |
||
| 218 | * @return self |
||
| 219 | */ |
||
| 220 | private function appendFields(QueryBuilder $builder, array $fields) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Appends offset and limit criteria to a Query Builder |
||
| 232 | * |
||
| 233 | * @param QueryBuilder $builder |
||
| 234 | * @param int $limit |
||
| 235 | * @param int $offset |
||
| 236 | * @return self |
||
| 237 | */ |
||
| 238 | private function appendLimitAndOffset(QueryBuilder $builder, $limit, $offset) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Appends text search score and sorting to a Query Builder. |
||
| 255 | * |
||
| 256 | * @param QueryBuilder $builder |
||
| 257 | * @param array $criteria |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | private function appendSearch(QueryBuilder $builder, array $criteria) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Appends sorting criteria to a Query Builder. |
||
| 272 | * |
||
| 273 | * @param QueryBuilder $builder |
||
| 274 | * @param array $sort |
||
| 275 | * @return self |
||
| 276 | */ |
||
| 277 | private function appendSort(QueryBuilder $builder, array $sort) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Determines if the provided query criteria contains text search. |
||
| 287 | * |
||
| 288 | * @param array $criteria |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | private function isSearchQuery(array $criteria) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Prepares projection fields for a query and returns as a tuple. |
||
| 306 | * |
||
| 307 | * @param array $fields |
||
| 308 | * @return array |
||
| 309 | * @throws PersisterException |
||
| 310 | */ |
||
| 311 | private function prepareFields(array $fields) |
||
| 326 | } |
||
| 327 |