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 | /** |
||
| 47 | * Model |
||
| 48 | * @var AnnotatedInterface |
||
| 49 | */ |
||
| 50 | public $model = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * |
||
| 54 | * @var EventDispatcher |
||
| 55 | */ |
||
| 56 | public $ed = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | * @var ScopeManager |
||
| 61 | */ |
||
| 62 | private $sm = null; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * |
||
| 66 | * @var |
||
| 67 | */ |
||
| 68 | public $meta = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Options |
||
| 72 | * @var EntityOptions |
||
| 73 | */ |
||
| 74 | public $options = null; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Current collection name |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | public $collectionName = ''; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Validator instance |
||
| 84 | * @var Validator |
||
| 85 | */ |
||
| 86 | private $validator = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Current collection |
||
| 90 | * @var MongoCollection |
||
| 91 | */ |
||
| 92 | private $_collection = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Create entity manager |
||
| 96 | * @param AnnotatedInterface $model |
||
| 97 | * @param Mangan $mangan |
||
| 98 | * @throws ManganException |
||
| 99 | */ |
||
| 100 | 84 | public function __construct(AnnotatedInterface $model, Mangan $mangan = null) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * Create model related entity manager. |
||
| 121 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
| 122 | * If no custom entity manager is defined this will return default EntityManager. |
||
| 123 | * @param AnnotatedInterface $model |
||
| 124 | * @param Mangan $mangan |
||
| 125 | * @return EntityManagerInterface |
||
| 126 | */ |
||
| 127 | 77 | public static function create($model, Mangan $mangan = null) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Set attributes en masse. |
||
| 135 | * Attributes will be filtered according to SafeAnnotation. |
||
| 136 | * Only attributes marked as safe will be set, other will be ignored. |
||
| 137 | * |
||
| 138 | * @param mixed[] $atributes |
||
| 139 | */ |
||
| 140 | public function setAttributes($atributes) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Inserts a row into the table based on this active record attributes. |
||
| 147 | * If the table's primary key is auto-incremental and is null before insertion, |
||
| 148 | * it will be populated with the actual value after insertion. |
||
| 149 | * |
||
| 150 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 151 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
| 152 | * and its {@link scenario} property will be set to be 'update'. |
||
| 153 | * |
||
| 154 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 155 | * |
||
| 156 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
| 157 | * @throws MongoException if the record is not new |
||
| 158 | * @throws MongoException on fail of insert or insert of empty document |
||
| 159 | * @throws MongoException on fail of insert, when safe flag is set to true |
||
| 160 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 161 | * @since v1.0 |
||
| 162 | */ |
||
| 163 | 30 | public function insert(AnnotatedInterface $model = null) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Updates the row represented by this active document. |
||
| 184 | * All loaded attributes will be saved to the database. |
||
| 185 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 186 | * |
||
| 187 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
| 188 | * meaning all attributes that are loaded from DB will be saved. |
||
| 189 | |||
| 190 | * @return boolean whether the update is successful |
||
| 191 | * @throws MongoException if the record is new |
||
| 192 | * @throws MongoException on fail of update |
||
| 193 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 194 | * @since v1.0 |
||
| 195 | */ |
||
| 196 | 5 | 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 | * @since v1.0 |
||
| 225 | */ |
||
| 226 | 11 | public function updateOne($criteria = null, array $attributes = null, $modify = false) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * Atomic, in-place update method. |
||
| 271 | * |
||
| 272 | * @since v1.3.6 |
||
| 273 | * @param Modifier $modifier updating rules to apply |
||
| 274 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | 1 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Saves the current document. |
||
| 296 | * |
||
| 297 | * **NOTE: This will overwrite entire document.** |
||
| 298 | * |
||
| 299 | * The record is inserted as a row into the database collection or updated if exists. |
||
| 300 | * |
||
| 301 | * Validation will be performed before saving the record. If the validation fails, |
||
| 302 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
| 303 | * validation errors. |
||
| 304 | * |
||
| 305 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
| 306 | * If the validation fails, the record will not be saved to database. |
||
| 307 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
|
|
|||
| 308 | * @return boolean whether the saving succeeds |
||
| 309 | * @since v1.0 |
||
| 310 | */ |
||
| 311 | 54 | public function save($runValidation = true) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Updates or inserts the current document. This will try to update existing fields. |
||
| 339 | * Will keep already stored data if present in document. |
||
| 340 | * |
||
| 341 | * If document does not exist, a new one will be inserted. |
||
| 342 | * |
||
| 343 | * @param boolean $runValidation |
||
| 344 | * @return boolean |
||
| 345 | * @throws MongoException |
||
| 346 | */ |
||
| 347 | 5 | public function upsert($runValidation = true) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Reloads document from database. |
||
| 381 | * It return true if document is reloaded and false if it's no longer exists. |
||
| 382 | * |
||
| 383 | * @return boolean |
||
| 384 | */ |
||
| 385 | 2 | public function refresh() |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Deletes the document from database. |
||
| 402 | * @return boolean whether the deletion is successful. |
||
| 403 | * @throws MongoException if the record is new |
||
| 404 | */ |
||
| 405 | 8 | public function delete() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Deletes one document with the specified primary keys. |
||
| 429 | * <b>Does not raise beforeDelete</b> |
||
| 430 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 431 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 432 | * @since v1.0 |
||
| 433 | */ |
||
| 434 | 7 | public function deleteOne($criteria = null) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Deletes document with the specified primary key. |
||
| 446 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 447 | * @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). |
||
| 448 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 449 | * @since v1.0 |
||
| 450 | */ |
||
| 451 | 2 | public function deleteByPk($pkValue, $criteria = null) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Deletes documents with the specified primary keys. |
||
| 468 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 469 | * @param mixed[] $pkValues Primary keys array |
||
| 470 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 471 | * @since v1.0 |
||
| 472 | */ |
||
| 473 | 2 | public function deleteAllByPk($pkValues, $criteria = null) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Deletes documents with the specified primary keys. |
||
| 489 | * |
||
| 490 | * **Does not raise beforeDelete event and does not emit signals** |
||
| 491 | * |
||
| 492 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 493 | * |
||
| 494 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 495 | * @since v1.0 |
||
| 496 | */ |
||
| 497 | 3 | public function deleteAll($criteria = null) |
|
| 506 | |||
| 507 | 70 | public function getCollection() |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Make status uniform |
||
| 514 | * @param bool|array $result |
||
| 515 | * @param bool $insert Set to true for inserts |
||
| 516 | * @return bool Return true if secceed |
||
| 517 | */ |
||
| 518 | 84 | private function _result($result, $insert = false) |
|
| 530 | |||
| 531 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Take care of EventBeforeSave |
||
| 535 | * @see EventBeforeSave |
||
| 536 | * @return boolean |
||
| 537 | */ |
||
| 538 | 82 | private function _beforeSave($model, $event = null) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Take care of EventAfterSave |
||
| 554 | * @see EventAfterSave |
||
| 555 | */ |
||
| 556 | 79 | private function _afterSave($model, $event = null) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * This method is invoked before deleting a record. |
||
| 569 | * The default implementation raises the {@link onBeforeDelete} event. |
||
| 570 | * You may override this method to do any preparation work for record deletion. |
||
| 571 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 572 | * @return boolean whether the record should be deleted. Defaults to true. |
||
| 573 | * @since v1.0 |
||
| 574 | */ |
||
| 575 | 10 | private function _beforeDelete() |
|
| 585 | |||
| 586 | /** |
||
| 587 | * This method is invoked after deleting a record. |
||
| 588 | * The default implementation raises the {@link onAfterDelete} event. |
||
| 589 | * You may override this method to do postprocessing after the record is deleted. |
||
| 590 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 591 | * @since v1.0 |
||
| 592 | */ |
||
| 593 | 7 | private function _afterDelete() |
|
| 598 | |||
| 599 | // </editor-fold> |
||
| 600 | } |
||
| 601 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.