Complex classes like EntityManager 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 EntityManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class EntityManager implements EntityManagerInterface |
||
| 43 | { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Model |
||
| 47 | * @var AnnotatedInterface |
||
| 48 | */ |
||
| 49 | public $model = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * |
||
| 53 | * @var ScopeManager |
||
| 54 | */ |
||
| 55 | private $sm = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * |
||
| 59 | * @var |
||
| 60 | */ |
||
| 61 | public $meta = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Options |
||
| 65 | * @var EntityOptions |
||
| 66 | */ |
||
| 67 | public $options = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Current collection name |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public $collectionName = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Validator instance |
||
| 77 | * @var Validator |
||
| 78 | */ |
||
| 79 | private $validator = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Current collection |
||
| 83 | * @var MongoCollection |
||
| 84 | */ |
||
| 85 | private $_collection = null; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Result of last operation |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | private $lastResult = []; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Create entity manager |
||
| 95 | * @param AnnotatedInterface $model |
||
| 96 | * @param Mangan $mangan |
||
| 97 | * @throws ManganException |
||
| 98 | */ |
||
| 99 | 122 | public function __construct(AnnotatedInterface $model, Mangan $mangan = null) |
|
| 100 | { |
||
| 101 | 122 | $this->model = $model; |
|
| 102 | 122 | $this->sm = new ScopeManager($model); |
|
| 103 | 122 | $this->options = new EntityOptions($model); |
|
| 104 | 122 | $this->collectionName = CollectionNamer::nameCollection($model); |
|
| 105 | 122 | $this->meta = ManganMeta::create($model); |
|
| 106 | 122 | $this->validator = new Validator($model); |
|
| 107 | 122 | if (null === $mangan) |
|
| 108 | { |
||
| 109 | 119 | $mangan = Mangan::fromModel($model); |
|
| 110 | } |
||
| 111 | 122 | if (!$this->collectionName) |
|
| 112 | { |
||
| 113 | throw new ManganException(sprintf('Invalid collection name for model: `%s`', $this->meta->type()->name)); |
||
| 114 | } |
||
| 115 | 122 | $this->_collection = new MongoCollection($mangan->getDbInstance(), $this->collectionName); |
|
| 116 | 122 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Create model related entity manager. |
||
| 120 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
| 121 | * If no custom entity manager is defined this will return default EntityManager. |
||
| 122 | * @param AnnotatedInterface $model |
||
| 123 | * @param Mangan $mangan |
||
| 124 | * @return EntityManagerInterface |
||
| 125 | */ |
||
| 126 | 113 | public static function create($model, Mangan $mangan = null) |
|
| 127 | { |
||
| 128 | 113 | $emClass = ManganMeta::create($model)->type()->entityManager ?: static::class; |
|
| 129 | 113 | return new $emClass($model, $mangan); |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Set attributes en masse. |
||
| 134 | * Attributes will be filtered according to SafeAnnotation. |
||
| 135 | * Only attributes marked as safe will be set, other will be ignored. |
||
| 136 | * |
||
| 137 | * @param mixed[] $attributes |
||
| 138 | */ |
||
| 139 | public function setAttributes($attributes) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Inserts a row into the table based on this active record attributes. |
||
| 146 | * If the table's primary key is auto-incremental and is null before insertion, |
||
| 147 | * it will be populated with the actual value after insertion. |
||
| 148 | * |
||
| 149 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 150 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
| 151 | * and its {@link scenario} property will be set to be 'update'. |
||
| 152 | * |
||
| 153 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 154 | * |
||
| 155 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
| 156 | * @throws ManganException if the record is not new |
||
| 157 | * @throws ManganException on fail of insert or insert of empty document |
||
| 158 | * @throws ManganException on fail of insert, when safe flag is set to true |
||
| 159 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
| 160 | * @since v1.0 |
||
| 161 | */ |
||
| 162 | 36 | public function insert(AnnotatedInterface $model = null) |
|
| 163 | { |
||
| 164 | 36 | $model = $model ?: $this->model; |
|
| 165 | 36 | if ($this->_beforeSave($model, EntityManagerInterface::EventBeforeInsert)) |
|
| 166 | { |
||
| 167 | 36 | $rawData = RawArray::fromModel($model); |
|
| 168 | /** |
||
| 169 | * TODO Save options ara failing with message: |
||
| 170 | * Unrecognized write concern field: authMechanism |
||
| 171 | * $this->options->getSaveOptions() |
||
| 172 | */ |
||
| 173 | 36 | $rawResult = $this->_collection->insert($rawData); |
|
| 174 | 36 | $result = $this->_result($rawResult, true); |
|
| 175 | |||
| 176 | 36 | if ($result) |
|
| 177 | { |
||
| 178 | 36 | $this->_afterSave($model, EntityManagerInterface::EventAfterInsert); |
|
| 179 | 36 | return true; |
|
| 180 | } |
||
| 181 | throw new ManganException('Can\t save the document to disk, or attempting to save an empty document. ' . ucfirst($rawResult['errmsg']), $rawResult['code']); |
||
| 182 | } |
||
| 183 | AspectManager::removeAspect($model, self::AspectSaving); |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Updates the row represented by this active document. |
||
| 189 | * All loaded attributes will be saved to the database. |
||
| 190 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 191 | * |
||
| 192 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
| 193 | * meaning all attributes that are loaded from DB will be saved. |
||
| 194 | |||
| 195 | * @return boolean whether the update is successful |
||
| 196 | * @throws ManganException if the record is new |
||
| 197 | * @throws ManganException on fail of update |
||
| 198 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
| 199 | * @since v1.0 |
||
| 200 | */ |
||
| 201 | 6 | public function update(array $attributes = null) |
|
| 202 | { |
||
| 203 | 6 | if ($this->_beforeSave($this->model, EntityManagerInterface::EventBeforeUpdate)) |
|
| 204 | { |
||
| 205 | 6 | $criteria = PkManager::prepareFromModel($this->model); |
|
| 206 | 6 | $result = $this->updateOne($criteria, $attributes); |
|
| 207 | 5 | if ($result) |
|
| 208 | { |
||
| 209 | 5 | $this->_afterSave($this->model, EntityManagerInterface::EventAfterUpdate); |
|
| 210 | 5 | return true; |
|
| 211 | } |
||
| 212 | throw new ManganException('Can\t save the document to disk, or attempting to save an empty document.'); |
||
| 213 | } |
||
| 214 | AspectManager::removeAspect($this->model, self::AspectSaving); |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Updates one document with the specified criteria and attributes |
||
| 220 | * |
||
| 221 | * This is more *raw* update: |
||
| 222 | * |
||
| 223 | * * Does not raise any events or signals |
||
| 224 | * * Does not perform any validation |
||
| 225 | * |
||
| 226 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 227 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
| 228 | * @param bool Whether tu force update/upsert document |
||
| 229 | * meaning all attributes that are loaded from DB will be saved. |
||
| 230 | * @since v1.0 |
||
| 231 | */ |
||
| 232 | 85 | public function updateOne($criteria = null, array $attributes = null, $modify = false) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Atomic, in-place update method. |
||
| 285 | * |
||
| 286 | * @since v1.3.6 |
||
| 287 | * @param Modifier $modifier updating rules to apply |
||
| 288 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
| 289 | * @return boolean |
||
| 290 | */ |
||
| 291 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Replaces the current document. |
||
| 310 | * |
||
| 311 | * **NOTE: This will overwrite entire document.** |
||
| 312 | * Any filtered out properties will be removed as well. |
||
| 313 | * |
||
| 314 | * The record is inserted as a documnent into the database collection, if exists it will be replaced. |
||
| 315 | * |
||
| 316 | * Validation will be performed before saving the record. If the validation fails, |
||
| 317 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
| 318 | * validation errors. |
||
| 319 | * |
||
| 320 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
| 321 | * If the validation fails, the record will not be saved to database. |
||
| 322 | * |
||
| 323 | * @return boolean whether the saving succeeds |
||
| 324 | * @since v1.0 |
||
| 325 | */ |
||
| 326 | public function replace($runValidation = true) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Saves the current document. |
||
| 361 | * |
||
| 362 | * The record is inserted as a document into the database collection or updated if exists. |
||
| 363 | * |
||
| 364 | * Filtered out properties will remain in database - it is partial safe. |
||
| 365 | * |
||
| 366 | * Validation will be performed before saving the record. If the validation fails, |
||
| 367 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
| 368 | * validation errors. |
||
| 369 | * |
||
| 370 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
| 371 | * If the validation fails, the record will not be saved to database. |
||
| 372 | * |
||
| 373 | * @return boolean whether the saving succeeds |
||
| 374 | * @since v1.0 |
||
| 375 | */ |
||
| 376 | 82 | public function save($runValidation = true) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Updates or inserts the current document. This will try to update existing fields. |
||
| 383 | * Will keep already stored data if present in document. |
||
| 384 | * |
||
| 385 | * If document does not exist, a new one will be inserted. |
||
| 386 | * |
||
| 387 | * @param boolean $runValidation |
||
| 388 | * @return boolean |
||
| 389 | * @throws ManganException |
||
| 390 | */ |
||
| 391 | 86 | public function upsert($runValidation = true) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Reloads document from database. |
||
| 432 | * It return true if document is reloaded and false if it's no longer exists. |
||
| 433 | * |
||
| 434 | * @return boolean |
||
| 435 | */ |
||
| 436 | 2 | public function refresh() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Deletes the document from database. |
||
| 453 | * @return boolean whether the deletion is successful. |
||
| 454 | * @throws ManganException if the record is new |
||
| 455 | */ |
||
| 456 | 10 | public function delete() |
|
| 477 | |||
| 478 | /** |
||
| 479 | * Deletes one document with the specified primary keys. |
||
| 480 | * <b>Does not raise beforeDelete</b> |
||
| 481 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 482 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 483 | * @since v1.0 |
||
| 484 | */ |
||
| 485 | 11 | public function deleteOne($criteria = null) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Deletes document with the specified primary key. |
||
| 497 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 498 | * @param mixed $pkValue primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value). |
||
| 499 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 500 | * @since v1.0 |
||
| 501 | */ |
||
| 502 | 3 | public function deleteByPk($pkValue, $criteria = null) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Deletes documents with the specified primary keys. |
||
| 519 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 520 | * @param mixed[] $pkValues Primary keys array |
||
| 521 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 522 | * @since v1.0 |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | 3 | public function deleteAllByPk($pkValues, $criteria = null) |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Deletes documents with the specified primary keys. |
||
| 541 | * |
||
| 542 | * **Does not raise beforeDelete event and does not emit signals** |
||
| 543 | * |
||
| 544 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 545 | * |
||
| 546 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 547 | * @since v1.0 |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | 5 | public function deleteAll($criteria = null) |
|
| 558 | |||
| 559 | 117 | public function getCollection() |
|
| 563 | |||
| 564 | /** |
||
| 565 | * Make status uniform |
||
| 566 | * @param bool|array $result |
||
| 567 | * @param bool $insert Set to true for inserts |
||
| 568 | * @return bool Return true if succeed |
||
| 569 | */ |
||
| 570 | 116 | private function _result($result, $insert = false) |
|
| 583 | |||
| 584 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
| 585 | |||
| 586 | /** |
||
| 587 | * Take care of EventBeforeSave |
||
| 588 | * @see EventBeforeSave |
||
| 589 | * @return boolean |
||
| 590 | */ |
||
| 591 | 118 | private function _beforeSave($model, $event = null) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Take care of EventAfterSave |
||
| 608 | * @see EventAfterSave |
||
| 609 | */ |
||
| 610 | 115 | private function _afterSave($model, $event = null) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * This method is invoked before deleting a record. |
||
| 624 | * The default implementation raises the {@link onBeforeDelete} event. |
||
| 625 | * You may override this method to do any preparation work for record deletion. |
||
| 626 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 627 | * @return boolean whether the record should be deleted. Defaults to true. |
||
| 628 | * @since v1.0 |
||
| 629 | */ |
||
| 630 | 14 | private function _beforeDelete() |
|
| 641 | |||
| 642 | /** |
||
| 643 | * This method is invoked after deleting a record. |
||
| 644 | * The default implementation raises the {@link onAfterDelete} event. |
||
| 645 | * You may override this method to do postprocessing after the record is deleted. |
||
| 646 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 647 | * @since v1.0 |
||
| 648 | */ |
||
| 649 | 9 | private function _afterDelete() |
|
| 656 | |||
| 657 | // </editor-fold> |
||
| 658 | } |
||
| 659 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: