Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ElasticUnitOfWork 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 ElasticUnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ElasticUnitOfWork { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Entity has been persisted and now is managed by EntityManager |
||
| 26 | */ |
||
| 27 | const STATE_MANAGED = 1; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Entity has been instantiated, and is not managed by EntityManager (yet) |
||
| 31 | */ |
||
| 32 | const STATE_NEW = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Entity has value(s) for identity field(s) and exists in elastic, therefore, |
||
| 36 | * is not managed by EntityManager (yet) |
||
| 37 | */ |
||
| 38 | const STATE_DETACHED = 3; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Entity was deleted by remove method, and will be removed on commit |
||
| 42 | */ |
||
| 43 | const STATE_DELETED = 4; |
||
| 44 | |||
| 45 | /* @var ElasticEntityManager $em */ |
||
| 46 | private $em; |
||
| 47 | |||
| 48 | /* @var EntityManagerInterface $em */ |
||
| 49 | private $elastic; |
||
| 50 | |||
| 51 | /** @var string[] */ |
||
| 52 | private $entitiesCommitOrder = []; |
||
| 53 | |||
| 54 | protected $entityDeletions; |
||
| 55 | protected $entityInsertions; |
||
| 56 | protected $entityUpdates; |
||
| 57 | |||
| 58 | private $hydrator; |
||
| 59 | |||
| 60 | public function __construct(EntityManagerInterface $em, Client $elastic) { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $entityName |
||
| 68 | * @return ElasticEntityPersister |
||
| 69 | */ |
||
| 70 | public function getEntityPersister($entityName) { |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param object $entity |
||
| 76 | */ |
||
| 77 | public function persist($entity) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param object $entity |
||
| 101 | * @return int |
||
| 102 | */ |
||
| 103 | public function getEntityState($entity) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param object $entity |
||
| 133 | */ |
||
| 134 | public function scheduleForInsert($entity) { |
||
| 149 | |||
| 150 | public function isScheduledForInsert($entity) { |
||
| 153 | |||
| 154 | public function isScheduledForUpdate($entity) { |
||
| 157 | |||
| 158 | public function isScheduledForDelete($entity) { |
||
| 161 | |||
| 162 | public function isEntityScheduled($entity) { |
||
| 169 | |||
| 170 | public function scheduleForUpdate($entity) { |
||
| 181 | |||
| 182 | public function scheduleForDelete($entity) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param null|object|array $entity |
||
| 200 | * @return void |
||
| 201 | * @throws \Exception |
||
| 202 | */ |
||
| 203 | public function commit($entity = null) { |
||
| 243 | |||
| 244 | public function executeInserts($className) { |
||
| 258 | |||
| 259 | View Code Duplication | public function executeUpdates($className) { |
|
| 271 | |||
| 272 | View Code Duplication | public function executeDeletions($className) { |
|
| 284 | |||
| 285 | protected function dispatchOnFlushEvent() { |
||
| 288 | |||
| 289 | protected function dispatchPostFlushEvent() { |
||
| 292 | |||
| 293 | public function clear($entity = null) { |
||
| 311 | |||
| 312 | View Code Duplication | private function clearEntityInsertions($entity = null) { |
|
| 323 | |||
| 324 | View Code Duplication | private function clearEntityUpdate($entity = null) { |
|
| 335 | |||
| 336 | public function delete($entity) { |
||
| 344 | |||
| 345 | View Code Duplication | private function clearEntityDeletions($entity = null) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @return string[] |
||
| 359 | */ |
||
| 360 | public function getEntitiesCommitOrder() { |
||
| 363 | |||
| 364 | protected function afterTransactionRolledBack() { |
||
| 367 | |||
| 368 | protected function afterTransactionComplete() { |
||
| 371 | |||
| 372 | public function createEntity() { |
||
| 375 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.