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 Collection |
||
| 91 | */ |
||
| 92 | private $_collection = null; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Create entity manager |
||
| 96 | * @param AnnotatedInterface $model |
||
| 97 | * @throws ManganException |
||
| 98 | */ |
||
| 99 | public function __construct(AnnotatedInterface $model) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Create model related entity manager. |
||
| 117 | * This will create customized entity manger if defined in model with EntityManager annotation. |
||
| 118 | * If no custom entity manager is defined this will return default EntityManager. |
||
| 119 | * @param AnnotatedInterface $model |
||
| 120 | * @return EntityManagerInterface |
||
| 121 | */ |
||
| 122 | public static function create($model) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set attributes en masse. |
||
| 130 | * Attributes will be filtered according to SafeAnnotation. |
||
| 131 | * Only attributes marked as safe will be set, other will be ignored. |
||
| 132 | * |
||
| 133 | * @param mixed[] $atributes |
||
| 134 | */ |
||
| 135 | public function setAttributes($atributes) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Inserts a row into the table based on this active record attributes. |
||
| 142 | * If the table's primary key is auto-incremental and is null before insertion, |
||
| 143 | * it will be populated with the actual value after insertion. |
||
| 144 | * |
||
| 145 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 146 | * After the record is inserted to DB successfully, its {@link isNewRecord} property will be set false, |
||
| 147 | * and its {@link scenario} property will be set to be 'update'. |
||
| 148 | * |
||
| 149 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 150 | * |
||
| 151 | * @return boolean whether the attributes are valid and the record is inserted successfully. |
||
| 152 | * @throws MongoException if the record is not new |
||
| 153 | * @throws MongoException on fail of insert or insert of empty document |
||
| 154 | * @throws MongoException on fail of insert, when safe flag is set to true |
||
| 155 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 156 | * @since v1.0 |
||
| 157 | */ |
||
| 158 | public function insert(AnnotatedInterface $model = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Updates the row represented by this active document. |
||
| 179 | * All loaded attributes will be saved to the database. |
||
| 180 | * Note, validation is not performed in this method. You may call {@link validate} to perform the validation. |
||
| 181 | * |
||
| 182 | * @param array $attributes list of attributes that need to be saved. Defaults to null, |
||
| 183 | * meaning all attributes that are loaded from DB will be saved. |
||
| 184 | |||
| 185 | * @return boolean whether the update is successful |
||
| 186 | * @throws MongoException if the record is new |
||
| 187 | * @throws MongoException on fail of update |
||
| 188 | * @throws MongoException on timeout of db operation , when safe flag is set to true |
||
| 189 | * @since v1.0 |
||
| 190 | */ |
||
| 191 | public function update(array $attributes = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Atomic, in-place update method. |
||
| 234 | * |
||
| 235 | * @since v1.3.6 |
||
| 236 | * @param Modifier $modifier updating rules to apply |
||
| 237 | * @param CriteriaInterface $criteria condition to limit updating rules |
||
| 238 | * @return boolean |
||
| 239 | */ |
||
| 240 | public function updateAll(Modifier $modifier, CriteriaInterface $criteria = null) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Saves the current record. |
||
| 259 | * |
||
| 260 | * The record is inserted as a row into the database collection or updated if exists. |
||
| 261 | * |
||
| 262 | * Validation will be performed before saving the record. If the validation fails, |
||
| 263 | * the record will not be saved. You can call {@link getErrors()} to retrieve the |
||
| 264 | * validation errors. |
||
| 265 | * |
||
| 266 | * @param boolean $runValidation whether to perform validation before saving the record. |
||
| 267 | * If the validation fails, the record will not be saved to database. |
||
| 268 | * @param AnnotatedInterface $model if want to insert different model than set in constructor |
||
| 269 | * @return boolean whether the saving succeeds |
||
| 270 | * @since v1.0 |
||
| 271 | */ |
||
| 272 | public function save($runValidation = true, $model = null) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Reloads document from database. |
||
| 301 | * It return true if document is reloaded and false if it's no longer exists. |
||
| 302 | * |
||
| 303 | * @return boolean |
||
| 304 | */ |
||
| 305 | public function refresh() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Deletes the document from database. |
||
| 322 | * @return boolean whether the deletion is successful. |
||
| 323 | * @throws MongoException if the record is new |
||
| 324 | */ |
||
| 325 | public function delete() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Deletes one document with the specified primary keys. |
||
| 349 | * <b>Does not raise beforeDelete</b> |
||
| 350 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 351 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 352 | * @since v1.0 |
||
| 353 | */ |
||
| 354 | public function deleteOne($criteria = null) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Deletes document with the specified primary key. |
||
| 366 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 367 | * @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). |
||
| 368 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 369 | * @since v1.0 |
||
| 370 | */ |
||
| 371 | public function deleteByPk($pkValue, $criteria = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Deletes documents with the specified primary keys. |
||
| 388 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 389 | * @param mixed[] $pkValues Primary keys array |
||
| 390 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 391 | * @since v1.0 |
||
| 392 | */ |
||
| 393 | public function deleteAllByPk($pkValues, $criteria = null) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Deletes documents with the specified primary keys. |
||
| 409 | * <b>Does not raise beforeDelete</b> |
||
| 410 | * See {@link find()} for detailed explanation about $condition and $params. |
||
| 411 | * @param array|CriteriaInterface $criteria query criteria. |
||
| 412 | * @since v1.0 |
||
| 413 | */ |
||
| 414 | public function deleteAll($criteria = null) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * |
||
| 426 | * @return Collection |
||
| 427 | */ |
||
| 428 | public function getCollection() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Make status uniform |
||
| 435 | * @param bool|array $result |
||
| 436 | * @param bool $insert Set to true for inserts |
||
| 437 | * @return bool Return true if secceed |
||
| 438 | */ |
||
| 439 | private function _result($result, $insert = false) |
||
| 451 | |||
| 452 | // <editor-fold defaultstate="collapsed" desc="Event and Signal handling"> |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Take care of EventBeforeSave |
||
| 456 | * @see EventBeforeSave |
||
| 457 | * @return boolean |
||
| 458 | */ |
||
| 459 | private function _beforeSave($model, $event = null) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Take care of EventAfterSave |
||
| 475 | * @see EventAfterSave |
||
| 476 | */ |
||
| 477 | private function _afterSave($model, $event = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * This method is invoked before deleting a record. |
||
| 490 | * The default implementation raises the {@link onBeforeDelete} event. |
||
| 491 | * You may override this method to do any preparation work for record deletion. |
||
| 492 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 493 | * @return boolean whether the record should be deleted. Defaults to true. |
||
| 494 | * @since v1.0 |
||
| 495 | */ |
||
| 496 | private function _beforeDelete() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * This method is invoked after deleting a record. |
||
| 509 | * The default implementation raises the {@link onAfterDelete} event. |
||
| 510 | * You may override this method to do postprocessing after the record is deleted. |
||
| 511 | * Make sure you call the parent implementation so that the event is raised properly. |
||
| 512 | * @since v1.0 |
||
| 513 | */ |
||
| 514 | private function _afterDelete() |
||
| 519 | |||
| 520 | // </editor-fold> |
||
| 521 | } |
||
| 522 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.