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 |
||
| 30 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $description; |
||
| 36 | /** |
||
| 37 | * @var string[] |
||
| 38 | */ |
||
| 39 | protected $fieldTitles; |
||
| 40 | /** |
||
| 41 | * @var string[] |
||
| 42 | */ |
||
| 43 | protected $fieldDescriptions; |
||
| 44 | /** |
||
| 45 | * @var string[] |
||
| 46 | */ |
||
| 47 | protected $requiredFields = array(); |
||
| 48 | /** |
||
| 49 | * @var string[] |
||
| 50 | */ |
||
| 51 | protected $searchableFields = array(); |
||
| 52 | /** |
||
| 53 | * @var DocumentRepository |
||
| 54 | */ |
||
| 55 | private $repository; |
||
| 56 | /** |
||
| 57 | * @var Visitor |
||
| 58 | */ |
||
| 59 | private $visitor; |
||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | protected $notModifiableOriginRecords; |
||
| 64 | /** |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | private $paginationDefaultLimit; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var boolean |
||
| 71 | */ |
||
| 72 | protected $filterByAuthUser; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | protected $filterByAuthField; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var RqlTranslator |
||
| 81 | */ |
||
| 82 | protected $translator; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var DocumentManager |
||
| 86 | */ |
||
| 87 | protected $manager; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param Visitor $visitor rql query visitor |
||
| 91 | * @param RqlTranslator $translator Translator for query modification |
||
| 92 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 93 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 94 | */ |
||
| 95 | public function __construct( |
||
| 107 | |||
| 108 | /** |
||
| 109 | * get repository instance |
||
| 110 | * |
||
| 111 | * @return DocumentRepository |
||
| 112 | */ |
||
| 113 | public function getRepository() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * create new app model |
||
| 120 | * |
||
| 121 | * @param DocumentRepository $repository Repository of countries |
||
| 122 | * |
||
| 123 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 124 | */ |
||
| 125 | public function setRepository(DocumentRepository $repository) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritDoc} |
||
| 135 | * |
||
| 136 | * @param Request $request The request object |
||
| 137 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 138 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $prefix the prefix for custom text search indexes |
||
| 232 | * @return bool |
||
| 233 | * @throws \Doctrine\ODM\MongoDB\MongoDBException |
||
| 234 | */ |
||
| 235 | private function hasCustomSearchIndex($prefix = 'search') |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @return string the version of the MongoDB as a string |
||
| 249 | */ |
||
| 250 | private function getMongoDBVersion() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param object $entity entity to insert |
||
| 264 | * @param bool $returnEntity true to return entity |
||
| 265 | * @param bool $doFlush if we should flush or not after insert |
||
| 266 | * |
||
| 267 | * @return Object|null |
||
| 268 | */ |
||
| 269 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @param string $documentId id of entity to find |
||
| 284 | * |
||
| 285 | * @return Object |
||
| 286 | */ |
||
| 287 | public function find($documentId) |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritDoc} |
||
| 294 | * |
||
| 295 | * @param string $documentId id of entity to update |
||
| 296 | * @param Object $entity new entity |
||
| 297 | * @param bool $returnEntity true to return entity |
||
| 298 | * |
||
| 299 | * @return Object|null |
||
| 300 | */ |
||
| 301 | public function updateRecord($documentId, $entity, $returnEntity = true) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * {@inheritDoc} |
||
| 326 | * |
||
| 327 | * @param string|object $id id of entity to delete or entity instance |
||
| 328 | * |
||
| 329 | * @return null|Object |
||
| 330 | */ |
||
| 331 | public function deleteRecord($id) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Triggers a flush on the DocumentManager |
||
| 352 | * |
||
| 353 | * @param null $document optional document |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function flush($document = null) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * A low level delete without any checks |
||
| 364 | * |
||
| 365 | * @param mixed $id record id |
||
| 366 | * |
||
| 367 | * @return void |
||
| 368 | */ |
||
| 369 | private function deleteById($id) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Checks in a performant way if a certain record id exists in the database |
||
| 381 | * |
||
| 382 | * @param mixed $id record id |
||
| 383 | * |
||
| 384 | * @return bool true if it exists, false otherwise |
||
| 385 | */ |
||
| 386 | public function recordExists($id) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 393 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 394 | * is a better way to get what you need. |
||
| 395 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 396 | * instance, make sure to pass false there. |
||
| 397 | * |
||
| 398 | * @param mixed $id record id |
||
| 399 | * @param array $fields list of fields you need. |
||
| 400 | * @param bool $hydrate whether to hydrate object or not |
||
| 401 | * |
||
| 402 | * @return array|null|object |
||
| 403 | */ |
||
| 404 | public function selectSingleFields($id, array $fields, $hydrate = true) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * get classname of entity |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | public function getEntityClass() |
||
| 428 | |||
| 429 | /** |
||
| 430 | * {@inheritDoc} |
||
| 431 | * |
||
| 432 | * Currently this is being used to build the route id used for redirecting |
||
| 433 | * to newly made documents. It might benefit from having a different name |
||
| 434 | * for those purposes. |
||
| 435 | * |
||
| 436 | * We might use a convention based mapping here: |
||
| 437 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 438 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 439 | * |
||
| 440 | * @todo implement this in a more convention based manner |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function getConnectionName() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Does the actual query using the RQL Bundle. |
||
| 453 | * |
||
| 454 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 455 | * @param Query $query query from parser |
||
| 456 | * |
||
| 457 | * @return array |
||
| 458 | */ |
||
| 459 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 468 | * |
||
| 469 | * @param Object $record record |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | protected function checkIfOriginRecord($record) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 491 | * |
||
| 492 | * @return int |
||
| 493 | */ |
||
| 494 | private function getDefaultLimit() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param Boolean $active active |
||
| 505 | * @param String $field field |
||
| 506 | * @return void |
||
| 507 | */ |
||
| 508 | public function setFilterByAuthUser($active, $field) |
||
| 513 | } |
||
| 514 |
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.