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 |
||
| 34 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $description; |
||
| 40 | /** |
||
| 41 | * @var string[] |
||
| 42 | */ |
||
| 43 | protected $fieldTitles; |
||
| 44 | /** |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | protected $fieldDescriptions; |
||
| 48 | /** |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | protected $requiredFields = array(); |
||
| 52 | /** |
||
| 53 | * @var string[] |
||
| 54 | */ |
||
| 55 | protected $searchableFields = array(); |
||
| 56 | /** |
||
| 57 | * @var string[] |
||
| 58 | */ |
||
| 59 | protected $textIndexes = array(); |
||
| 60 | /** |
||
| 61 | * @var DocumentRepository |
||
| 62 | */ |
||
| 63 | private $repository; |
||
| 64 | /** |
||
| 65 | * @var Visitor |
||
| 66 | */ |
||
| 67 | private $visitor; |
||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $notModifiableOriginRecords; |
||
| 72 | /** |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | private $paginationDefaultLimit; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var boolean |
||
| 79 | */ |
||
| 80 | protected $filterByAuthUser; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $filterByAuthField; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var DocumentManager |
||
| 89 | */ |
||
| 90 | protected $manager; |
||
| 91 | |||
| 92 | /** @var EventDispatcher */ |
||
| 93 | protected $eventDispatcher; |
||
| 94 | |||
| 95 | /** @var $collectionCache */ |
||
| 96 | protected $cache; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param Visitor $visitor rql query visitor |
||
| 100 | * @param EventDispatcher $eventDispatcher Kernel event dispatcher |
||
| 101 | * @param CollectionCache $collectionCache Cache Service |
||
| 102 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 103 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination cnt |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 106 | Visitor $visitor, |
||
| 107 | $eventDispatcher, |
||
| 108 | CollectionCache $collectionCache, |
||
| 109 | $notModifiableOriginRecords, |
||
| 110 | $paginationDefaultLimit |
||
| 111 | ) { |
||
| 112 | parent::__construct(); |
||
| 113 | $this->visitor = $visitor; |
||
| 114 | $this->eventDispatcher = $eventDispatcher; |
||
| 115 | $this->notModifiableOriginRecords = $notModifiableOriginRecords; |
||
| 116 | $this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
||
| 117 | $this->cache = $collectionCache; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * get repository instance |
||
| 122 | * |
||
| 123 | * @return DocumentRepository |
||
| 124 | */ |
||
| 125 | public function getRepository() |
||
| 126 | { |
||
| 127 | return $this->repository; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * create new app model |
||
| 132 | * |
||
| 133 | * @param DocumentRepository $repository Repository of countries |
||
| 134 | * |
||
| 135 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 136 | */ |
||
| 137 | public function setRepository(DocumentRepository $repository) |
||
| 138 | { |
||
| 139 | $this->repository = $repository; |
||
| 140 | $this->manager = $repository->getDocumentManager(); |
||
| 141 | |||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritDoc} |
||
| 147 | * |
||
| 148 | * @param Request $request The request object |
||
| 149 | * |
||
| 150 | * @return array |
||
| 151 | */ |
||
| 152 | public function findAll(Request $request) |
||
| 153 | { |
||
| 154 | $pageNumber = $request->query->get('page', 1); |
||
| 155 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
| 156 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
| 157 | |||
| 158 | /** @var XiagQuery $xiagQuery */ |
||
| 159 | $xiagQuery = $request->attributes->get('rqlQuery'); |
||
| 160 | |||
| 161 | /** @var MongoBuilder $queryBuilder */ |
||
| 162 | $queryBuilder = $this->repository |
||
| 163 | ->createQueryBuilder(); |
||
| 164 | |||
| 165 | // Setting RQL Query |
||
| 166 | if ($xiagQuery) { |
||
| 167 | // Clean up Search rql param and set it as Doctrine query |
||
| 168 | if ($xiagQuery->getQuery() && $this->hasCustomSearchIndex() && (float) $this->getMongoDBVersion() >= 2.6) { |
||
| 169 | $searchQueries = $this->buildSearchQuery($xiagQuery, $queryBuilder); |
||
| 170 | $xiagQuery = $searchQueries['xiagQuery']; |
||
| 171 | $queryBuilder = $searchQueries['queryBuilder']; |
||
| 172 | } |
||
| 173 | $queryBuilder = $this->doRqlQuery( |
||
| 174 | $queryBuilder, |
||
| 175 | $xiagQuery |
||
| 176 | ); |
||
| 177 | } else { |
||
| 178 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 179 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 180 | $queryBuilder->find($this->repository->getDocumentName()); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** @var LimitNode $rqlLimit */ |
||
| 184 | $rqlLimit = $xiagQuery instanceof XiagQuery ? $xiagQuery->getLimit() : false; |
||
| 185 | |||
| 186 | // define offset and limit |
||
| 187 | if (!$rqlLimit || !$rqlLimit->getOffset()) { |
||
| 188 | $queryBuilder->skip($startAt); |
||
|
|
|||
| 189 | } else { |
||
| 190 | $startAt = (int) $rqlLimit->getOffset(); |
||
| 191 | $queryBuilder->skip($startAt); |
||
| 192 | } |
||
| 193 | |||
| 194 | if (!$rqlLimit || is_null($rqlLimit->getLimit())) { |
||
| 195 | $queryBuilder->limit($numberPerPage); |
||
| 196 | } else { |
||
| 197 | $numberPerPage = (int) $rqlLimit->getLimit(); |
||
| 198 | $queryBuilder->limit($numberPerPage); |
||
| 199 | } |
||
| 200 | |||
| 201 | // Limit can not be negative nor null. |
||
| 202 | if ($numberPerPage < 1) { |
||
| 203 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * add a default sort on id if none was specified earlier |
||
| 208 | * |
||
| 209 | * not specifying something to sort on leads to very weird cases when fetching references. |
||
| 210 | */ |
||
| 211 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 212 | $queryBuilder->sort('_id'); |
||
| 213 | } |
||
| 214 | |||
| 215 | // run query |
||
| 216 | $query = $queryBuilder->getQuery(); |
||
| 217 | $records = array_values($query->execute()->toArray()); |
||
| 218 | |||
| 219 | $totalCount = $query->count(); |
||
| 220 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
| 221 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
| 222 | if ($numPages > 1) { |
||
| 223 | $request->attributes->set('paging', true); |
||
| 224 | $request->attributes->set('page', $page); |
||
| 225 | $request->attributes->set('numPages', $numPages); |
||
| 226 | $request->attributes->set('startAt', $startAt); |
||
| 227 | $request->attributes->set('perPage', $numberPerPage); |
||
| 228 | $request->attributes->set('totalCount', $totalCount); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $records; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 236 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 240 | { |
||
| 241 | $innerQuery = $xiagQuery->getQuery(); |
||
| 242 | $hasSearch = false; |
||
| 243 | $nodes = []; |
||
| 244 | if ($innerQuery instanceof AbstractLogicOperatorNode) { |
||
| 245 | foreach ($innerQuery->getQueries() as $key => $innerRql) { |
||
| 246 | if ($innerRql instanceof SearchNode) { |
||
| 247 | if (!$hasSearch) { |
||
| 248 | $queryBuilder = $this->buildSearchTextQuery($queryBuilder, $innerRql); |
||
| 249 | $hasSearch = true; |
||
| 250 | } |
||
| 251 | } else { |
||
| 252 | $nodes[] = $innerRql; |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } elseif ($innerQuery instanceof SearchNode) { |
||
| 256 | $queryBuilder = $this->repository->createQueryBuilder(); |
||
| 257 | $queryBuilder = $this->buildSearchTextQuery($queryBuilder, $innerQuery); |
||
| 258 | $hasSearch = true; |
||
| 259 | } |
||
| 260 | // Remove the Search from RQL xiag |
||
| 261 | if ($hasSearch && $nodes) { |
||
| 262 | $newXiagQuery = new XiagQuery(); |
||
| 263 | if ($xiagQuery->getLimit()) { |
||
| 264 | $newXiagQuery->setLimit($xiagQuery->getLimit()); |
||
| 265 | } |
||
| 266 | if ($xiagQuery->getSelect()) { |
||
| 267 | $newXiagQuery->setSelect($xiagQuery->getSelect()); |
||
| 268 | } |
||
| 269 | if ($xiagQuery->getSort()) { |
||
| 270 | $newXiagQuery->setSort($xiagQuery->getSort()); |
||
| 271 | } |
||
| 272 | $binderClass = get_class($innerQuery); |
||
| 273 | /** @var AbstractLogicOperatorNode $newBinder */ |
||
| 274 | $newBinder = new $binderClass(); |
||
| 275 | foreach ($nodes as $node) { |
||
| 276 | $newBinder->addQuery($node); |
||
| 277 | } |
||
| 278 | $newXiagQuery->setQuery($newBinder); |
||
| 279 | // Reset original query, so that there is no Search param |
||
| 280 | $xiagQuery = $newXiagQuery; |
||
| 281 | } |
||
| 282 | if ($hasSearch) { |
||
| 283 | $queryBuilder->sortMeta('score', 'textScore'); |
||
| 284 | } |
||
| 285 | return [ |
||
| 286 | 'xiagQuery' => $xiagQuery, |
||
| 287 | 'queryBuilder' => $queryBuilder |
||
| 288 | ]; |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Check if collection has search indexes in DB |
||
| 293 | * |
||
| 294 | * @param string $prefix the prefix for custom text search indexes |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 298 | { |
||
| 299 | $metadata = $this->repository->getClassMetadata(); |
||
| 300 | $indexes = $metadata->getIndexes(); |
||
| 301 | if (count($indexes) < 1) { |
||
| 302 | return false; |
||
| 303 | } |
||
| 304 | $collectionsName = substr($metadata->getName(), strrpos($metadata->getName(), '\\') + 1); |
||
| 305 | $searchIndexName = $prefix.$collectionsName.'Index'; |
||
| 306 | // We reverse as normally the search index is the last. |
||
| 307 | foreach (array_reverse($indexes) as $index) { |
||
| 308 | if (array_key_exists('keys', $index) && array_key_exists($searchIndexName, $index['keys'])) { |
||
| 309 | return true; |
||
| 310 | } |
||
| 311 | } |
||
| 312 | return false; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Build Search text index |
||
| 317 | * |
||
| 318 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 319 | * @param SearchNode $searchNode Graviton Search node |
||
| 320 | * @return MongoBuilder |
||
| 321 | */ |
||
| 322 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 323 | { |
||
| 324 | $searchArr = []; |
||
| 325 | foreach ($searchNode->getSearchTerms() as $string) { |
||
| 326 | if (!empty(trim($string))) { |
||
| 327 | $searchArr[] = (strpos($string, '.') !== false) ? "\"{$string}\"" : $string; |
||
| 328 | } |
||
| 329 | } |
||
| 330 | if (!empty($searchArr)) { |
||
| 331 | $queryBuilder->addAnd($queryBuilder->expr()->text(implode(' ', $searchArr))); |
||
| 332 | } |
||
| 333 | return $queryBuilder; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return string the version of the MongoDB as a string |
||
| 338 | */ |
||
| 339 | private function getMongoDBVersion() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param object $entity entity to insert |
||
| 353 | * @param bool $returnEntity true to return entity |
||
| 354 | * @param bool $doFlush if we should flush or not after insert |
||
| 355 | * |
||
| 356 | * @return Object|null |
||
| 357 | */ |
||
| 358 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $documentId id of entity to find |
||
| 376 | * @param Request $request request |
||
| 377 | * |
||
| 378 | * @throws NotFoundException |
||
| 379 | * @return Object |
||
| 380 | */ |
||
| 381 | public function find($documentId, Request $request = null) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritDoc} |
||
| 404 | * |
||
| 405 | * @param string $documentId id of entity to update |
||
| 406 | * @param Object $entity new entity |
||
| 407 | * @param bool $returnEntity true to return entity |
||
| 408 | * |
||
| 409 | * @return Object|null |
||
| 410 | */ |
||
| 411 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritDoc} |
||
| 435 | * |
||
| 436 | * @param string|object $id id of entity to delete or entity instance |
||
| 437 | * |
||
| 438 | * @return null|Object |
||
| 439 | */ |
||
| 440 | public function deleteRecord($id) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Triggers a flush on the DocumentManager |
||
| 466 | * |
||
| 467 | * @param null $document optional document |
||
| 468 | * |
||
| 469 | * @return void |
||
| 470 | */ |
||
| 471 | public function flush($document = null) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * A low level delete without any checks |
||
| 478 | * |
||
| 479 | * @param mixed $id record id |
||
| 480 | * |
||
| 481 | * @return void |
||
| 482 | */ |
||
| 483 | private function deleteById($id) |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Checks in a performant way if a certain record id exists in the database |
||
| 495 | * |
||
| 496 | * @param mixed $id record id |
||
| 497 | * |
||
| 498 | * @return bool true if it exists, false otherwise |
||
| 499 | */ |
||
| 500 | public function recordExists($id) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 507 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 508 | * is a better way to get what you need. |
||
| 509 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 510 | * instance, make sure to pass false there. |
||
| 511 | * |
||
| 512 | * @param mixed $id record id |
||
| 513 | * @param array $fields list of fields you need. |
||
| 514 | * @param bool $hydrate whether to hydrate object or not |
||
| 515 | * |
||
| 516 | * @return array|null|object |
||
| 517 | */ |
||
| 518 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * get classname of entity |
||
| 535 | * |
||
| 536 | * @return string|null |
||
| 537 | */ |
||
| 538 | public function getEntityClass() |
||
| 546 | |||
| 547 | /** |
||
| 548 | * {@inheritDoc} |
||
| 549 | * |
||
| 550 | * Currently this is being used to build the route id used for redirecting |
||
| 551 | * to newly made documents. It might benefit from having a different name |
||
| 552 | * for those purposes. |
||
| 553 | * |
||
| 554 | * We might use a convention based mapping here: |
||
| 555 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 556 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 557 | * |
||
| 558 | * @todo implement this in a more convention based manner |
||
| 559 | * |
||
| 560 | * @return string |
||
| 561 | */ |
||
| 562 | public function getConnectionName() |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Does the actual query using the RQL Bundle. |
||
| 571 | * |
||
| 572 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 573 | * @param Query $query query from parser |
||
| 574 | * |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 586 | * |
||
| 587 | * @param Object $record record |
||
| 588 | * |
||
| 589 | * @return void |
||
| 590 | */ |
||
| 591 | protected function checkIfOriginRecord($record) |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 609 | * |
||
| 610 | * @return int |
||
| 611 | */ |
||
| 612 | private function getDefaultLimit() |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Will fire a ModelEvent |
||
| 623 | * |
||
| 624 | * @param string $action insert or update |
||
| 625 | * @param Object $collection the changed Document |
||
| 626 | * |
||
| 627 | * @return void |
||
| 628 | */ |
||
| 629 | private function dispatchModelEvent($action, $collection) |
||
| 647 | } |
||
| 648 |
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: