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) | ||
| 169 | |||
| 170 | /** | ||
| 171 | * Gets the MongoDB Collection object for a Model. | ||
| 172 | * | ||
| 173 | * @param EntityMetadata $metadata | ||
| 174 | * @return \Doctrine\MongoDB\Collection | ||
| 175 | */ | ||
| 176 | public function getModelCollection(EntityMetadata $metadata) | ||
| 183 | |||
| 184 | /** | ||
| 185 | * Gets standard database retrieval criteria for an entity and the provided identifiers. | ||
| 186 | * | ||
| 187 | * @param EntityMetadata $metadata The entity to retrieve database records for. | ||
| 188 | * @param string|array|null $identifiers The IDs to query. | ||
| 189 | * @return array | ||
| 190 | */ | ||
| 191 | public function getRetrieveCritiera(EntityMetadata $metadata, $identifiers = null) | ||
| 207 | |||
| 208 | /** | ||
| 209 | * Appends projection fields to a Query Builder. | ||
| 210 | * | ||
| 211 | * @param QueryBuilder $builder | ||
| 212 | * @param array $fields | ||
| 213 | * @return self | ||
| 214 | */ | ||
| 215 | private function appendFields(QueryBuilder $builder, array $fields) | ||
| 224 | |||
| 225 | /** | ||
| 226 | * Appends offset and limit criteria to a Query Builder | ||
| 227 | * | ||
| 228 | * @param QueryBuilder $builder | ||
| 229 | * @param int $limit | ||
| 230 | * @param int $offset | ||
| 231 | * @return self | ||
| 232 | */ | ||
| 233 | private function appendLimitAndOffset(QueryBuilder $builder, $limit, $offset) | ||
| 247 | |||
| 248 | /** | ||
| 249 | * Appends text search score and sorting to a Query Builder. | ||
| 250 | * | ||
| 251 | * @param QueryBuilder $builder | ||
| 252 | * @param array $criteria | ||
| 253 | * @return self | ||
| 254 | */ | ||
| 255 | private function appendSearch(QueryBuilder $builder, array $criteria) | ||
| 264 | |||
| 265 | /** | ||
| 266 | * Appends sorting criteria to a Query Builder. | ||
| 267 | * | ||
| 268 | * @param QueryBuilder $builder | ||
| 269 | * @param array $sort | ||
| 270 | * @return self | ||
| 271 | */ | ||
| 272 | private function appendSort(QueryBuilder $builder, array $sort) | ||
| 279 | |||
| 280 | /** | ||
| 281 | * Determines if the provided query criteria contains text search. | ||
| 282 | * | ||
| 283 | * @param array $criteria | ||
| 284 | * @return bool | ||
| 285 | */ | ||
| 286 | private function isSearchQuery(array $criteria) | ||
| 298 | |||
| 299 | /** | ||
| 300 | * Prepares projection fields for a query and returns as a tuple. | ||
| 301 | * | ||
| 302 | * @param array $fields | ||
| 303 | * @return array | ||
| 304 | * @throws PersisterException | ||
| 305 | */ | ||
| 306 | private function prepareFields(array $fields) | ||
| 321 | } | ||
| 322 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.