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:
Complex classes like DocumentModel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DocumentModel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $description; |
||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | protected $fieldTitles; |
||
| 42 | /** |
||
| 43 | * @var string[] |
||
| 44 | */ |
||
| 45 | protected $fieldDescriptions; |
||
| 46 | /** |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | protected $requiredFields = array(); |
||
| 50 | /** |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | protected $searchableFields = array(); |
||
| 54 | /** |
||
| 55 | * @var DocumentRepository |
||
| 56 | */ |
||
| 57 | private $repository; |
||
| 58 | /** |
||
| 59 | * @var Visitor |
||
| 60 | */ |
||
| 61 | private $visitor; |
||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $notModifiableOriginRecords; |
||
| 66 | /** |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $paginationDefaultLimit; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean |
||
| 73 | */ |
||
| 74 | protected $filterByAuthUser; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $filterByAuthField; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var RqlTranslator |
||
| 83 | */ |
||
| 84 | protected $translator; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param Visitor $visitor rql query visitor |
||
| 88 | * @param RqlTranslator $translator Translator for query modification |
||
| 89 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 90 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 91 | */ |
||
| 92 | public function __construct( |
||
| 93 | Visitor $visitor, |
||
| 94 | RqlTranslator $translator, |
||
| 95 | $notModifiableOriginRecords, |
||
| 96 | $paginationDefaultLimit |
||
| 97 | ) { |
||
| 98 | 4 | parent::__construct(); |
|
| 99 | $this->visitor = $visitor; |
||
| 100 | $this->translator = $translator; |
||
| 101 | $this->notModifiableOriginRecords = $notModifiableOriginRecords; |
||
| 102 | $this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
||
| 103 | } |
||
| 104 | 4 | ||
| 105 | 4 | /** |
|
| 106 | 4 | * get repository instance |
|
| 107 | 4 | * |
|
| 108 | 4 | * @return DocumentRepository |
|
| 109 | 4 | */ |
|
| 110 | public function getRepository() |
||
| 114 | |||
| 115 | /** |
||
| 116 | 2 | * create new app model |
|
| 117 | * |
||
| 118 | 2 | * @param DocumentRepository $repository Repository of countries |
|
| 119 | * |
||
| 120 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 121 | */ |
||
| 122 | public function setRepository(DocumentRepository $repository) |
||
| 123 | { |
||
| 124 | $this->repository = $repository; |
||
| 125 | |||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | 4 | ||
| 129 | /** |
||
| 130 | 4 | * {@inheritDoc} |
|
| 131 | 4 | * |
|
| 132 | * @param Request $request The request object |
||
| 133 | 4 | * @param SecurityUser $user SecurityUser Object |
|
|
|
|||
| 134 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 139 | { |
||
| 140 | $pageNumber = $request->query->get('page', 1); |
||
| 141 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
| 142 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
| 143 | // Only 1 search text node allowed. |
||
| 144 | $hasSearch = false; |
||
| 145 | |||
| 146 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
| 147 | $queryBuilder = $this->repository |
||
| 148 | ->createQueryBuilder(); |
||
| 149 | |||
| 150 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
| 151 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
||
| 152 | } |
||
| 153 | |||
| 154 | // *** do we have an RQL expression, do we need to filter data? |
||
| 155 | if ($request->attributes->get('hasRql', false)) { |
||
| 156 | $innerQuery = $request->attributes->get('rqlQuery')->getQuery(); |
||
| 157 | $xiagQuery = new XiagQuery(); |
||
| 158 | // can we perform a search in an index instead of filtering? |
||
| 159 | if ($innerQuery instanceof AbstractLogicOperatorNode) { |
||
| 160 | foreach ($innerQuery->getQueries() as $innerRql) { |
||
| 161 | if (!$hasSearch && $innerRql instanceof SearchNode) { |
||
| 162 | $searchString = implode('&', $innerRql->getSearchTerms()); |
||
| 163 | $queryBuilder->addAnd( |
||
| 164 | $queryBuilder->expr()->text($searchString) |
||
| 165 | ); |
||
| 166 | $hasSearch = true; |
||
| 167 | } else { |
||
| 168 | $xiagQuery->setQuery($innerRql); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } elseif ($this->hasCustomSearchIndex() && ($innerQuery instanceof SearchNode)) { |
||
| 172 | $searchString = implode('&', $innerQuery->getSearchTerms()); |
||
| 173 | $queryBuilder->addAnd( |
||
| 174 | $queryBuilder->expr()->text($searchString) |
||
| 175 | ); |
||
| 176 | $hasSearch = true; |
||
| 177 | } else { |
||
| 178 | if ($innerQuery instanceof AbstractScalarOperatorNode) { |
||
| 179 | $xiagQuery->setQuery($innerQuery); |
||
| 180 | } else { |
||
| 181 | /** @var AbstractLogicOperatorNode $innerQuery */ |
||
| 182 | foreach ($innerQuery->getQueries() as $innerRql) { |
||
| 183 | if (!$innerRql instanceof SearchNode) { |
||
| 184 | $xiagQuery->setQuery($innerRql); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $queryBuilder = $this->doRqlQuery( |
||
| 191 | $queryBuilder, |
||
| 192 | $xiagQuery |
||
| 193 | ); |
||
| 194 | } else { |
||
| 195 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 196 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 197 | $queryBuilder->find($this->repository->getDocumentName()); |
||
| 198 | } |
||
| 199 | |||
| 200 | // define offset and limit |
||
| 201 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
||
| 202 | $queryBuilder->skip($startAt); |
||
| 203 | } else { |
||
| 204 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
| 205 | } |
||
| 206 | |||
| 207 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
| 208 | $queryBuilder->limit($numberPerPage); |
||
| 209 | } else { |
||
| 210 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
| 211 | } |
||
| 212 | |||
| 213 | // Limit can not be negative nor null. |
||
| 214 | if ($numberPerPage < 1) { |
||
| 215 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * add a default sort on id if none was specified earlier |
||
| 220 | * |
||
| 221 | * not specifying something to sort on leads to very weird cases when fetching references |
||
| 222 | * If search node, sort by Score |
||
| 223 | * TODO Review this sorting, not 100% sure |
||
| 224 | */ |
||
| 225 | if ($hasSearch && !array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 226 | $queryBuilder->sortMeta('score', 'textScore'); |
||
| 227 | } elseif (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 228 | $queryBuilder->sort('_id'); |
||
| 229 | } |
||
| 230 | |||
| 231 | // run query |
||
| 232 | $query = $queryBuilder->getQuery(); |
||
| 233 | $records = array_values($query->execute()->toArray()); |
||
| 234 | |||
| 235 | $totalCount = $query->count(); |
||
| 236 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
| 237 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
| 238 | if ($numPages > 1) { |
||
| 239 | $request->attributes->set('paging', true); |
||
| 240 | $request->attributes->set('page', $page); |
||
| 241 | $request->attributes->set('numPages', $numPages); |
||
| 242 | $request->attributes->set('startAt', $startAt); |
||
| 243 | $request->attributes->set('perPage', $numberPerPage); |
||
| 244 | $request->attributes->set('totalCount', $totalCount); |
||
| 245 | } |
||
| 246 | |||
| 247 | return $records; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $prefix the prefix for custom text search indexes |
||
| 252 | * @return bool |
||
| 253 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 254 | */ |
||
| 255 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 256 | { |
||
| 257 | $collection = $this->repository->getDocumentManager()->getDocumentCollection($this->repository->getClassName()); |
||
| 258 | $indexesInfo = $collection->getIndexInfo(); |
||
| 259 | foreach ($indexesInfo as $indexInfo) { |
||
| 260 | if ($indexInfo['name']==$prefix.$collection->getName().'Index') { |
||
| 261 | return true; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | return false; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @return string the version of the MongoDB as a string |
||
| 269 | */ |
||
| 270 | private function getMongoDBVersion() |
||
| 271 | { |
||
| 272 | $buildInfo = $this->repository->getDocumentManager()->getDocumentDatabase( |
||
| 273 | $this->repository->getClassName() |
||
| 274 | )->command(['buildinfo'=>1]); |
||
| 275 | if (isset($buildInfo['version'])) { |
||
| 276 | return $buildInfo['version']; |
||
| 277 | } else { |
||
| 278 | return "unkown"; |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param \Graviton\I18nBundle\Document\Translatable $entity entity to insert |
||
| 284 | * |
||
| 285 | * @return Object |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function insertRecord($entity) |
|
| 288 | { |
||
| 289 | $this->checkIfOriginRecord($entity); |
||
| 290 | $manager = $this->repository->getDocumentManager(); |
||
| 291 | $manager->persist($entity); |
||
| 292 | $manager->flush($entity); |
||
| 293 | |||
| 294 | return $this->find($entity->getId()); |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param string $documentId id of entity to find |
||
| 299 | * |
||
| 300 | * @return Object |
||
| 301 | */ |
||
| 302 | public function find($documentId) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritDoc} |
||
| 309 | * |
||
| 310 | * @param string $documentId id of entity to update |
||
| 311 | * @param Object $entity new entity |
||
| 312 | * |
||
| 313 | * @return Object |
||
| 314 | 4 | */ |
|
| 315 | View Code Duplication | public function updateRecord($documentId, $entity) |
|
| 326 | |||
| 327 | /** |
||
| 328 | 2 | * {@inheritDoc} |
|
| 329 | * |
||
| 330 | * @param string $documentId id of entity to delete |
||
| 331 | 2 | * |
|
| 332 | 2 | * @return null|Object |
|
| 333 | */ |
||
| 334 | 2 | public function deleteRecord($documentId) |
|
| 349 | 2 | ||
| 350 | /** |
||
| 351 | * get classname of entity |
||
| 352 | * |
||
| 353 | * @return string|null |
||
| 354 | */ |
||
| 355 | public function getEntityClass() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * {@inheritDoc} |
||
| 366 | * |
||
| 367 | * Currently this is being used to build the route id used for redirecting |
||
| 368 | * to newly made documents. It might benefit from having a different name |
||
| 369 | * for those purposes. |
||
| 370 | * |
||
| 371 | * We might use a convention based mapping here: |
||
| 372 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 373 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 374 | * |
||
| 375 | * @todo implement this in a more convention based manner |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getConnectionName() |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Does the actual query using the RQL Bundle. |
||
| 388 | * |
||
| 389 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 390 | * @param Query $query query from parser |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 400 | 2 | ||
| 401 | 2 | /** |
|
| 402 | 2 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
|
| 403 | 2 | * |
|
| 404 | 2 | * @param Object $record record |
|
| 405 | * |
||
| 406 | * @return void |
||
| 407 | */ |
||
| 408 | protected function checkIfOriginRecord($record) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 426 | * |
||
| 427 | * @return int |
||
| 428 | */ |
||
| 429 | private function getDefaultLimit() |
||
| 437 | 4 | ||
| 438 | 4 | /** |
|
| 439 | 4 | * @param Boolean $active active |
|
| 440 | 4 | * @param String $field field |
|
| 441 | 4 | * @return void |
|
| 442 | */ |
||
| 443 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 448 | } |
||
| 449 |
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.