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  | 
            ||
| 29 | class DocumentModel extends SchemaModel implements ModelInterface  | 
            ||
| 30 | { | 
            ||
| 31 | /**  | 
            ||
| 32 | * @var string  | 
            ||
| 33 | */  | 
            ||
| 34 | protected $description;  | 
            ||
| 35 | /**  | 
            ||
| 36 | * @var string[]  | 
            ||
| 37 | */  | 
            ||
| 38 | protected $fieldTitles;  | 
            ||
| 39 | /**  | 
            ||
| 40 | * @var string[]  | 
            ||
| 41 | */  | 
            ||
| 42 | protected $fieldDescriptions;  | 
            ||
| 43 | /**  | 
            ||
| 44 | * @var string[]  | 
            ||
| 45 | */  | 
            ||
| 46 | protected $requiredFields = array();  | 
            ||
| 47 | /**  | 
            ||
| 48 | * @var string[]  | 
            ||
| 49 | */  | 
            ||
| 50 | protected $searchableFields = array();  | 
            ||
| 51 | /**  | 
            ||
| 52 | * @var DocumentRepository  | 
            ||
| 53 | */  | 
            ||
| 54 | private $repository;  | 
            ||
| 55 | /**  | 
            ||
| 56 | * @var Visitor  | 
            ||
| 57 | */  | 
            ||
| 58 | private $visitor;  | 
            ||
| 59 | /**  | 
            ||
| 60 | * @var array  | 
            ||
| 61 | */  | 
            ||
| 62 | protected $notModifiableOriginRecords;  | 
            ||
| 63 | /**  | 
            ||
| 64 | * @var integer  | 
            ||
| 65 | */  | 
            ||
| 66 | private $paginationDefaultLimit;  | 
            ||
| 67 | |||
| 68 | /**  | 
            ||
| 69 | * @var Logger  | 
            ||
| 70 | */  | 
            ||
| 71 | private $logger;  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * @var boolean  | 
            ||
| 75 | */  | 
            ||
| 76 | protected $filterByAuthUser;  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * @var string  | 
            ||
| 80 | */  | 
            ||
| 81 | protected $filterByAuthField;  | 
            ||
| 82 | |||
| 83 | /**  | 
            ||
| 84 | * @var RqlTranslator  | 
            ||
| 85 | */  | 
            ||
| 86 | protected $translator;  | 
            ||
| 87 | |||
| 88 | /**  | 
            ||
| 89 | * @var DocumentManager  | 
            ||
| 90 | */  | 
            ||
| 91 | protected $manager;  | 
            ||
| 92 | |||
| 93 | /**  | 
            ||
| 94 | * @param Visitor $visitor rql query visitor  | 
            ||
| 95 | * @param RqlTranslator $translator Translator for query modification  | 
            ||
| 96 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values  | 
            ||
| 97 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context  | 
            ||
| 98 | * @param Logger $logger The defined system logger  | 
            ||
| 99 | */  | 
            ||
| 100 | 4 | public function __construct(  | 
            |
| 114 | |||
| 115 | /**  | 
            ||
| 116 | * get repository instance  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return DocumentRepository  | 
            ||
| 119 | */  | 
            ||
| 120 | 2 | public function getRepository()  | 
            |
| 124 | |||
| 125 | /**  | 
            ||
| 126 | * create new app model  | 
            ||
| 127 | *  | 
            ||
| 128 | * @param DocumentRepository $repository Repository of countries  | 
            ||
| 129 | *  | 
            ||
| 130 | * @return \Graviton\RestBundle\Model\DocumentModel  | 
            ||
| 131 | */  | 
            ||
| 132 | 4 | public function setRepository(DocumentRepository $repository)  | 
            |
| 139 | |||
| 140 | /**  | 
            ||
| 141 |      * {@inheritDoc} | 
            ||
| 142 | *  | 
            ||
| 143 | * @param Request $request The request object  | 
            ||
| 144 | * @param SecurityUser $user SecurityUser Object  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 145 | * @param SchemaDocument $schema Schema model used for search fields extraction  | 
            ||
| 146 | *  | 
            ||
| 147 | * @return array  | 
            ||
| 148 | */  | 
            ||
| 149 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null)  | 
            ||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | /**  | 
            ||
| 250 | * @return string the version of the MongoDB as a string  | 
            ||
| 251 | */  | 
            ||
| 252 | public function getMongoDBVersion()  | 
            ||
| 263 | |||
| 264 | /**  | 
            ||
| 265 | * @param object $entity entity to insert  | 
            ||
| 266 | * @param bool $returnEntity true to return entity  | 
            ||
| 267 | * @param bool $doFlush if we should flush or not after insert  | 
            ||
| 268 | *  | 
            ||
| 269 | * @return Object|null  | 
            ||
| 270 | */  | 
            ||
| 271 | public function insertRecord($entity, $returnEntity = true, $doFlush = true)  | 
            ||
| 283 | |||
| 284 | /**  | 
            ||
| 285 | * @param string $documentId id of entity to find  | 
            ||
| 286 | *  | 
            ||
| 287 | * @return Object  | 
            ||
| 288 | */  | 
            ||
| 289 | 4 | public function find($documentId)  | 
            |
| 293 | |||
| 294 | /**  | 
            ||
| 295 |      * {@inheritDoc} | 
            ||
| 296 | *  | 
            ||
| 297 | * @param string $documentId id of entity to update  | 
            ||
| 298 | * @param Object $entity new entity  | 
            ||
| 299 | * @param bool $returnEntity true to return entity  | 
            ||
| 300 | *  | 
            ||
| 301 | * @return Object|null  | 
            ||
| 302 | */  | 
            ||
| 303 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true)  | 
            |
| 325 | |||
| 326 | /**  | 
            ||
| 327 |      * {@inheritDoc} | 
            ||
| 328 | *  | 
            ||
| 329 | * @param string|object $id id of entity to delete or entity instance  | 
            ||
| 330 | *  | 
            ||
| 331 | * @return null|Object  | 
            ||
| 332 | */  | 
            ||
| 333 | public function deleteRecord($id)  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * Triggers a flush on the DocumentManager  | 
            ||
| 354 | *  | 
            ||
| 355 | * @param null $document optional document  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return void  | 
            ||
| 358 | */  | 
            ||
| 359 | public function flush($document = null)  | 
            ||
| 363 | |||
| 364 | /**  | 
            ||
| 365 | * A low level delete without any checks  | 
            ||
| 366 | *  | 
            ||
| 367 | * @param mixed $id record id  | 
            ||
| 368 | *  | 
            ||
| 369 | * @return void  | 
            ||
| 370 | */  | 
            ||
| 371 | 2 | private function deleteById($id)  | 
            |
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * Checks in a performant way if a certain record id exists in the database  | 
            ||
| 383 | *  | 
            ||
| 384 | * @param mixed $id record id  | 
            ||
| 385 | *  | 
            ||
| 386 | * @return bool true if it exists, false otherwise  | 
            ||
| 387 | */  | 
            ||
| 388 | 4 | public function recordExists($id)  | 
            |
| 392 | |||
| 393 | /**  | 
            ||
| 394 | * Returns a set of fields from an existing resource in a performant manner.  | 
            ||
| 395 | * If you need to check certain fields on an object (and don't need everything), this  | 
            ||
| 396 | * is a better way to get what you need.  | 
            ||
| 397 | * If the record is not present, you will receive null. If you don't need an hydrated  | 
            ||
| 398 | * instance, make sure to pass false there.  | 
            ||
| 399 | *  | 
            ||
| 400 | * @param mixed $id record id  | 
            ||
| 401 | * @param array $fields list of fields you need.  | 
            ||
| 402 | * @param bool $hydrate whether to hydrate object or not  | 
            ||
| 403 | *  | 
            ||
| 404 | * @return array|null|object  | 
            ||
| 405 | */  | 
            ||
| 406 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true)  | 
            |
| 420 | |||
| 421 | /**  | 
            ||
| 422 | * get classname of entity  | 
            ||
| 423 | *  | 
            ||
| 424 | * @return string  | 
            ||
| 425 | */  | 
            ||
| 426 | 4 | public function getEntityClass()  | 
            |
| 430 | |||
| 431 | /**  | 
            ||
| 432 |      * {@inheritDoc} | 
            ||
| 433 | *  | 
            ||
| 434 | * Currently this is being used to build the route id used for redirecting  | 
            ||
| 435 | * to newly made documents. It might benefit from having a different name  | 
            ||
| 436 | * for those purposes.  | 
            ||
| 437 | *  | 
            ||
| 438 | * We might use a convention based mapping here:  | 
            ||
| 439 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core  | 
            ||
| 440 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core  | 
            ||
| 441 | *  | 
            ||
| 442 | * @todo implement this in a more convention based manner  | 
            ||
| 443 | *  | 
            ||
| 444 | * @return string  | 
            ||
| 445 | */  | 
            ||
| 446 | public function getConnectionName()  | 
            ||
| 452 | |||
| 453 | /**  | 
            ||
| 454 | * Does the actual query using the RQL Bundle.  | 
            ||
| 455 | *  | 
            ||
| 456 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder  | 
            ||
| 457 | * @param Query $query query from parser  | 
            ||
| 458 | *  | 
            ||
| 459 | * @return array  | 
            ||
| 460 | */  | 
            ||
| 461 | protected function doRqlQuery($queryBuilder, Query $query)  | 
            ||
| 467 | |||
| 468 | /**  | 
            ||
| 469 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed  | 
            ||
| 470 | *  | 
            ||
| 471 | * @param Object $record record  | 
            ||
| 472 | *  | 
            ||
| 473 | * @return void  | 
            ||
| 474 | */  | 
            ||
| 475 | 14 | protected function checkIfOriginRecord($record)  | 
            |
| 490 | |||
| 491 | /**  | 
            ||
| 492 | * Determines the configured amount fo data records to be returned in pagination context.  | 
            ||
| 493 | *  | 
            ||
| 494 | * @return int  | 
            ||
| 495 | */  | 
            ||
| 496 | private function getDefaultLimit()  | 
            ||
| 504 | |||
| 505 | /**  | 
            ||
| 506 | * @param Boolean $active active  | 
            ||
| 507 | * @param String $field field  | 
            ||
| 508 | * @return void  | 
            ||
| 509 | */  | 
            ||
| 510 | 4 | public function setFilterByAuthUser($active, $field)  | 
            |
| 515 | }  | 
            ||
| 516 | 
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.