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 ConfigModelManager 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 ConfigModelManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ConfigModelManager |
||
| 22 | { |
||
| 23 | /** @deprecated since 1.9. Use ConfigModel::MODE_DEFAULT instead */ |
||
| 24 | const MODE_DEFAULT = ConfigModel::MODE_DEFAULT; |
||
| 25 | /** @deprecated since 1.9. Use ConfigModel::MODE_HIDDEN instead */ |
||
| 26 | const MODE_HIDDEN = ConfigModel::MODE_HIDDEN; |
||
| 27 | /** @deprecated since 1.9. Use ConfigModel::MODE_READONLY instead */ |
||
| 28 | const MODE_READONLY = ConfigModel::MODE_READONLY; |
||
| 29 | |||
| 30 | /** @var EntityConfigModel[] [{class name} => EntityConfigModel, ...] */ |
||
| 31 | private $entities; |
||
| 32 | |||
| 33 | /** @var bool */ |
||
| 34 | private $entitiesAreLoaded = false; |
||
| 35 | |||
| 36 | /** @var array [{class name} => [{field name} => FieldConfigModel, ...], ...] */ |
||
| 37 | private $fields = []; |
||
| 38 | |||
| 39 | /** @var bool */ |
||
| 40 | private $dbCheck; |
||
| 41 | |||
| 42 | /** @var ServiceLink */ |
||
| 43 | protected $emLink; |
||
| 44 | |||
| 45 | /** @var LockObject */ |
||
| 46 | protected $lockObject; |
||
| 47 | |||
| 48 | private $requiredTables = [ |
||
| 49 | 'oro_entity_config', |
||
| 50 | 'oro_entity_config_field', |
||
| 51 | 'oro_entity_config_index_value', |
||
| 52 | ]; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param ServiceLink $emLink A link to the EntityManager |
||
| 56 | * @param LockObject $lockObject |
||
| 57 | */ |
||
| 58 | public function __construct(ServiceLink $emLink, LockObject $lockObject) |
||
| 59 | { |
||
| 60 | $this->emLink = $emLink; |
||
| 61 | $this->lockObject = $lockObject; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @return EntityManager |
||
| 66 | */ |
||
| 67 | public function getEntityManager() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return bool |
||
| 74 | */ |
||
| 75 | public function checkDatabase() |
||
| 76 | { |
||
| 77 | if (null !== $this->dbCheck) { |
||
| 78 | return $this->dbCheck; |
||
| 79 | } |
||
| 80 | if ($this->lockObject->isLocked()) { |
||
| 81 | return true; |
||
| 82 | } |
||
| 83 | |||
| 84 | $this->dbCheck = SafeDatabaseChecker::tablesExist( |
||
| 85 | $this->getEntityManager()->getConnection(), |
||
| 86 | $this->requiredTables |
||
| 87 | ); |
||
| 88 | |||
| 89 | return $this->dbCheck; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function clearCheckDatabase() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Finds a model for an entity |
||
| 99 | * |
||
| 100 | * @param string $className |
||
| 101 | * |
||
| 102 | * @return EntityConfigModel|null An instance of EntityConfigModel or null if a model was not found |
||
| 103 | */ |
||
| 104 | public function findEntityModel($className) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Finds a model for an entity field |
||
| 144 | * |
||
| 145 | * @param string $className |
||
| 146 | * @param string $fieldName |
||
| 147 | * |
||
| 148 | * @return FieldConfigModel|null An instance of FieldConfigModel or null if a model was not found |
||
| 149 | */ |
||
| 150 | public function findFieldModel($className, $fieldName) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $className |
||
| 177 | * |
||
| 178 | * @return EntityConfigModel |
||
| 179 | * |
||
| 180 | * @throws \InvalidArgumentException if $className is empty |
||
| 181 | * @throws RuntimeException if a model was not found |
||
| 182 | */ |
||
| 183 | public function getEntityModel($className) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param string $className |
||
| 205 | * @param string $fieldName |
||
| 206 | * |
||
| 207 | * @return FieldConfigModel |
||
| 208 | * |
||
| 209 | * @throws \InvalidArgumentException if $className or $fieldName is empty |
||
| 210 | * @throws RuntimeException if a model was not found |
||
| 211 | */ |
||
| 212 | public function getFieldModel($className, $fieldName) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Renames a field |
||
| 238 | * Important: this method do not save changes in a database. To do this you need to call entityManager->flush |
||
| 239 | * |
||
| 240 | * @param string $className |
||
| 241 | * @param string $fieldName |
||
| 242 | * @param string $newFieldName |
||
| 243 | * |
||
| 244 | * @return bool TRUE if the name was changed; otherwise, FALSE |
||
| 245 | * |
||
| 246 | * @throws \InvalidArgumentException if $className, $fieldName or $newFieldName is empty |
||
| 247 | * @throws RuntimeException if models are locked |
||
| 248 | */ |
||
| 249 | public function changeFieldName($className, $fieldName, $newFieldName) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Changes a type of a field |
||
| 286 | * Important: this method do not save changes in a database. To do this you need to call entityManager->flush |
||
| 287 | * |
||
| 288 | * @param string $className |
||
| 289 | * @param string $fieldName |
||
| 290 | * @param string $fieldType |
||
| 291 | * |
||
| 292 | * @return bool TRUE if the type was changed; otherwise, FALSE |
||
| 293 | * |
||
| 294 | * @throws \InvalidArgumentException if $className, $fieldName or $fieldType is empty |
||
| 295 | * @throws RuntimeException if models are locked |
||
| 296 | */ |
||
| 297 | View Code Duplication | public function changeFieldType($className, $fieldName, $fieldType) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * Changes a mode of a field |
||
| 333 | * Important: this method do not save changes in a database. To do this you need to call entityManager->flush |
||
| 334 | * |
||
| 335 | * @param string $className |
||
| 336 | * @param string $fieldName |
||
| 337 | * @param string $mode Can be the value of one of ConfigModel::MODE_* constants |
||
| 338 | * |
||
| 339 | * @return bool TRUE if the mode was changed; otherwise, FALSE |
||
| 340 | * |
||
| 341 | * @throws \InvalidArgumentException if $className, $fieldName or $mode is empty |
||
| 342 | * @throws RuntimeException if models are locked |
||
| 343 | */ |
||
| 344 | View Code Duplication | public function changeFieldMode($className, $fieldName, $mode) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Changes a mode of an entity |
||
| 380 | * Important: this method do not save changes in a database. To do this you need to call entityManager->flush |
||
| 381 | * |
||
| 382 | * @param string $className |
||
| 383 | * @param string $mode Can be the value of one of ConfigModel::MODE_* constants |
||
| 384 | * |
||
| 385 | * @return bool TRUE if the type was changed; otherwise, FALSE |
||
| 386 | * |
||
| 387 | * @throws \InvalidArgumentException if $className or $mode is empty |
||
| 388 | * @throws RuntimeException if models are locked |
||
| 389 | */ |
||
| 390 | public function changeEntityMode($className, $mode) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param string|null $className |
||
| 422 | * |
||
| 423 | * @return ConfigModel[] |
||
| 424 | */ |
||
| 425 | public function getModels($className = null) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string|null $className |
||
| 450 | * @param string|null $mode |
||
| 451 | * |
||
| 452 | * @return EntityConfigModel |
||
| 453 | * |
||
| 454 | * @throws \InvalidArgumentException if $mode is invalid |
||
| 455 | * @throws RuntimeException if models are locked |
||
| 456 | */ |
||
| 457 | public function createEntityModel($className = null, $mode = ConfigModel::MODE_DEFAULT) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $className |
||
| 484 | * @param string $fieldName |
||
| 485 | * @param string $fieldType |
||
| 486 | * @param string $mode |
||
| 487 | * |
||
| 488 | * @return FieldConfigModel |
||
| 489 | * |
||
| 490 | * @throws \InvalidArgumentException if $className is empty or $mode is invalid |
||
| 491 | * @throws RuntimeException if models are locked |
||
| 492 | */ |
||
| 493 | public function createFieldModel($className, $fieldName, $fieldType, $mode = ConfigModel::MODE_DEFAULT) |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Removes all cached data |
||
| 527 | */ |
||
| 528 | public function clearCache() |
||
| 538 | |||
| 539 | /** |
||
| 540 | * Makes sure that an entity model for the given class is loaded |
||
| 541 | * or, if the class name is not specified, make sure that all entity models are loaded |
||
| 542 | * |
||
| 543 | * @param string|null $className |
||
| 544 | */ |
||
| 545 | protected function ensureEntityCacheWarmed($className = null) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Checks $this->fields[$className] and if it is empty loads all fields models at once |
||
| 571 | * |
||
| 572 | * @param string $className |
||
| 573 | */ |
||
| 574 | protected function ensureFieldCacheWarmed($className) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * @return EntityConfigModel[] |
||
| 591 | */ |
||
| 592 | protected function loadEntityModels() |
||
| 615 | |||
| 616 | /** |
||
| 617 | * @param string $className |
||
| 618 | * |
||
| 619 | * @return EntityConfigModel|null |
||
| 620 | */ |
||
| 621 | protected function loadEntityModel($className) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @param string $mode |
||
| 630 | * |
||
| 631 | * @return bool |
||
| 632 | */ |
||
| 633 | protected function isValidMode($mode) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Determines whether all entities in local cache are detached from an entity manager or not |
||
| 644 | */ |
||
| 645 | protected function areAllEntitiesDetached() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Determines whether the given entity is managed by an entity manager or not |
||
| 663 | * |
||
| 664 | * @param object $entity |
||
| 665 | * |
||
| 666 | * @return bool |
||
| 667 | */ |
||
| 668 | protected function isEntityDetached($entity) |
||
| 676 | } |
||
| 677 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: