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 |
||
21 | class EntityManager implements EntityManagerInterface { |
||
22 | |||
23 | /** |
||
24 | * The factory container. |
||
25 | * |
||
26 | * @var \TheSportsDb\Entity\Factory\FactoryContainerInterface |
||
27 | */ |
||
28 | protected $factoryContainer; |
||
29 | |||
30 | /** |
||
31 | * The repository container. |
||
32 | * |
||
33 | * @var \TheSportsDb\Entity\Repository\RepositoryContainerInterface |
||
34 | */ |
||
35 | protected $repositoryContainer; |
||
36 | |||
37 | /** |
||
38 | * Map entity types to classes. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $classes = array(); |
||
43 | |||
44 | /** |
||
45 | * Property map definitions. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $propertyMapDefinitions = array(); |
||
50 | |||
51 | /** |
||
52 | * Property map definitions. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $propertyMaps = array(); |
||
57 | |||
58 | /** |
||
59 | * The property mapper. |
||
60 | * |
||
61 | * @var \FastNorth\PropertyMapper\MapperInterface |
||
62 | */ |
||
63 | protected $propertyMapper; |
||
64 | |||
65 | |||
66 | /** |
||
67 | * Placeholder for empty properties. |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | const EMPTYPROPERTYPLACEHOLDER = '__EMPTY_PROPERTY_PLACEHOLDER__'; |
||
72 | |||
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | public function __construct(MapperInterface $propertyMapper, FactoryContainerInterface $factoryContainer = NULL, RepositoryContainerInterface $repositoryContainer = NULL) { |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | public function setFactoryContainer(FactoryContainerInterface $factoryContainer) { |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function setRepositoryContainer(RepositoryContainerInterface $repositoryContainer) { |
||
106 | |||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function repository($entityType) { |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function factory($entityType) { |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function registerClass($entityType, $realClass = NULL, $proxyClass = NULL) { |
||
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | public function getPropertyMapDefinition($entityType) { |
||
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function getClass($entityType, $type = 'real') { |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | public function mapProperties(\stdClass $values, $entityType) { |
||
185 | |||
186 | |||
187 | /** |
||
188 | * {@inheritdoc} |
||
189 | */ |
||
190 | public function reverseMapProperties(\stdClass $values, $entityType) { |
||
202 | |||
203 | /** |
||
204 | * {@inheritdoc} |
||
205 | */ |
||
206 | public function isFullObject(\stdClass $object, $entityType) { |
||
215 | |||
216 | /** |
||
217 | * Initializes the property map. |
||
218 | * |
||
219 | * @param string $entityType |
||
220 | * The entity type to initialize the property map for. |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | protected function initPropertyMap($entityType) { |
||
261 | |||
262 | /** |
||
263 | * Gets the property map. |
||
264 | * |
||
265 | * @param string $entityType |
||
266 | * The entity type to get the map for. |
||
267 | * |
||
268 | * @return \FastNorth\PropertyMapper\MapInterface |
||
269 | * The property map. |
||
270 | */ |
||
271 | protected function getPropertyMap($entityType) { |
||
277 | |||
278 | /** |
||
279 | * Sanitize empty values from an object. |
||
280 | * |
||
281 | * @param \stdClass $object |
||
282 | * The object to sanitize. |
||
283 | * |
||
284 | * @return \stdClass |
||
285 | * The sanitized object. |
||
286 | */ |
||
287 | protected function sanitizeObject(\stdClass $object) { |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function isEmptyValue($value) { |
||
303 | |||
304 | /** |
||
305 | * {@inheritdoc} |
||
306 | */ |
||
307 | public function sanitizeValues(\stdClass &$values, $entityType) { |
||
312 | |||
313 | /** |
||
314 | * Helper function for sanitizeValues. |
||
315 | * |
||
316 | * @param \stdClass $object |
||
317 | * The object of which to sanitize the property. |
||
318 | * @param PropertyDefinition $property |
||
319 | * The property definition of the property to sanitize. |
||
320 | * |
||
321 | * @return void |
||
322 | */ |
||
323 | protected function sanitizeProperty(\stdClass &$object, PropertyDefinition $property) { |
||
339 | } |
||
340 |