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 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 |
||
| 20 | class EntityManager implements EntityManagerInterface { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The factory container. |
||
| 24 | * |
||
| 25 | * @var \TheSportsDb\Entity\Factory\FactoryContainerInterface |
||
| 26 | */ |
||
| 27 | protected $factoryContainer; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The repository container. |
||
| 31 | * |
||
| 32 | * @var \TheSportsDb\Entity\Repository\RepositoryContainerInterface |
||
| 33 | */ |
||
| 34 | protected $repositoryContainer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Map entity types to classes. |
||
| 38 | * |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $classes = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Property map definitions. |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $propertyMapDefinitions = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Property map definitions. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $propertyMaps = array(); |
||
| 56 | |||
| 57 | protected $propertyMapper; |
||
| 58 | |||
| 59 | |||
| 60 | const EMPTYPROPERTYPLACEHOLDER = '__EMPTY_PROPERTY_PLACEHOLDER__'; |
||
| 61 | |||
| 62 | |||
| 63 | public function __construct(MapperInterface $propertyMapper, FactoryContainerInterface $factoryContainer = NULL, RepositoryContainerInterface $repositoryContainer = NULL) { |
||
| 72 | |||
| 73 | public function setFactoryContainer(FactoryContainerInterface $factoryContainer) { |
||
| 79 | |||
| 80 | public function setRepositoryContainer(RepositoryContainerInterface $repositoryContainer) { |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * {@inheritdoc} |
||
| 90 | */ |
||
| 91 | public function repository($entityType) { |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function factory($entityType) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function registerClass($entityType, $realClass = NULL, $proxyClass = NULL) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | public function getPropertyMapDefinition($entityType) { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function getClass($entityType, $type = 'real') { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function mapProperties(\stdClass $values, $entityType) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * {@inheritdoc} |
||
| 169 | */ |
||
| 170 | public function reverseMapProperties(\stdClass $values, $entityType) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function isFullObject(\stdClass $object, $entityType) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Initializes the property map. |
||
| 202 | * @param string $entityType |
||
| 203 | */ |
||
| 204 | protected function initPropertyMap($entityType) { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Gets the property map. |
||
| 240 | * |
||
| 241 | * @param string $entityType |
||
| 242 | * @return \FastNorth\PropertyMapper\MapInterface |
||
| 243 | * The property map. |
||
| 244 | */ |
||
| 245 | protected function getPropertyMap($entityType) { |
||
| 251 | |||
| 252 | protected function sanitizeObject(\stdClass $object) { |
||
| 261 | |||
| 262 | |||
| 263 | public function isEmptyValue($value) { |
||
| 266 | } |
||
| 267 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.