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 bool|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) |
|
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) |
|
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 | */ |
||
161 | 36 | public function insert(AnnotatedInterface $model = null) |
|
182 | |||
183 | /** |
||
184 | * Updates the row represented by this active document. |
||
185 | * All loaded attributes will be saved to the database. |
||
186 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
187 | * |
||
188 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
189 | * meaning all attributes that are loaded from DB will be saved. |
||
190 | * @return boolean whether the update is successful |
||
191 | * @throws ManganException if the record is new |
||
192 | * @throws ManganException on fail of update |
||
193 | * @throws ManganException on timeout of db operation , when safe flag is set to true |
||
194 | */ |
||
195 | 6 | public function update(array $attributes = null) |
|
211 | |||
212 | /** |
||
213 | * Updates one document with the specified criteria and attributes |
||
214 | * |
||
215 | * This is more *raw* update: |
||
216 | * |
||
217 | * * Does not raise any events or signals |
||
218 | * * Does not perform any validation |
||
219 | * |
||
220 | * @param array|CriteriaInterface $criteria query criteria. |
||
221 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
222 | * @param bool Whether tu force update/upsert document |
||
223 | * meaning all attributes that are loaded from DB will be saved. |
||
224 | * @return bool |
||
225 | */ |
||
226 | 85 | public function updateOne($criteria = null, array $attributes = null, $modify = false) |
|
279 | |||
280 | /** |
||
281 | * Atomic, in-place update method. |
||
282 | * |
||
283 | * @param Modifier $modifier updating rules to apply |
||
284 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
285 | * @return boolean |
||
286 | */ |
||
287 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
306 | |||
307 | /** |
||
308 | * Replaces the current document. |
||
309 | * |
||
310 | * **NOTE: This will overwrite entire document.** |
||
311 | * Any filtered out properties will be removed as well. |
||
312 | * |
||
313 | * The record is inserted as a document into the database collection, if exists it will be replaced. |
||
314 | * |
||
315 | * Validation will be performed before saving the record. If the validation fails, |
||
316 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
317 | * validation errors. |
||
318 | * |
||
319 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
320 | * If the validation fails, the record will not be saved to database. |
||
321 | * |
||
322 | * @return boolean whether the saving succeeds |
||
323 | */ |
||
324 | public function replace($runValidation = true) |
||
357 | |||
358 | /** |
||
359 | * Saves the current document. |
||
360 | * |
||
361 | * The record is inserted as a document into the database collection or updated if exists. |
||
362 | * |
||
363 | * Filtered out properties will remain in database - it is partial safe. |
||
364 | * |
||
365 | * Validation will be performed before saving the record. If the validation fails, |
||
366 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
367 | * validation errors. |
||
368 | * |
||
369 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
370 | * If the validation fails, the record will not be saved to database. |
||
371 | * |
||
372 | * @return boolean whether the saving succeeds |
||
373 | */ |
||
374 | 82 | public function save($runValidation = true) |
|
378 | |||
379 | /** |
||
380 | * Updates or inserts the current document. This will try to update existing fields. |
||
381 | * Will keep already stored data if present in document. |
||
382 | * |
||
383 | * If document does not exist, a new one will be inserted. |
||
384 | * |
||
385 | * @param boolean $runValidation |
||
386 | * @return boolean |
||
387 | * @throws ManganException |
||
388 | */ |
||
389 | 86 | public function upsert($runValidation = true) |
|
428 | |||
429 | /** |
||
430 | * Reloads document from database. |
||
431 | * It return true if document is reloaded and false if it's no longer exists. |
||
432 | * |
||
433 | * @return boolean |
||
434 | */ |
||
435 | 2 | public function refresh() |
|
449 | |||
450 | /** |
||
451 | * Deletes the document from database. |
||
452 | * @return boolean whether the deletion is successful. |
||
453 | */ |
||
454 | 10 | public function delete() |
|
476 | |||
477 | /** |
||
478 | * Deletes one document with the specified primary keys. |
||
479 | * <b>Does not raise beforeDelete</b> |
||
480 | * See {@link find()} for detailed explanation about $condition and $params. |
||
481 | * @param array|CriteriaInterface $criteria query criteria. |
||
482 | * @return bool |
||
483 | */ |
||
484 | 11 | public function deleteOne($criteria = null) |
|
493 | |||
494 | /** |
||
495 | * Deletes document with the specified primary key. |
||
496 | * See {@link find()} for detailed explanation about $condition and $params. |
||
497 | * @param mixed $pkValue primary key value(s). Use array for multiple primary keys. For |
||
498 | * composite key, each key value must be an array (column name=>column |
||
499 | * value). |
||
500 | * @param array|CriteriaInterface $criteria query criteria. |
||
501 | * |
||
502 | * @return bool |
||
503 | */ |
||
504 | 3 | public function deleteByPk($pkValue, $criteria = null) |
|
518 | |||
519 | /** |
||
520 | * Deletes documents with the specified primary keys. |
||
521 | * See {@link find()} for detailed explanation about $condition and $params. |
||
522 | * @param mixed[] $pkValues Primary keys array |
||
523 | * @param array|CriteriaInterface $criteria query criteria. |
||
524 | * @return bool |
||
525 | */ |
||
526 | 3 | public function deleteAllByPk($pkValues, $criteria = null) |
|
541 | |||
542 | /** |
||
543 | * Deletes documents with the specified primary keys. |
||
544 | * |
||
545 | * **Does not raise beforeDelete event and does not emit signals** |
||
546 | * |
||
547 | * See {@link find()} for detailed explanation about $condition and $params. |
||
548 | * |
||
549 | * @param array|CriteriaInterface $criteria query criteria. |
||
550 | * @return bool |
||
551 | */ |
||
552 | 5 | public function deleteAll($criteria = null) |
|
563 | |||
564 | 117 | public function getCollection() |
|
568 | |||
569 | /** |
||
570 | * Make status uniform |
||
571 | * @param bool|array $result |
||
572 | * @return bool Return true if succeed |
||
573 | */ |
||
574 | 19 | private function deleteResult($result) |
|
583 | |||
584 | /** |
||
585 | * Make status uniform |
||
586 | * @param bool|array $result |
||
587 | * @return bool Return true if succeed |
||
588 | */ |
||
589 | 36 | private function insertResult($result) |
|
598 | |||
599 | /** |
||
600 | * Make status uniform |
||
601 | * @param bool|array $result |
||
602 | * @return bool Return true if succeed |
||
603 | */ |
||
604 | 85 | private function updateResult($result) |
|
613 | |||
614 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
615 | |||
616 | 86 | private function beforeValidate($model) |
|
620 | |||
621 | /** |
||
622 | * Take care of EventBeforeSave |
||
623 | * @see EventBeforeSave |
||
624 | * @param $model |
||
625 | * @param string $event |
||
626 | * @return boolean |
||
627 | */ |
||
628 | 118 | private function beforeSave($model, $event = null) |
|
642 | |||
643 | /** |
||
644 | * Take care of EventAfterSave |
||
645 | * @see EventAfterSave |
||
646 | * @param $model |
||
647 | * @param null|ModelEvent $event |
||
648 | */ |
||
649 | 115 | private function afterSave($model, $event = null) |
|
660 | |||
661 | /** |
||
662 | * This method is invoked before deleting a record. |
||
663 | * The default implementation raises the {@link onBeforeDelete} event. |
||
664 | * You may override this method to do any preparation work for record deletion. |
||
665 | * Make sure you call the parent implementation so that the event is raised properly. |
||
666 | * @return boolean whether the record should be deleted. Defaults to true. |
||
667 | */ |
||
668 | 14 | private function beforeDelete() |
|
679 | |||
680 | /** |
||
681 | * This method is invoked after deleting a record. |
||
682 | * The default implementation raises the {@link onAfterDelete} event. |
||
683 | * You may override this method to do postprocessing after the record is deleted. |
||
684 | * Make sure you call the parent implementation so that the event is raised properly. |
||
685 | */ |
||
686 | 9 | private function afterDelete() |
|
693 | |||
694 | // </editor-fold> |
||
695 | } |
||
696 |
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: