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 |
||
| 24 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $description; |
||
| 30 | /** |
||
| 31 | * @var string[] |
||
| 32 | */ |
||
| 33 | protected $fieldTitles; |
||
| 34 | /** |
||
| 35 | * @var string[] |
||
| 36 | */ |
||
| 37 | protected $fieldDescriptions; |
||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | protected $requiredFields = array(); |
||
| 42 | /** |
||
| 43 | * @var DocumentRepository |
||
| 44 | */ |
||
| 45 | private $repository; |
||
| 46 | /** |
||
| 47 | * @var Visitor |
||
| 48 | */ |
||
| 49 | private $visitor; |
||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $notModifiableOriginRecords; |
||
| 54 | /** |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | private $paginationDefaultLimit; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var boolean |
||
| 61 | */ |
||
| 62 | protected $filterByAuthUser; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $filterByAuthField; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param Visitor $visitor rql query visitor |
||
| 71 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 72 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context. |
||
| 73 | */ |
||
| 74 | 2 | public function __construct(Visitor $visitor, $notModifiableOriginRecords, $paginationDefaultLimit) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * get repository instance |
||
| 84 | * |
||
| 85 | * @return DocumentRepository |
||
| 86 | */ |
||
| 87 | public function getRepository() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * create new app model |
||
| 94 | * |
||
| 95 | * @param DocumentRepository $repository Repository of countries |
||
| 96 | * |
||
| 97 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 98 | */ |
||
| 99 | 2 | public function setRepository(DocumentRepository $repository) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritDoc} |
||
| 108 | * |
||
| 109 | * @param Request $request The request object |
||
| 110 | * @param SecurityUser $user SecurityUser Object |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 1 | public function findAll(Request $request, SecurityUser $user = null) |
|
| 115 | { |
||
| 116 | 1 | $pageNumber = $request->query->get('page', 1); |
|
| 117 | 1 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
|
| 118 | 1 | $startAt = ($pageNumber - 1) * $numberPerPage; |
|
| 119 | |||
| 120 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
| 121 | 1 | $queryBuilder = $this->repository |
|
| 122 | 1 | ->createQueryBuilder(); |
|
| 123 | |||
| 124 | 1 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
|
| 125 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
||
| 126 | } |
||
| 127 | |||
| 128 | // *** do we have an RQL expression, do we need to filter data? |
||
| 129 | 1 | if ($request->attributes->get('hasRql', false)) { |
|
| 130 | $queryBuilder = $this->doRqlQuery( |
||
| 131 | $queryBuilder, |
||
| 132 | $request->attributes->get('rqlQuery') |
||
| 133 | ); |
||
| 134 | } else { |
||
| 135 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 136 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 137 | 1 | $queryBuilder->find($this->repository->getDocumentName()); |
|
| 138 | } |
||
| 139 | |||
| 140 | // define offset and limit |
||
| 141 | 1 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
|
| 142 | 1 | $queryBuilder->skip($startAt); |
|
|
|
|||
| 143 | 1 | } else { |
|
| 144 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
| 145 | } |
||
| 146 | |||
| 147 | 1 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
|
| 148 | 1 | $queryBuilder->limit($numberPerPage); |
|
| 149 | 1 | } else { |
|
| 150 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * add a default sort on id if none was specified earlier |
||
| 155 | * |
||
| 156 | * not specifying something to sort on leads to very weird cases when fetching references |
||
| 157 | */ |
||
| 158 | 1 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
|
| 159 | 1 | $queryBuilder->sort('_id'); |
|
| 160 | 1 | } |
|
| 161 | |||
| 162 | // run query |
||
| 163 | 1 | $query = $queryBuilder->getQuery(); |
|
| 164 | 1 | $records = array_values($query->execute()->toArray()); |
|
| 165 | |||
| 166 | 1 | $totalCount = $query->count(); |
|
| 167 | 1 | $numPages = (int) ceil($totalCount / $numberPerPage); |
|
| 168 | 1 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
|
| 169 | 1 | if ($numPages > 1) { |
|
| 170 | $request->attributes->set('paging', true); |
||
| 171 | $request->attributes->set('page', $page); |
||
| 172 | $request->attributes->set('numPages', $numPages); |
||
| 173 | $request->attributes->set('startAt', $startAt); |
||
| 174 | $request->attributes->set('perPage', $numberPerPage); |
||
| 175 | $request->attributes->set('totalCount', $totalCount); |
||
| 176 | } |
||
| 177 | |||
| 178 | 1 | return $records; |
|
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 183 | * |
||
| 184 | * @return Object |
||
| 185 | */ |
||
| 186 | View Code Duplication | public function insertRecord($entity) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $documentId id of entity to find |
||
| 198 | * |
||
| 199 | * @return Object |
||
| 200 | */ |
||
| 201 | 1 | public function find($documentId) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * {@inheritDoc} |
||
| 208 | * |
||
| 209 | * @param string $documentId id of entity to update |
||
| 210 | * @param Object $entity new entity |
||
| 211 | * |
||
| 212 | * @return Object |
||
| 213 | */ |
||
| 214 | 1 | View Code Duplication | public function updateRecord($documentId, $entity) |
| 225 | |||
| 226 | /** |
||
| 227 | * {@inheritDoc} |
||
| 228 | * |
||
| 229 | * @param string $documentId id of entity to delete |
||
| 230 | * |
||
| 231 | * @return null|Object |
||
| 232 | */ |
||
| 233 | 1 | public function deleteRecord($documentId) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * get classname of entity |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | 1 | public function getEntityClass() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * {@inheritDoc} |
||
| 261 | * |
||
| 262 | * Currently this is being used to build the route id used for redirecting |
||
| 263 | * to newly made documents. It might benefit from having a different name |
||
| 264 | * for those purposes. |
||
| 265 | * |
||
| 266 | * We might use a convention based mapping here: |
||
| 267 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 268 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 269 | * |
||
| 270 | * @todo implement this in a more convention based manner |
||
| 271 | * |
||
| 272 | * @return string |
||
| 273 | */ |
||
| 274 | public function getConnectionName() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Does the actual query using the RQL Bundle. |
||
| 283 | * |
||
| 284 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 285 | * @param Query $query query from parser |
||
| 286 | * |
||
| 287 | * @return array |
||
| 288 | */ |
||
| 289 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 298 | * |
||
| 299 | * @param Object $record record |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | 7 | protected function checkIfOriginRecord($record) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | 1 | private function getDefaultLimit() |
|
| 332 | |||
| 333 | /** |
||
| 334 | * @param Boolean $active active |
||
| 335 | * @param String $field field |
||
| 336 | * @return void |
||
| 337 | */ |
||
| 338 | 2 | public function setFilterByAuthUser($active, $field) |
|
| 343 | } |
||
| 344 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: