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 DocumentRepository |
||
| 57 | */ |
||
| 58 | private $repository; |
||
| 59 | /** |
||
| 60 | * @var Visitor |
||
| 61 | */ |
||
| 62 | private $visitor; |
||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $notModifiableOriginRecords; |
||
| 67 | /** |
||
| 68 | * @var integer |
||
| 69 | */ |
||
| 70 | private $paginationDefaultLimit; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var boolean |
||
| 74 | */ |
||
| 75 | protected $filterByAuthUser; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | protected $filterByAuthField; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var RqlTranslator |
||
| 84 | */ |
||
| 85 | protected $translator; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var DocumentManager |
||
| 89 | */ |
||
| 90 | protected $manager; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param Visitor $visitor rql query visitor |
||
| 94 | * @param RqlTranslator $translator Translator for query modification |
||
| 95 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 96 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 97 | */ |
||
| 98 | 4 | public function __construct( |
|
| 110 | |||
| 111 | /** |
||
| 112 | * get repository instance |
||
| 113 | * |
||
| 114 | * @return DocumentRepository |
||
| 115 | */ |
||
| 116 | 2 | public function getRepository() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * create new app model |
||
| 123 | * |
||
| 124 | * @param DocumentRepository $repository Repository of countries |
||
| 125 | * |
||
| 126 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 127 | */ |
||
| 128 | 4 | public function setRepository(DocumentRepository $repository) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritDoc} |
||
| 138 | * |
||
| 139 | * @param Request $request The request object |
||
| 140 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 141 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 142 | * |
||
| 143 | * @return array |
||
| 144 | */ |
||
| 145 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string $prefix the prefix for custom text search indexes |
||
| 246 | * @return bool |
||
| 247 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 248 | */ |
||
| 249 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return string the version of the MongoDB as a string |
||
| 263 | */ |
||
| 264 | private function getMongoDBVersion() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param object $entity entity to insert |
||
| 278 | * @param bool $returnEntity true to return entity |
||
| 279 | * @param bool $doFlush if we should flush or not after insert |
||
| 280 | * |
||
| 281 | * @return Object|null |
||
| 282 | */ |
||
| 283 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param string $documentId id of entity to find |
||
| 298 | * |
||
| 299 | * @return Object |
||
| 300 | */ |
||
| 301 | 4 | public function find($documentId) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritDoc} |
||
| 308 | * |
||
| 309 | * @param string $documentId id of entity to update |
||
| 310 | * @param Object $entity new entity |
||
| 311 | * @param bool $returnEntity true to return entity |
||
| 312 | * |
||
| 313 | * @return Object|null |
||
| 314 | */ |
||
| 315 | 3 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritDoc} |
||
| 340 | * |
||
| 341 | * @param string|object $id id of entity to delete or entity instance |
||
| 342 | * |
||
| 343 | * @return null|Object |
||
| 344 | */ |
||
| 345 | public function deleteRecord($id) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Triggers a flush on the DocumentManager |
||
| 366 | * |
||
| 367 | * @param null $document optional document |
||
| 368 | * |
||
| 369 | * @return void |
||
| 370 | */ |
||
| 371 | public function flush($document = null) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * A low level delete without any checks |
||
| 378 | * |
||
| 379 | * @param mixed $id record id |
||
| 380 | * |
||
| 381 | * @return void |
||
| 382 | */ |
||
| 383 | 2 | private function deleteById($id) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Checks in a performant way if a certain record id exists in the database |
||
| 395 | * |
||
| 396 | * @param mixed $id record id |
||
| 397 | * |
||
| 398 | * @return bool true if it exists, false otherwise |
||
| 399 | */ |
||
| 400 | 4 | public function recordExists($id) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 407 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 408 | * is a better way to get what you need. |
||
| 409 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 410 | * instance, make sure to pass false there. |
||
| 411 | * |
||
| 412 | * @param mixed $id record id |
||
| 413 | * @param array $fields list of fields you need. |
||
| 414 | * @param bool $hydrate whether to hydrate object or not |
||
| 415 | * |
||
| 416 | * @return array|null|object |
||
| 417 | */ |
||
| 418 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * get classname of entity |
||
| 435 | * |
||
| 436 | * @return string|null |
||
| 437 | */ |
||
| 438 | 4 | public function getEntityClass() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * {@inheritDoc} |
||
| 449 | * |
||
| 450 | * Currently this is being used to build the route id used for redirecting |
||
| 451 | * to newly made documents. It might benefit from having a different name |
||
| 452 | * for those purposes. |
||
| 453 | * |
||
| 454 | * We might use a convention based mapping here: |
||
| 455 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 456 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 457 | * |
||
| 458 | * @todo implement this in a more convention based manner |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getConnectionName() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Does the actual query using the RQL Bundle. |
||
| 471 | * |
||
| 472 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 473 | * @param Query $query query from parser |
||
| 474 | * |
||
| 475 | * @return array |
||
| 476 | */ |
||
| 477 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 486 | * |
||
| 487 | * @param Object $record record |
||
| 488 | * |
||
| 489 | * @return void |
||
| 490 | */ |
||
| 491 | 14 | protected function checkIfOriginRecord($record) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 509 | * |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | private function getDefaultLimit() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @param Boolean $active active |
||
| 523 | * @param String $field field |
||
| 524 | * @return void |
||
| 525 | */ |
||
| 526 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 531 | } |
||
| 532 |
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.