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 boolean |
||
| 70 | */ |
||
| 71 | protected $filterByAuthUser; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | protected $filterByAuthField; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var RqlTranslator |
||
| 80 | */ |
||
| 81 | protected $translator; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var DocumentManager |
||
| 85 | */ |
||
| 86 | protected $manager; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param Visitor $visitor rql query visitor |
||
| 90 | * @param RqlTranslator $translator Translator for query modification |
||
| 91 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 92 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 93 | * @param Logger $logger The defined system logger |
||
| 94 | */ |
||
| 95 | public function __construct( |
||
| 108 | 4 | ||
| 109 | 4 | /** |
|
| 110 | 4 | * get repository instance |
|
| 111 | 4 | * |
|
| 112 | 4 | * @return DocumentRepository |
|
| 113 | 4 | */ |
|
| 114 | public function getRepository() |
||
| 118 | |||
| 119 | /** |
||
| 120 | 2 | * create new app model |
|
| 121 | * |
||
| 122 | 2 | * @param DocumentRepository $repository Repository of countries |
|
| 123 | * |
||
| 124 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 125 | */ |
||
| 126 | public function setRepository(DocumentRepository $repository) |
||
| 133 | |||
| 134 | 4 | /** |
|
| 135 | 4 | * {@inheritDoc} |
|
| 136 | * |
||
| 137 | 4 | * @param Request $request The request object |
|
| 138 | * @param SecurityUser $user SecurityUser Object |
||
| 139 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $prefix the prefix for custom text search indexes |
||
| 239 | * @return bool |
||
| 240 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 241 | */ |
||
| 242 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @return string the version of the MongoDB as a string |
||
| 256 | */ |
||
| 257 | private function getMongoDBVersion() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @param object $entity entity to insert |
||
| 271 | * @param bool $returnEntity true to return entity |
||
| 272 | * @param bool $doFlush if we should flush or not after insert |
||
| 273 | * |
||
| 274 | * @return Object|null |
||
| 275 | */ |
||
| 276 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 288 | |||
| 289 | 4 | /** |
|
| 290 | * @param string $documentId id of entity to find |
||
| 291 | 4 | * |
|
| 292 | * @return Object |
||
| 293 | */ |
||
| 294 | public function find($documentId) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritDoc} |
||
| 301 | * |
||
| 302 | * @param string $documentId id of entity to update |
||
| 303 | 2 | * @param Object $entity new entity |
|
| 304 | * @param bool $returnEntity true to return entity |
||
| 305 | * |
||
| 306 | 2 | * @return Object|null |
|
| 307 | 2 | */ |
|
| 308 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritDoc} |
||
| 333 | * |
||
| 334 | * @param string|object $id id of entity to delete or entity instance |
||
| 335 | * |
||
| 336 | * @return null|Object |
||
| 337 | */ |
||
| 338 | public function deleteRecord($id) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Triggers a flush on the DocumentManager |
||
| 359 | * |
||
| 360 | * @param null $document optional document |
||
| 361 | * |
||
| 362 | * @return void |
||
| 363 | */ |
||
| 364 | public function flush($document = null) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * A low level delete without any checks |
||
| 371 | 2 | * |
|
| 372 | * @param mixed $id record id |
||
| 373 | 2 | * |
|
| 374 | * @return void |
||
| 375 | 2 | */ |
|
| 376 | 2 | private function deleteById($id) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Checks in a performant way if a certain record id exists in the database |
||
| 388 | 4 | * |
|
| 389 | * @param mixed $id record id |
||
| 390 | 4 | * |
|
| 391 | * @return bool true if it exists, false otherwise |
||
| 392 | */ |
||
| 393 | public function recordExists($id) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 400 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 401 | * is a better way to get what you need. |
||
| 402 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 403 | * instance, make sure to pass false there. |
||
| 404 | * |
||
| 405 | * @param mixed $id record id |
||
| 406 | 4 | * @param array $fields list of fields you need. |
|
| 407 | * @param bool $hydrate whether to hydrate object or not |
||
| 408 | 4 | * |
|
| 409 | 4 | * @return array|null|object |
|
| 410 | */ |
||
| 411 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 425 | |||
| 426 | 4 | /** |
|
| 427 | * get classname of entity |
||
| 428 | 4 | * |
|
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getEntityClass() |
||
| 435 | |||
| 436 | /** |
||
| 437 | * {@inheritDoc} |
||
| 438 | * |
||
| 439 | * Currently this is being used to build the route id used for redirecting |
||
| 440 | * to newly made documents. It might benefit from having a different name |
||
| 441 | * for those purposes. |
||
| 442 | * |
||
| 443 | * We might use a convention based mapping here: |
||
| 444 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 445 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 446 | * |
||
| 447 | * @todo implement this in a more convention based manner |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function getConnectionName() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Does the actual query using the RQL Bundle. |
||
| 460 | * |
||
| 461 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 462 | * @param Query $query query from parser |
||
| 463 | * |
||
| 464 | * @return array |
||
| 465 | */ |
||
| 466 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 475 | 14 | * |
|
| 476 | * @param Object $record record |
||
| 477 | 7 | * |
|
| 478 | 14 | * @return void |
|
| 479 | 7 | */ |
|
| 480 | 6 | protected function checkIfOriginRecord($record) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 498 | * |
||
| 499 | * @return int |
||
| 500 | */ |
||
| 501 | private function getDefaultLimit() |
||
| 509 | |||
| 510 | 4 | /** |
|
| 511 | * @param Boolean $active active |
||
| 512 | 4 | * @param String $field field |
|
| 513 | 4 | * @return void |
|
| 514 | 4 | */ |
|
| 515 | public function setFilterByAuthUser($active, $field) |
||
| 520 | } |
||
| 521 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.