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 | /** |
||
58 | * The property mapper. |
||
59 | * |
||
60 | * @var \FastNorth\PropertyMapper\MapperInterface |
||
61 | */ |
||
62 | protected $propertyMapper; |
||
63 | |||
64 | |||
65 | /** |
||
66 | * Placeholder for empty properties. |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | const EMPTYPROPERTYPLACEHOLDER = '__EMPTY_PROPERTY_PLACEHOLDER__'; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function __construct(MapperInterface $propertyMapper, FactoryContainerInterface $factoryContainer = NULL, RepositoryContainerInterface $repositoryContainer = NULL) { |
||
85 | |||
86 | /** |
||
87 | * {@inheritdoc} |
||
88 | */ |
||
89 | 1 | public function setFactoryContainer(FactoryContainerInterface $factoryContainer) { |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 1 | public function setRepositoryContainer(RepositoryContainerInterface $repositoryContainer) { |
|
105 | |||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | 1 | public function repository($entityType) { |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 1 | public function factory($entityType) { |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 1 | public function registerClass($entityType, $realClass = NULL, $proxyClass = NULL) { |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function getPropertyMapDefinition($entityType) { |
|
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 1 | public function getClass($entityType, $type = 'real') { |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 1 | public function mapProperties(\stdClass $values, $entityType) { |
|
187 | |||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 1 | public function reverseMapProperties(\stdClass $values, $entityType) { |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | 1 | public function isFullObject(\stdClass $object, $entityType) { |
|
217 | |||
218 | /** |
||
219 | * Initializes the property map. |
||
220 | * |
||
221 | * @param string $entityType |
||
222 | * The entity type to initialize the property map for. |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | protected function initPropertyMap($entityType) { |
||
263 | |||
264 | /** |
||
265 | * Gets the property map. |
||
266 | * |
||
267 | * @param string $entityType |
||
268 | * The entity type to get the map for. |
||
269 | * |
||
270 | * @return \FastNorth\PropertyMapper\MapInterface |
||
271 | * The property map. |
||
272 | */ |
||
273 | protected function getPropertyMap($entityType) { |
||
279 | |||
280 | /** |
||
281 | * Sanitize empty values from an object. |
||
282 | * |
||
283 | * @param \stdClass $object |
||
284 | * The object to sanitize. |
||
285 | * |
||
286 | * @return \stdClass |
||
287 | * The sanitized object. |
||
288 | */ |
||
289 | 1 | protected function sanitizeObject(\stdClass $object) { |
|
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | 1 | public function isEmptyValue($value) { |
|
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function sanitizeValues(\stdClass &$values, $entityType) { |
||
314 | } |
||
315 |