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 |
||
| 33 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $description; |
||
| 39 | /** |
||
| 40 | * @var string[] |
||
| 41 | */ |
||
| 42 | protected $fieldTitles; |
||
| 43 | /** |
||
| 44 | * @var string[] |
||
| 45 | */ |
||
| 46 | protected $fieldDescriptions; |
||
| 47 | /** |
||
| 48 | * @var string[] |
||
| 49 | */ |
||
| 50 | protected $requiredFields = array(); |
||
| 51 | /** |
||
| 52 | * @var string[] |
||
| 53 | */ |
||
| 54 | protected $searchableFields = array(); |
||
| 55 | /** |
||
| 56 | * @var string[] |
||
| 57 | */ |
||
| 58 | protected $textIndexes = array(); |
||
| 59 | /** |
||
| 60 | * @var DocumentRepository |
||
| 61 | */ |
||
| 62 | private $repository; |
||
| 63 | /** |
||
| 64 | * @var Visitor |
||
| 65 | */ |
||
| 66 | private $visitor; |
||
| 67 | /** |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $notModifiableOriginRecords; |
||
| 71 | /** |
||
| 72 | * @var integer |
||
| 73 | */ |
||
| 74 | private $paginationDefaultLimit; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var boolean |
||
| 78 | */ |
||
| 79 | protected $filterByAuthUser; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $filterByAuthField; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var DocumentManager |
||
| 88 | */ |
||
| 89 | protected $manager; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Visitor $visitor rql query visitor |
||
| 93 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 94 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 106 | |||
| 107 | /** |
||
| 108 | * get repository instance |
||
| 109 | * |
||
| 110 | * @return DocumentRepository |
||
| 111 | */ |
||
| 112 | public function getRepository() |
||
| 116 | |||
| 117 | /** |
||
| 118 | * create new app model |
||
| 119 | * |
||
| 120 | * @param DocumentRepository $repository Repository of countries |
||
| 121 | * |
||
| 122 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 123 | */ |
||
| 124 | public function setRepository(DocumentRepository $repository) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritDoc} |
||
| 134 | * |
||
| 135 | * @param Request $request The request object |
||
| 136 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 137 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param XiagQuery $xiagQuery Xiag Builder |
||
| 226 | * @param MongoBuilder $queryBuilder Mongo Doctrine query builder |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | private function buildSearchQuery(XiagQuery $xiagQuery, MongoBuilder $queryBuilder) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Check if collection has search indexes in DB |
||
| 283 | * |
||
| 284 | * @param string $prefix the prefix for custom text search indexes |
||
| 285 | * @return bool |
||
| 286 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 287 | */ |
||
| 288 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Build Search text index |
||
| 302 | * |
||
| 303 | * @param MongoBuilder $queryBuilder Doctrine mongo query builder object |
||
| 304 | * @param SearchNode $searchNode Graviton Search node |
||
| 305 | * @return MongoBuilder |
||
| 306 | */ |
||
| 307 | private function buildSearchTextQuery(MongoBuilder $queryBuilder, SearchNode $searchNode) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @return string the version of the MongoDB as a string |
||
| 323 | */ |
||
| 324 | private function getMongoDBVersion() |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @param object $entity entity to insert |
||
| 338 | * @param bool $returnEntity true to return entity |
||
| 339 | * @param bool $doFlush if we should flush or not after insert |
||
| 340 | * |
||
| 341 | * @return Object|null |
||
| 342 | */ |
||
| 343 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param string $documentId id of entity to find |
||
| 357 | * |
||
| 358 | * @return Object |
||
| 359 | */ |
||
| 360 | public function find($documentId) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * {@inheritDoc} |
||
| 367 | * |
||
| 368 | * @param string $documentId id of entity to update |
||
| 369 | * @param Object $entity new entity |
||
| 370 | * @param bool $returnEntity true to return entity |
||
| 371 | * |
||
| 372 | * @return Object|null |
||
| 373 | */ |
||
| 374 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * {@inheritDoc} |
||
| 395 | * |
||
| 396 | * @param string|object $id id of entity to delete or entity instance |
||
| 397 | * |
||
| 398 | * @return null|Object |
||
| 399 | */ |
||
| 400 | public function deleteRecord($id) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Triggers a flush on the DocumentManager |
||
| 421 | * |
||
| 422 | * @param null $document optional document |
||
| 423 | * |
||
| 424 | * @return void |
||
| 425 | */ |
||
| 426 | public function flush($document = null) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * A low level delete without any checks |
||
| 433 | * |
||
| 434 | * @param mixed $id record id |
||
| 435 | * |
||
| 436 | * @return void |
||
| 437 | */ |
||
| 438 | private function deleteById($id) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Checks in a performant way if a certain record id exists in the database |
||
| 450 | * |
||
| 451 | * @param mixed $id record id |
||
| 452 | * |
||
| 453 | * @return bool true if it exists, false otherwise |
||
| 454 | */ |
||
| 455 | public function recordExists($id) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 462 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 463 | * is a better way to get what you need. |
||
| 464 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 465 | * instance, make sure to pass false there. |
||
| 466 | * |
||
| 467 | * @param mixed $id record id |
||
| 468 | * @param array $fields list of fields you need. |
||
| 469 | * @param bool $hydrate whether to hydrate object or not |
||
| 470 | * |
||
| 471 | * @return array|null|object |
||
| 472 | */ |
||
| 473 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * get classname of entity |
||
| 490 | * |
||
| 491 | * @return string|null |
||
| 492 | */ |
||
| 493 | public function getEntityClass() |
||
| 501 | |||
| 502 | /** |
||
| 503 | * {@inheritDoc} |
||
| 504 | * |
||
| 505 | * Currently this is being used to build the route id used for redirecting |
||
| 506 | * to newly made documents. It might benefit from having a different name |
||
| 507 | * for those purposes. |
||
| 508 | * |
||
| 509 | * We might use a convention based mapping here: |
||
| 510 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 511 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 512 | * |
||
| 513 | * @todo implement this in a more convention based manner |
||
| 514 | * |
||
| 515 | * @return string |
||
| 516 | */ |
||
| 517 | public function getConnectionName() |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Does the actual query using the RQL Bundle. |
||
| 526 | * |
||
| 527 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 528 | * @param Query $query query from parser |
||
| 529 | * |
||
| 530 | * @return array |
||
| 531 | */ |
||
| 532 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 541 | * |
||
| 542 | * @param Object $record record |
||
| 543 | * |
||
| 544 | * @return void |
||
| 545 | */ |
||
| 546 | protected function checkIfOriginRecord($record) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 564 | * |
||
| 565 | * @return int |
||
| 566 | */ |
||
| 567 | private function getDefaultLimit() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * @param Boolean $active active |
||
| 578 | * @param String $field field |
||
| 579 | * @return void |
||
| 580 | */ |
||
| 581 | public function setFilterByAuthUser($active, $field) |
||
| 586 | } |
||
| 587 |
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.