Complex classes like MetadataFactory 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 MetadataFactory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class MetadataFactory implements MetadataFactoryInterface |
||
19 | { |
||
20 | /** |
||
21 | * The Metadata driver. |
||
22 | * |
||
23 | * @var DriverInterface |
||
24 | */ |
||
25 | private $driver; |
||
26 | |||
27 | /** |
||
28 | * Entity utility. Used for formatting and validation. |
||
29 | * |
||
30 | * @var EntityUtility |
||
31 | */ |
||
32 | private $entityUtil; |
||
33 | |||
34 | /** |
||
35 | * The Metadata cache instance. |
||
36 | * Is optional, if defined using the setter. |
||
37 | * |
||
38 | * @var CacheInterface |
||
39 | */ |
||
40 | private $cache; |
||
41 | |||
42 | /** |
||
43 | * Flags whether metadata caching is enabled. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | private $cacheEnabled = true; |
||
48 | |||
49 | /** |
||
50 | * In-memory loaded Metadata instances. |
||
51 | * |
||
52 | * @var EntityMetadata[] |
||
53 | */ |
||
54 | private $loaded; |
||
55 | |||
56 | /** |
||
57 | * Constructor. |
||
58 | * |
||
59 | * @param DriverInterface $driver |
||
60 | */ |
||
61 | public function __construct(DriverInterface $driver, EntityUtility $entityUtil) |
||
66 | |||
67 | /** |
||
68 | * Sets the cache instance to use for reading/writing Metadata objects. |
||
69 | * |
||
70 | * @param CacheInterface $cache |
||
71 | * @return self |
||
72 | */ |
||
73 | public function setCache(CacheInterface $cache) |
||
78 | |||
79 | /** |
||
80 | * Gets the cache instance. |
||
81 | * |
||
82 | * @return CacheInterface|null |
||
83 | */ |
||
84 | public function getCache() |
||
88 | |||
89 | /** |
||
90 | * Enables or disables the cache. |
||
91 | * |
||
92 | * @param bool $bit |
||
93 | * @return self |
||
94 | */ |
||
95 | public function enableCache($bit = true) |
||
100 | |||
101 | /** |
||
102 | * Determines if cache is enbled. |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function hasCache() |
||
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | public function getMetadataForType($type) |
||
157 | |||
158 | /** |
||
159 | * {@inheritDoc} |
||
160 | */ |
||
161 | public function getAllTypeNames() |
||
165 | |||
166 | /** |
||
167 | * {@inheritDoc} |
||
168 | */ |
||
169 | public function getAllMetadata() |
||
177 | |||
178 | /** |
||
179 | * {@inheritDoc} |
||
180 | */ |
||
181 | public function metadataExists($type) |
||
189 | |||
190 | /** |
||
191 | * Determines if a type is direct child of another type. |
||
192 | * |
||
193 | * @param string $child |
||
194 | * @param string $parent |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function isChildOf($child, $parent) |
||
205 | |||
206 | /** |
||
207 | * Determines if a type is an ancestor of another type. |
||
208 | * |
||
209 | * @param string $parent |
||
210 | * @param string $child |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function isAncestorOf($parent, $child) |
||
217 | |||
218 | /** |
||
219 | * Determines if a type is a descendant of another type. |
||
220 | * |
||
221 | * @param string $child |
||
222 | * @param string $parent |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isDescendantOf($child, $parent) |
||
236 | |||
237 | public function validateResourceTypes($parentType, $childType) |
||
251 | |||
252 | /** |
||
253 | * Handles persistence specific metadata loading. |
||
254 | * |
||
255 | * @param EntityMetadata $metadata |
||
256 | * @return EntityMetadata |
||
257 | */ |
||
258 | private function loadPersistenceMetadata(EntityMetadata $metadata) |
||
267 | |||
268 | /** |
||
269 | * Handles search specific metadata loading. |
||
270 | * |
||
271 | * @param EntityMetadata $metadata |
||
272 | * @return EntityMetadata |
||
273 | */ |
||
274 | private function loadSearchMetadata(EntityMetadata $metadata) |
||
286 | |||
287 | /** |
||
288 | * Merges two sets of EntityMetadata. |
||
289 | * Is used for applying inheritance information. |
||
290 | * |
||
291 | * @param EntityMetadata &$metadata |
||
292 | * @param EntityMetadata $toAdd |
||
293 | */ |
||
294 | private function mergeMetadata(EntityMetadata &$metadata = null, EntityMetadata $toAdd) |
||
302 | |||
303 | /** |
||
304 | * Attempts to load a Metadata instance from a memory or cache source. |
||
305 | * |
||
306 | * @param string $type |
||
307 | * @return EntityMetadata|null |
||
308 | */ |
||
309 | private function doLoadMetadata($type) |
||
323 | |||
324 | /** |
||
325 | * Puts the Metadata instance into a cache source (if set) and memory. |
||
326 | * |
||
327 | * @param EntityMetadata $metadata |
||
328 | * @return self |
||
329 | */ |
||
330 | private function doPutMetadata(EntityMetadata $metadata) |
||
338 | |||
339 | /** |
||
340 | * Clears any loaded metadata objects from memory. |
||
341 | * |
||
342 | * @return self |
||
343 | */ |
||
344 | public function clearMemory() |
||
349 | |||
350 | /** |
||
351 | * Gets a Metadata instance for a type from memory. |
||
352 | * |
||
353 | * @return EntityMetadata|null |
||
354 | */ |
||
355 | private function getFromMemory($type) |
||
362 | |||
363 | /** |
||
364 | * Sets a Metadata instance to the memory cache. |
||
365 | * |
||
366 | * @param EntityMetadata $metadata |
||
367 | * @return self |
||
368 | */ |
||
369 | private function setToMemory(EntityMetadata $metadata) |
||
374 | |||
375 | /** |
||
376 | * Retrieves a Metadata instance for a type from cache. |
||
377 | * |
||
378 | * @return EntityMetadata|null |
||
379 | */ |
||
380 | private function getFromCache($type) |
||
387 | } |
||
388 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: