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 $toInsert |
||
|
|
|||
| 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) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Executes an insert for the provided metadata. |
||
| 104 | * |
||
| 105 | * @param EntityMetadata $metadata |
||
| 106 | * @param array $toInsert |
||
| 107 | * @return array|bool |
||
| 108 | */ |
||
| 109 | public function executeInsert(EntityMetadata $metadata, array $toInsert) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Updates a record from the database based on the provided metadata and criteria. |
||
| 121 | * |
||
| 122 | * @param EntityMetadata $metadata The model metadata that the database should query against. |
||
| 123 | * @param Store $store The store. |
||
| 124 | * @param array $criteria The query criteria. |
||
| 125 | * @param array $toUpdate The data to update. |
||
| 126 | * @return array|bool |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function executeUpdate(EntityMetadata $metadata, Store $store, array $criteria, array $toUpdate) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return Formatter |
||
| 142 | */ |
||
| 143 | public function getFormatter() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Gets standard database retrieval criteria for an inverse relationship. |
||
| 150 | * |
||
| 151 | * @param EntityMetadata $metadata The entity to retrieve database records for. |
||
| 152 | * @param string|array $identifiers The IDs to query. |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getInverseCriteria(EntityMetadata $owner, EntityMetadata $related, $identifiers, $inverseField) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Gets the MongoDB Collection object for a Model. |
||
| 174 | * |
||
| 175 | * @param EntityMetadata $metadata |
||
| 176 | * @return \Doctrine\MongoDB\Collection |
||
| 177 | */ |
||
| 178 | public function getModelCollection(EntityMetadata $metadata) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Gets standard database retrieval criteria for an entity and the provided identifiers. |
||
| 188 | * |
||
| 189 | * @param EntityMetadata $metadata The entity to retrieve database records for. |
||
| 190 | * @param string|array|null $identifiers The IDs to query. |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | 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 sorting criteria to a Query Builder. |
||
| 250 | * |
||
| 251 | * @param QueryBuilder $builder |
||
| 252 | * @param array $sort |
||
| 253 | * @return self |
||
| 254 | */ |
||
| 255 | private function appendSort(QueryBuilder $builder, array $sort) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Prepares projection fields for a query and returns as a tuple. |
||
| 265 | * |
||
| 266 | * @param array $fields |
||
| 267 | * @return array |
||
| 268 | * @throws PersisterException |
||
| 269 | */ |
||
| 270 | private function prepareFields(array $fields) |
||
| 285 | } |
||
| 286 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.