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 |
||
| 31 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $description; |
||
| 37 | /** |
||
| 38 | * @var string[] |
||
| 39 | */ |
||
| 40 | protected $fieldTitles; |
||
| 41 | /** |
||
| 42 | * @var string[] |
||
| 43 | */ |
||
| 44 | protected $fieldDescriptions; |
||
| 45 | /** |
||
| 46 | * @var string[] |
||
| 47 | */ |
||
| 48 | protected $requiredFields = array(); |
||
| 49 | /** |
||
| 50 | * @var string[] |
||
| 51 | */ |
||
| 52 | protected $searchableFields = array(); |
||
| 53 | /** |
||
| 54 | * @var DocumentRepository |
||
| 55 | */ |
||
| 56 | private $repository; |
||
| 57 | /** |
||
| 58 | * @var Visitor |
||
| 59 | */ |
||
| 60 | private $visitor; |
||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $notModifiableOriginRecords; |
||
| 65 | /** |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | private $paginationDefaultLimit; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var boolean |
||
| 72 | */ |
||
| 73 | protected $filterByAuthUser; |
||
| 74 | |||
| 75 | 2 | /** |
|
| 76 | * @var string |
||
| 77 | 2 | */ |
|
| 78 | 2 | protected $filterByAuthField; |
|
| 79 | 2 | ||
| 80 | 2 | /** |
|
| 81 | 2 | * @var RqlTranslator |
|
| 82 | */ |
||
| 83 | protected $translator; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param Visitor $visitor rql query visitor |
||
| 87 | * @param RqlTranslator $translator Translator for query modification |
||
| 88 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 89 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 90 | */ |
||
| 91 | public function __construct( |
||
| 103 | |||
| 104 | 2 | /** |
|
| 105 | * get repository instance |
||
| 106 | * |
||
| 107 | * @return DocumentRepository |
||
| 108 | */ |
||
| 109 | public function getRepository() |
||
| 113 | |||
| 114 | /** |
||
| 115 | 1 | * create new app model |
|
| 116 | * |
||
| 117 | 1 | * @param DocumentRepository $repository Repository of countries |
|
| 118 | 1 | * |
|
| 119 | 1 | * @return \Graviton\RestBundle\Model\DocumentModel |
|
| 120 | */ |
||
| 121 | public function setRepository(DocumentRepository $repository) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * {@inheritDoc} |
||
| 130 | 1 | * |
|
| 131 | * @param Request $request The request object |
||
| 132 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 133 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 134 | * |
||
| 135 | * @return array |
||
| 136 | */ |
||
| 137 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 138 | 1 | { |
|
| 139 | $pageNumber = $request->query->get('page', 1); |
||
| 140 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
| 141 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
| 142 | 1 | ||
| 143 | 1 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
|
| 144 | 1 | $queryBuilder = $this->repository |
|
| 145 | ->createQueryBuilder(); |
||
| 146 | |||
| 147 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
| 148 | 1 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
|
| 149 | 1 | } |
|
| 150 | 1 | ||
| 151 | |||
| 152 | $searchableFields = $this->getSearchableFields(); |
||
| 153 | if (!is_null($schema)) { |
||
| 154 | $searchableFields = $schema->getSearchable(); |
||
| 155 | 1 | } |
|
| 156 | |||
| 157 | // *** do we have an RQL expression, do we need to filter data? |
||
| 158 | if ($request->attributes->get('hasRql', false)) { |
||
| 159 | $queryBuilder = $this->doRqlQuery( |
||
| 160 | $queryBuilder, |
||
| 161 | $this->translator->translateSearchQuery( |
||
| 162 | $request->attributes->get('rqlQuery'), |
||
| 163 | $searchableFields |
||
| 164 | 1 | ) |
|
| 165 | 1 | ); |
|
| 166 | 1 | } else { |
|
| 167 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 168 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 169 | 1 | $queryBuilder->find($this->repository->getDocumentName()); |
|
| 170 | 1 | } |
|
| 171 | |||
| 172 | 1 | // define offset and limit |
|
| 173 | 1 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
|
| 174 | 1 | $queryBuilder->skip($startAt); |
|
| 175 | 1 | } else { |
|
| 176 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
| 180 | $queryBuilder->limit($numberPerPage); |
||
| 181 | } else { |
||
| 182 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
| 183 | } |
||
| 184 | 1 | ||
| 185 | // Limit can not be negative nor null. |
||
| 186 | if ($numberPerPage < 1) { |
||
| 187 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * add a default sort on id if none was specified earlier |
||
| 192 | * |
||
| 193 | * not specifying something to sort on leads to very weird cases when fetching references |
||
| 194 | */ |
||
| 195 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 196 | $queryBuilder->sort('_id'); |
||
| 197 | } |
||
| 198 | |||
| 199 | // run query |
||
| 200 | $query = $queryBuilder->getQuery(); |
||
| 201 | $records = array_values($query->execute()->toArray()); |
||
| 202 | |||
| 203 | $totalCount = $query->count(); |
||
| 204 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
| 205 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
| 206 | if ($numPages > 1) { |
||
| 207 | 1 | $request->attributes->set('paging', true); |
|
| 208 | $request->attributes->set('page', $page); |
||
| 209 | 1 | $request->attributes->set('numPages', $numPages); |
|
| 210 | $request->attributes->set('startAt', $startAt); |
||
| 211 | $request->attributes->set('perPage', $numberPerPage); |
||
| 212 | $request->attributes->set('totalCount', $totalCount); |
||
| 213 | } |
||
| 214 | |||
| 215 | return $records; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 220 | 1 | * |
|
| 221 | * @return Object |
||
| 222 | 1 | */ |
|
| 223 | View Code Duplication | public function insertRecord($entity) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $documentId id of entity to find |
||
| 235 | * |
||
| 236 | * @return Object |
||
| 237 | */ |
||
| 238 | public function find($documentId) |
||
| 242 | 1 | ||
| 243 | /** |
||
| 244 | 1 | * {@inheritDoc} |
|
| 245 | 1 | * |
|
| 246 | 1 | * @param string $documentId id of entity to update |
|
| 247 | 1 | * @param Object $entity new entity |
|
| 248 | 1 | * |
|
| 249 | 1 | * @return Object |
|
| 250 | 1 | */ |
|
| 251 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
| 262 | 1 | ||
| 263 | /** |
||
| 264 | * {@inheritDoc} |
||
| 265 | * |
||
| 266 | * @param string $documentId id of entity to delete |
||
| 267 | * |
||
| 268 | * @return null|Object |
||
| 269 | */ |
||
| 270 | public function deleteRecord($documentId) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * get classname of entity |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getEntityClass() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritDoc} |
||
| 298 | * |
||
| 299 | * Currently this is being used to build the route id used for redirecting |
||
| 300 | * to newly made documents. It might benefit from having a different name |
||
| 301 | * for those purposes. |
||
| 302 | * |
||
| 303 | * We might use a convention based mapping here: |
||
| 304 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 305 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 306 | * |
||
| 307 | * @todo implement this in a more convention based manner |
||
| 308 | * |
||
| 309 | 8 | * @return string |
|
| 310 | */ |
||
| 311 | public function getConnectionName() |
||
| 317 | 3 | ||
| 318 | 1 | /** |
|
| 319 | * Does the actual query using the RQL Bundle. |
||
| 320 | 1 | * |
|
| 321 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 322 | 2 | * @param Query $query query from parser |
|
| 323 | 6 | * |
|
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 332 | 1 | ||
| 333 | 1 | /** |
|
| 334 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 335 | * |
||
| 336 | * @param Object $record record |
||
| 337 | * |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | protected function checkIfOriginRecord($record) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 358 | * |
||
| 359 | * @return int |
||
| 360 | */ |
||
| 361 | private function getDefaultLimit() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param Boolean $active active |
||
| 372 | * @param String $field field |
||
| 373 | * @return void |
||
| 374 | */ |
||
| 375 | public function setFilterByAuthUser($active, $field) |
||
| 380 | } |
||
| 381 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.