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) { |
|
131 | 1 | if (is_null($realClass)) { |
|
132 | 1 | $realClass = (new \ReflectionClass(static::class))->getNamespaceName() . '\\' . ucfirst($entityType); |
|
133 | } |
||
134 | 1 | if (!class_exists($realClass)) { |
|
135 | 1 | throw new \Exception('Class ' . $realClass . ' not found.'); |
|
136 | } |
||
137 | 1 | if (is_null($proxyClass)) { |
|
138 | 1 | $proxyClass = (new \ReflectionClass($realClass))->getNamespaceName() . '\\Proxy\\' . ucfirst($entityType) . 'Proxy'; |
|
139 | } |
||
140 | 1 | if (!class_exists($proxyClass)) { |
|
141 | throw new \Exception('Class ' . $proxyClass . 'not found.'); |
||
142 | } |
||
143 | 1 | $this->classes[$entityType] = array( |
|
144 | 1 | 'real' => $realClass, |
|
145 | 1 | 'proxy' => $proxyClass |
|
146 | ); |
||
147 | 1 | return $this->classes[$entityType]; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 1 | public function getPropertyMapDefinition($entityType) { |
|
154 | 1 | if (!isset($this->propertyMapDefinitions[$entityType])) { |
|
155 | 1 | $propertyMapDefinition = new \ReflectionMethod($this->getClass($entityType), 'getPropertyMapDefinition'); |
|
156 | 1 | $this->propertyMapDefinitions[$entityType] = $propertyMapDefinition->invoke(NULL); |
|
157 | } |
||
158 | 1 | return $this->propertyMapDefinitions[$entityType]; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | 1 | public function getClass($entityType, $type = 'real') { |
|
165 | 1 | if (!isset($this->classes[$entityType][$type])) { |
|
166 | throw new \Exception(ucfirst($type) . ' class for ' . $entityType . ' not registered.'); |
||
167 | } |
||
168 | 1 | return $this->classes[$entityType][$type]; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 1 | public function mapProperties(\stdClass $values, $entityType) { |
|
175 | 1 | $mapped = new \stdClass(); |
|
176 | 1 | foreach ($this->getPropertyMapDefinition($entityType)->getPropertyMaps() as $map) { |
|
177 | // The property on the destination must exist or it will not map. |
||
178 | 1 | $mapped->{$map->getDestination()->getName()} = NULL; |
|
179 | 1 | if (!isset($values->{$map->getSource()->getName()})) { |
|
180 | // If a source property is not set, we must unset it on the destination |
||
181 | // later on, so give it a value we can recognize. |
||
182 | 1 | $values->{$map->getSource()->getName()} = static::EMPTYPROPERTYPLACEHOLDER; |
|
183 | } |
||
184 | } |
||
185 | 1 | return $this->sanitizeObject($this->propertyMapper->process($values, $mapped, $this->getPropertyMap($entityType))); |
|
186 | } |
||
187 | |||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 1 | public function reverseMapProperties(\stdClass $values, $entityType) { |
|
193 | 1 | $reversed = new \stdClass(); |
|
194 | 1 | foreach ($this->getPropertyMapDefinition($entityType)->getPropertyMaps() as $map) { |
|
195 | 1 | if (!isset($reversed->{$map->getSource()->getName()})) { |
|
196 | 1 | $reversed->{$map->getSource()->getName()} = static::EMPTYPROPERTYPLACEHOLDER; |
|
197 | } |
||
198 | 1 | if (!isset($values->{$map->getDestination()->getName()})) { |
|
199 | 1 | $values->{$map->getDestination()->getName()} = static::EMPTYPROPERTYPLACEHOLDER; |
|
200 | } |
||
201 | } |
||
202 | 1 | return $this->sanitizeObject($this->propertyMapper->reverse($reversed, $values, $this->getPropertyMap($entityType))); |
|
203 | } |
||
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | 1 | public function isFullObject(\stdClass $object, $entityType) { |
|
209 | 1 | $reflection = new \ReflectionClass($this->getClass($entityType)); |
|
210 | 1 | $defaultProperties = $reflection->getDefaultProperties(); |
|
211 | $properties = array_flip(array_filter(array_keys($defaultProperties), function($prop) use ($reflection) { |
||
212 | // Filter out static properties. |
||
213 | 1 | return !$reflection->getProperty($prop)->isStatic(); |
|
214 | 1 | })); |
|
215 | 1 | return count(array_intersect_key($properties, (array) $object)) === count($properties); |
|
216 | } |
||
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) { |
|
290 | 1 | $arr = (array) $object; |
|
291 | 1 | foreach ($arr as $prop => $val) { |
|
292 | 1 | if ($this->isEmptyValue($val)) { |
|
293 | 1 | unset($arr[$prop]); |
|
294 | } |
||
295 | } |
||
296 | 1 | return (object) $arr; |
|
297 | } |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | 1 | public function isEmptyValue($value) { |
|
303 | 1 | return $value === static::EMPTYPROPERTYPLACEHOLDER; |
|
304 | } |
||
305 | |||
306 | /** |
||
307 | * {@inheritdoc} |
||
308 | */ |
||
309 | public function sanitizeValues(\stdClass &$values, $entityType) { |
||
314 | } |
||
315 |