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 |
||
43 | class EntityManager implements EntityManagerInterface |
||
44 | { |
||
45 | |||
46 | use CreateModel; |
||
47 | |||
48 | /** |
||
49 | * Model |
||
50 | * @var AnnotatedInterface |
||
51 | */ |
||
52 | public $model = null; |
||
53 | |||
54 | /** |
||
55 | * |
||
56 | * @var ScopeManager |
||
57 | */ |
||
58 | private $sm = null; |
||
59 | |||
60 | /** |
||
61 | * |
||
62 | * @var |
||
63 | */ |
||
64 | public $meta = null; |
||
65 | |||
66 | /** |
||
67 | * Options |
||
68 | * @var EntityOptions |
||
69 | */ |
||
70 | public $options = null; |
||
71 | |||
72 | /** |
||
73 | * Current collection name |
||
74 | * @var string |
||
75 | */ |
||
76 | public $collectionName = ''; |
||
77 | |||
78 | /** |
||
79 | * Validator instance |
||
80 | * @var Validator |
||
81 | */ |
||
82 | private $validator = null; |
||
83 | |||
84 | /** |
||
85 | * Current collection |
||
86 | * @var MongoCollection |
||
87 | */ |
||
88 | private $_collection = null; |
||
89 | |||
90 | /** |
||
91 | * Result of last operation |
||
92 | * @var bool|array |
||
93 | */ |
||
94 | private $lastResult = []; |
||
95 | |||
96 | /** |
||
97 | * Create entity manager |
||
98 | * @param AnnotatedInterface $model |
||
99 | * @param Mangan $mangan |
||
100 | * @throws ManganException |
||
101 | */ |
||
102 | 134 | public function __construct(AnnotatedInterface $model, Mangan $mangan = null) |
|
120 | |||
121 | /** |
||
122 | * @return AnnotatedInterface |
||
123 | */ |
||
124 | 1 | public function getModel(): AnnotatedInterface |
|
128 | |||
129 | /** |
||
130 | * Create model related entity manager. |
||
131 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
132 | * If no custom entity manager is defined this will return default EntityManager. |
||
133 | * @param AnnotatedInterface $model |
||
134 | * @param Mangan $mangan |
||
135 | * @return EntityManagerInterface |
||
136 | */ |
||
137 | 124 | public static function create($model, Mangan $mangan = null) |
|
142 | |||
143 | /** |
||
144 | * Set attributes en masse. |
||
145 | * Attributes will be filtered according to SafeAnnotation. |
||
146 | * Only attributes marked as safe will be set, other will be ignored. |
||
147 | * |
||
148 | * @param mixed[] $attributes |
||
149 | */ |
||
150 | public function setAttributes($attributes) |
||
154 | |||
155 | /** |
||
156 | * Inserts a row into the table based on this active record attributes. |
||
157 | * If the table's primary key is auto-incremental and is null before insertion, |
||
158 | * it will be populated with the actual value after insertion. |
||
159 | * |
||
160 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
161 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
162 | * and its {@link scenario} property will be set to be 'update'. |
||
163 | * |
||
164 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
165 | * |
||
166 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
167 | * @throws ManganException if the record is not new |
||
168 | * @throws ManganException on fail of insert or insert of empty document |
||
169 | * @throws ManganException on fail of insert, when safe flag is set to true |
||
170 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
171 | */ |
||
172 | 37 | public function insert(AnnotatedInterface $model = null) |
|
193 | |||
194 | /** |
||
195 | * Updates the row represented by this active document. |
||
196 | * All loaded attributes will be saved to the database. |
||
197 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
198 | * |
||
199 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
200 | * meaning all attributes that are loaded from DB will be saved. |
||
201 | * @return boolean whether the update is successful |
||
202 | * @throws ManganException if the record is new |
||
203 | * @throws ManganException on fail of update |
||
204 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
205 | */ |
||
206 | 6 | public function update(array $attributes = null) |
|
222 | |||
223 | /** |
||
224 | * Updates one document with the specified criteria and attributes |
||
225 | * |
||
226 | * This is more *raw* update: |
||
227 | * |
||
228 | * * Does not raise any events or signals |
||
229 | * * Does not perform any validation |
||
230 | * |
||
231 | * @param array|CriteriaInterface $criteria query criteria. |
||
232 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
233 | * @param bool Whether tu force update/upsert document |
||
234 | * meaning all attributes that are loaded from DB will be saved. |
||
235 | * @return bool |
||
236 | */ |
||
237 | 95 | public function updateOne($criteria = null, array $attributes = null, $modify = false) |
|
290 | |||
291 | /** |
||
292 | * Atomic, in-place update method. This method does not raise |
||
293 | * events and does not emit signals. |
||
294 | * |
||
295 | * @param Modifier $modifier updating rules to apply |
||
296 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
297 | * @return boolean |
||
298 | */ |
||
299 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
318 | |||
319 | /** |
||
320 | * Find and modify single document atomically. |
||
321 | * |
||
322 | * By default this function will return updated document, ie document |
||
323 | * with applied Modifier operations. |
||
324 | * |
||
325 | * To return document before applied updates, set parameter |
||
326 | * `$returnUpdated` to false. |
||
327 | * |
||
328 | * This function will raise events and signals before operation on |
||
329 | * current model. |
||
330 | * |
||
331 | * The events and signals after operation will be performed |
||
332 | * on the returned model, depending on `$returnUpdated` parameter. |
||
333 | * |
||
334 | * @param array|CriteriaInterface $criteria |
||
335 | * @param Modifier $modifier |
||
336 | * @param bool $returnUpdated |
||
337 | * @return AnnotatedInterface|null |
||
338 | */ |
||
339 | 2 | public function findAndModify($criteria, Modifier $modifier, $returnUpdated = true) |
|
370 | |||
371 | |||
372 | /** |
||
373 | * Replaces the current document. |
||
374 | * |
||
375 | * **NOTE: This will overwrite entire document.** |
||
376 | * Any filtered out properties will be removed as well. |
||
377 | * |
||
378 | * The record is inserted as a document into the database collection, if exists it will be replaced. |
||
379 | * |
||
380 | * Validation will be performed before saving the record. If the validation fails, |
||
381 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
382 | * validation errors. |
||
383 | * |
||
384 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
385 | * If the validation fails, the record will not be saved to database. |
||
386 | * |
||
387 | * @return boolean whether the saving succeeds |
||
388 | */ |
||
389 | public function replace($runValidation = true) |
||
422 | |||
423 | /** |
||
424 | * Saves the current document. |
||
425 | * |
||
426 | * The record is inserted as a document into the database collection or updated if exists. |
||
427 | * |
||
428 | * Filtered out properties will remain in database - it is partial safe. |
||
429 | * |
||
430 | * Validation will be performed before saving the record. If the validation fails, |
||
431 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
432 | * validation errors. |
||
433 | * |
||
434 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
435 | * If the validation fails, the record will not be saved to database. |
||
436 | * |
||
437 | * @return boolean whether the saving succeeds |
||
438 | */ |
||
439 | 92 | public function save($runValidation = true) |
|
443 | |||
444 | /** |
||
445 | * Updates or inserts the current document. This will try to update existing fields. |
||
446 | * Will keep already stored data if present in document. |
||
447 | * |
||
448 | * If document does not exist, a new one will be inserted. |
||
449 | * |
||
450 | * @param boolean $runValidation |
||
451 | * @return boolean |
||
452 | * @throws ManganException |
||
453 | */ |
||
454 | 96 | public function upsert($runValidation = true) |
|
493 | |||
494 | /** |
||
495 | * Reloads document from database. |
||
496 | * It return true if document is reloaded and false if it's no longer exists. |
||
497 | * |
||
498 | * @return boolean |
||
499 | */ |
||
500 | 2 | public function refresh() |
|
514 | |||
515 | /** |
||
516 | * Deletes the document from database. |
||
517 | * @return boolean whether the deletion is successful. |
||
518 | */ |
||
519 | 10 | public function delete() |
|
541 | |||
542 | /** |
||
543 | * Deletes one document with the specified primary keys. |
||
544 | * <b>Does not raise beforeDelete</b> |
||
545 | * See {@link find()} for detailed explanation about $condition and $params. |
||
546 | * @param array|CriteriaInterface $criteria query criteria. |
||
547 | * @return bool |
||
548 | */ |
||
549 | 11 | public function deleteOne($criteria = null) |
|
558 | |||
559 | /** |
||
560 | * Deletes document with the specified primary key. |
||
561 | * See {@link find()} for detailed explanation about $condition and $params. |
||
562 | * @param mixed $pkValue primary key value(s). Use array for multiple primary keys. For |
||
563 | * composite key, each key value must be an array (column name=>column |
||
564 | * value). |
||
565 | * @param array|CriteriaInterface $criteria query criteria. |
||
566 | * |
||
567 | * @return bool |
||
568 | */ |
||
569 | 3 | public function deleteByPk($pkValue, $criteria = null) |
|
583 | |||
584 | /** |
||
585 | * Deletes documents with the specified primary keys. |
||
586 | * See {@link find()} for detailed explanation about $condition and $params. |
||
587 | * @param mixed[] $pkValues Primary keys array |
||
588 | * @param array|CriteriaInterface $criteria query criteria. |
||
589 | * @return bool |
||
590 | */ |
||
591 | 3 | public function deleteAllByPk($pkValues, $criteria = null) |
|
606 | |||
607 | /** |
||
608 | * Deletes documents with the specified primary keys. |
||
609 | * |
||
610 | * **Does not raise beforeDelete event and does not emit signals** |
||
611 | * |
||
612 | * See {@link find()} for detailed explanation about $condition and $params. |
||
613 | * |
||
614 | * @param array|CriteriaInterface $criteria query criteria. |
||
615 | * @return bool |
||
616 | */ |
||
617 | 5 | public function deleteAll($criteria = null) |
|
628 | |||
629 | 128 | public function getCollection() |
|
633 | |||
634 | /** |
||
635 | * Make status uniform |
||
636 | * @param bool|array $result |
||
637 | * @return bool Return true if succeed |
||
638 | */ |
||
639 | 19 | private function deleteResult($result) |
|
648 | |||
649 | /** |
||
650 | * Make status uniform |
||
651 | * @param bool|array $result |
||
652 | * @return bool Return true if succeed |
||
653 | */ |
||
654 | 37 | private function insertResult($result) |
|
663 | |||
664 | /** |
||
665 | * Make status uniform |
||
666 | * @param bool|array $result |
||
667 | * @return bool Return true if succeed |
||
668 | */ |
||
669 | 94 | private function updateResult($result) |
|
678 | |||
679 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
680 | |||
681 | 96 | private function beforeValidate($model) |
|
685 | |||
686 | /** |
||
687 | * Take care of EventBeforeSave |
||
688 | * @see EventBeforeSave |
||
689 | * @param $model |
||
690 | * @param string $event |
||
691 | * @return boolean |
||
692 | */ |
||
693 | 130 | private function beforeSave($model, $event = null) |
|
707 | |||
708 | /** |
||
709 | * Take care of EventAfterSave |
||
710 | * @see EventAfterSave |
||
711 | * @param $model |
||
712 | * @param null|ModelEvent $event |
||
713 | */ |
||
714 | 125 | private function afterSave($model, $event = null) |
|
725 | |||
726 | /** |
||
727 | * This method is invoked before deleting a record. |
||
728 | * The default implementation raises the {@link onBeforeDelete} event. |
||
729 | * You may override this method to do any preparation work for record deletion. |
||
730 | * Make sure you call the parent implementation so that the event is raised properly. |
||
731 | * @return boolean whether the record should be deleted. Defaults to true. |
||
732 | */ |
||
733 | 14 | private function beforeDelete() |
|
744 | |||
745 | /** |
||
746 | * This method is invoked after deleting a record. |
||
747 | * The default implementation raises the {@link onAfterDelete} event. |
||
748 | * You may override this method to do postprocessing after the record is deleted. |
||
749 | * Make sure you call the parent implementation so that the event is raised properly. |
||
750 | */ |
||
751 | 9 | private function afterDelete() |
|
758 | |||
759 | // </editor-fold> |
||
760 | } |
||
761 |
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: