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 |
||
| 32 | class DocumentModel extends SchemaModel implements ModelInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $description; |
||
| 38 | /** |
||
| 39 | * @var string[] |
||
| 40 | */ |
||
| 41 | protected $fieldTitles; |
||
| 42 | /** |
||
| 43 | * @var string[] |
||
| 44 | */ |
||
| 45 | protected $fieldDescriptions; |
||
| 46 | /** |
||
| 47 | * @var string[] |
||
| 48 | */ |
||
| 49 | protected $requiredFields = array(); |
||
| 50 | /** |
||
| 51 | * @var string[] |
||
| 52 | */ |
||
| 53 | protected $searchableFields = array(); |
||
| 54 | /** |
||
| 55 | * @var DocumentRepository |
||
| 56 | */ |
||
| 57 | private $repository; |
||
| 58 | /** |
||
| 59 | * @var Visitor |
||
| 60 | */ |
||
| 61 | private $visitor; |
||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $notModifiableOriginRecords; |
||
| 66 | /** |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | private $paginationDefaultLimit; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var boolean |
||
| 73 | */ |
||
| 74 | protected $filterByAuthUser; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $filterByAuthField; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var RqlTranslator |
||
| 83 | */ |
||
| 84 | protected $translator; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var DocumentManager |
||
| 88 | */ |
||
| 89 | protected $manager; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Visitor $visitor rql query visitor |
||
| 93 | * @param RqlTranslator $translator Translator for query modification |
||
| 94 | * @param array $notModifiableOriginRecords strings with not modifiable recordOrigin values |
||
| 95 | * @param integer $paginationDefaultLimit amount of data records to be returned when in pagination context |
||
| 96 | */ |
||
| 97 | 4 | public function __construct( |
|
| 98 | Visitor $visitor, |
||
| 99 | RqlTranslator $translator, |
||
| 100 | $notModifiableOriginRecords, |
||
| 101 | $paginationDefaultLimit |
||
| 102 | ) { |
||
| 103 | 4 | parent::__construct(); |
|
| 104 | 4 | $this->visitor = $visitor; |
|
| 105 | 4 | $this->translator = $translator; |
|
| 106 | 4 | $this->notModifiableOriginRecords = $notModifiableOriginRecords; |
|
| 107 | 4 | $this->paginationDefaultLimit = (int) $paginationDefaultLimit; |
|
| 108 | 4 | } |
|
| 109 | |||
| 110 | /** |
||
| 111 | * get repository instance |
||
| 112 | * |
||
| 113 | * @return DocumentRepository |
||
| 114 | */ |
||
| 115 | 2 | public function getRepository() |
|
| 116 | { |
||
| 117 | 2 | return $this->repository; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * create new app model |
||
| 122 | * |
||
| 123 | * @param DocumentRepository $repository Repository of countries |
||
| 124 | * |
||
| 125 | * @return \Graviton\RestBundle\Model\DocumentModel |
||
| 126 | */ |
||
| 127 | 4 | public function setRepository(DocumentRepository $repository) |
|
| 128 | { |
||
| 129 | 4 | $this->repository = $repository; |
|
| 130 | 4 | $this->manager = $repository->getDocumentManager(); |
|
| 131 | |||
| 132 | 4 | return $this; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | * |
||
| 138 | * @param Request $request The request object |
||
| 139 | * @param SecurityUser $user SecurityUser Object |
||
|
|
|||
| 140 | * @param SchemaDocument $schema Schema model used for search fields extraction |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function findAll(Request $request, SecurityUser $user = null, SchemaDocument $schema = null) |
||
| 145 | { |
||
| 146 | $pageNumber = $request->query->get('page', 1); |
||
| 147 | $numberPerPage = (int) $request->query->get('perPage', $this->getDefaultLimit()); |
||
| 148 | $startAt = ($pageNumber - 1) * $numberPerPage; |
||
| 149 | |||
| 150 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $queryBuilder */ |
||
| 151 | $queryBuilder = $this->repository |
||
| 152 | ->createQueryBuilder(); |
||
| 153 | |||
| 154 | if ($this->filterByAuthUser && $user && $user->hasRole(SecurityUser::ROLE_USER)) { |
||
| 155 | $queryBuilder->field($this->filterByAuthField)->equals($user->getUser()->getId()); |
||
| 156 | } |
||
| 157 | |||
| 158 | |||
| 159 | $searchableFields = $this->getSearchableFields(); |
||
| 160 | if (!is_null($schema)) { |
||
| 161 | $searchableFields = $schema->getSearchable(); |
||
| 162 | } |
||
| 163 | |||
| 164 | // *** do we have an RQL expression, do we need to filter data? |
||
| 165 | if ($request->attributes->get('hasRql', false)) { |
||
| 166 | $queryBuilder = $this->doRqlQuery( |
||
| 167 | $queryBuilder, |
||
| 168 | $this->translator->translateSearchQuery( |
||
| 169 | $request->attributes->get('rqlQuery'), |
||
| 170 | $searchableFields |
||
| 171 | ) |
||
| 172 | ); |
||
| 173 | } else { |
||
| 174 | // @todo [lapistano]: seems the offset is missing for this query. |
||
| 175 | /** @var \Doctrine\ODM\MongoDB\Query\Builder $qb */ |
||
| 176 | $queryBuilder->find($this->repository->getDocumentName()); |
||
| 177 | } |
||
| 178 | |||
| 179 | // define offset and limit |
||
| 180 | if (!array_key_exists('skip', $queryBuilder->getQuery()->getQuery())) { |
||
| 181 | $queryBuilder->skip($startAt); |
||
| 182 | } else { |
||
| 183 | $startAt = (int) $queryBuilder->getQuery()->getQuery()['skip']; |
||
| 184 | } |
||
| 185 | |||
| 186 | if (!array_key_exists('limit', $queryBuilder->getQuery()->getQuery())) { |
||
| 187 | $queryBuilder->limit($numberPerPage); |
||
| 188 | } else { |
||
| 189 | $numberPerPage = (int) $queryBuilder->getQuery()->getQuery()['limit']; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Limit can not be negative nor null. |
||
| 193 | if ($numberPerPage < 1) { |
||
| 194 | throw new RqlSyntaxErrorException('negative or null limit in rql'); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * add a default sort on id if none was specified earlier |
||
| 199 | * |
||
| 200 | * not specifying something to sort on leads to very weird cases when fetching references |
||
| 201 | */ |
||
| 202 | if (!array_key_exists('sort', $queryBuilder->getQuery()->getQuery())) { |
||
| 203 | $queryBuilder->sort('_id'); |
||
| 204 | } |
||
| 205 | |||
| 206 | // run query |
||
| 207 | $query = $queryBuilder->getQuery(); |
||
| 208 | $records = array_values($query->execute()->toArray()); |
||
| 209 | |||
| 210 | $totalCount = $query->count(); |
||
| 211 | $numPages = (int) ceil($totalCount / $numberPerPage); |
||
| 212 | $page = (int) ceil($startAt / $numberPerPage) + 1; |
||
| 213 | if ($numPages > 1) { |
||
| 214 | $request->attributes->set('paging', true); |
||
| 215 | $request->attributes->set('page', $page); |
||
| 216 | $request->attributes->set('numPages', $numPages); |
||
| 217 | $request->attributes->set('startAt', $startAt); |
||
| 218 | $request->attributes->set('perPage', $numberPerPage); |
||
| 219 | $request->attributes->set('totalCount', $totalCount); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $records; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param object $entity entity to insert |
||
| 227 | * @param bool $returnEntity true to return entity |
||
| 228 | * @param bool $doFlush if we should flush or not after insert |
||
| 229 | * |
||
| 230 | * @return Object|null |
||
| 231 | */ |
||
| 232 | public function insertRecord($entity, $returnEntity = true, $doFlush = true) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param string $documentId id of entity to find |
||
| 247 | * |
||
| 248 | * @return Object |
||
| 249 | */ |
||
| 250 | 4 | public function find($documentId) |
|
| 251 | { |
||
| 252 | 4 | return $this->repository->find($documentId); |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritDoc} |
||
| 257 | * |
||
| 258 | * @param string $documentId id of entity to update |
||
| 259 | * @param Object $entity new entity |
||
| 260 | * @param bool $returnEntity true to return entity |
||
| 261 | * |
||
| 262 | * @return Object|null |
||
| 263 | */ |
||
| 264 | 2 | public function updateRecord($documentId, $entity, $returnEntity = true) |
|
| 265 | { |
||
| 266 | // In both cases the document attribute named originRecord must not be 'core' |
||
| 267 | 2 | $this->checkIfOriginRecord($entity); |
|
| 268 | 2 | $this->checkIfOriginRecord($this->selectSingleFields($documentId, ['recordOrigin'])); |
|
| 269 | |||
| 270 | 2 | if (!is_null($documentId)) { |
|
| 271 | 2 | $this->deleteById($documentId); |
|
| 272 | // detach so odm knows it's gone |
||
| 273 | 2 | $this->manager->detach($entity); |
|
| 274 | 2 | $this->manager->clear(); |
|
| 275 | 1 | } |
|
| 276 | |||
| 277 | 2 | $entity = $this->manager->merge($entity); |
|
| 278 | |||
| 279 | 2 | $this->manager->persist($entity); |
|
| 280 | 2 | $this->manager->flush($entity); |
|
| 281 | |||
| 282 | 2 | if ($returnEntity) { |
|
| 283 | return $entity; |
||
| 284 | } |
||
| 285 | 2 | } |
|
| 286 | |||
| 287 | /** |
||
| 288 | * {@inheritDoc} |
||
| 289 | * |
||
| 290 | * @param string|object $id id of entity to delete or entity instance |
||
| 291 | * |
||
| 292 | * @return null|Object |
||
| 293 | */ |
||
| 294 | public function deleteRecord($id) |
||
| 295 | { |
||
| 296 | if (is_object($id)) { |
||
| 297 | $entity = $id; |
||
| 298 | } else { |
||
| 299 | $entity = $this->find($id); |
||
| 300 | } |
||
| 301 | |||
| 302 | $return = $entity; |
||
| 303 | if ($entity) { |
||
| 304 | $this->checkIfOriginRecord($entity); |
||
| 305 | $this->manager->remove($entity); |
||
| 306 | $this->manager->flush(); |
||
| 307 | $return = null; |
||
| 308 | } |
||
| 309 | |||
| 310 | return $return; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Triggers a flush on the DocumentManager |
||
| 315 | * |
||
| 316 | * @param null $document optional document |
||
| 317 | * |
||
| 318 | * @return void |
||
| 319 | */ |
||
| 320 | public function flush($document = null) |
||
| 321 | { |
||
| 322 | $this->manager->flush($document); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * A low level delete without any checks |
||
| 327 | * |
||
| 328 | * @param mixed $id record id |
||
| 329 | * |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | 2 | private function deleteById($id) |
|
| 333 | { |
||
| 334 | 2 | $builder = $this->repository->createQueryBuilder(); |
|
| 335 | $builder |
||
| 336 | 2 | ->remove() |
|
| 337 | 2 | ->field('id')->equals($id) |
|
| 338 | 2 | ->getQuery() |
|
| 339 | 2 | ->execute(); |
|
| 340 | 2 | } |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Checks in a performant way if a certain record id exists in the database |
||
| 344 | * |
||
| 345 | * @param mixed $id record id |
||
| 346 | * |
||
| 347 | * @return bool true if it exists, false otherwise |
||
| 348 | */ |
||
| 349 | 4 | public function recordExists($id) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Returns a set of fields from an existing resource in a performant manner. |
||
| 356 | * If you need to check certain fields on an object (and don't need everything), this |
||
| 357 | * is a better way to get what you need. |
||
| 358 | * If the record is not present, you will receive null. If you don't need an hydrated |
||
| 359 | * instance, make sure to pass false there. |
||
| 360 | * |
||
| 361 | * @param mixed $id record id |
||
| 362 | * @param array $fields list of fields you need. |
||
| 363 | * @param bool $hydrate whether to hydrate object or not |
||
| 364 | * |
||
| 365 | * @return array|null|object |
||
| 366 | */ |
||
| 367 | 4 | public function selectSingleFields($id, array $fields, $hydrate = true) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * get classname of entity |
||
| 384 | * |
||
| 385 | * @return string|null |
||
| 386 | */ |
||
| 387 | 4 | public function getEntityClass() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * {@inheritDoc} |
||
| 398 | * |
||
| 399 | * Currently this is being used to build the route id used for redirecting |
||
| 400 | * to newly made documents. It might benefit from having a different name |
||
| 401 | * for those purposes. |
||
| 402 | * |
||
| 403 | * We might use a convention based mapping here: |
||
| 404 | * Graviton\CoreBundle\Document\App -> mongodb://graviton_core |
||
| 405 | * Graviton\CoreBundle\Entity\Table -> mysql://graviton_core |
||
| 406 | * |
||
| 407 | * @todo implement this in a more convention based manner |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | public function getConnectionName() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Does the actual query using the RQL Bundle. |
||
| 420 | * |
||
| 421 | * @param Builder $queryBuilder Doctrine ODM QueryBuilder |
||
| 422 | * @param Query $query query from parser |
||
| 423 | * |
||
| 424 | * @return array |
||
| 425 | */ |
||
| 426 | protected function doRqlQuery($queryBuilder, Query $query) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Checks the recordOrigin attribute of a record and will throw an exception if value is not allowed |
||
| 435 | * |
||
| 436 | * @param Object $record record |
||
| 437 | * |
||
| 438 | * @return void |
||
| 439 | */ |
||
| 440 | 14 | protected function checkIfOriginRecord($record) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Determines the configured amount fo data records to be returned in pagination context. |
||
| 458 | * |
||
| 459 | * @return int |
||
| 460 | */ |
||
| 461 | private function getDefaultLimit() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param Boolean $active active |
||
| 472 | * @param String $field field |
||
| 473 | * @return void |
||
| 474 | */ |
||
| 475 | 4 | public function setFilterByAuthUser($active, $field) |
|
| 480 | } |
||
| 481 |
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.