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 |
||
19 | class MetadataFactory implements MetadataFactoryInterface |
||
20 | { |
||
21 | /** |
||
22 | * The Metadata driver. |
||
23 | * |
||
24 | * @var DriverInterface |
||
25 | */ |
||
26 | private $driver; |
||
27 | |||
28 | /** |
||
29 | * Entity utility. Used for formatting and validation. |
||
30 | * |
||
31 | * @var EntityUtility |
||
32 | */ |
||
33 | private $entityUtil; |
||
34 | |||
35 | /** |
||
36 | * The event dispatcher |
||
37 | * |
||
38 | * @var EventDispatcher |
||
39 | */ |
||
40 | private $dispatcher; |
||
41 | |||
42 | /** |
||
43 | * The Metadata cache instance. |
||
44 | * Is optional, if defined using the setter. |
||
45 | * |
||
46 | * @var CacheInterface |
||
47 | */ |
||
48 | private $cache; |
||
49 | |||
50 | /** |
||
51 | * Flags whether metadata caching is enabled. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $cacheEnabled = true; |
||
56 | |||
57 | /** |
||
58 | * In-memory loaded Metadata instances. |
||
59 | * |
||
60 | * @var EntityMetadata[] |
||
61 | */ |
||
62 | private $loaded; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | * |
||
67 | * @param DriverInterface $driver |
||
68 | * @param EntityUtility $entityUtil |
||
69 | * @param EventDispatcher $dispatcher |
||
70 | */ |
||
71 | public function __construct(DriverInterface $driver, EntityUtility $entityUtil, EventDispatcher $dispatcher) |
||
77 | |||
78 | /** |
||
79 | * Sets the cache instance to use for reading/writing Metadata objects. |
||
80 | * |
||
81 | * @param CacheInterface $cache |
||
82 | * @return self |
||
83 | */ |
||
84 | public function setCache(CacheInterface $cache) |
||
89 | |||
90 | /** |
||
91 | * Gets the cache instance. |
||
92 | * |
||
93 | * @return CacheInterface|null |
||
94 | */ |
||
95 | public function getCache() |
||
99 | |||
100 | /** |
||
101 | * Enables or disables the cache. |
||
102 | * |
||
103 | * @param bool $bit |
||
104 | * @return self |
||
105 | */ |
||
106 | public function enableCache($bit = true) |
||
111 | |||
112 | /** |
||
113 | * Determines if cache is enbled. |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | public function hasCache() |
||
121 | |||
122 | /** |
||
123 | * {@inheritDoc} |
||
124 | */ |
||
125 | public function getMetadataForType($type) |
||
169 | |||
170 | /** |
||
171 | * Dispatches a Metadata event |
||
172 | * |
||
173 | * @param EntityMetadata $metadata |
||
174 | */ |
||
175 | private function dispatchMetadataEvent(EntityMetadata $metadata) |
||
180 | |||
181 | /** |
||
182 | * {@inheritDoc} |
||
183 | */ |
||
184 | public function getAllTypeNames() |
||
188 | |||
189 | /** |
||
190 | * {@inheritDoc} |
||
191 | */ |
||
192 | public function getAllMetadata() |
||
200 | |||
201 | /** |
||
202 | * {@inheritDoc} |
||
203 | */ |
||
204 | public function metadataExists($type) |
||
212 | |||
213 | /** |
||
214 | * Determines if a type is direct child of another type. |
||
215 | * |
||
216 | * @param string $child |
||
217 | * @param string $parent |
||
218 | * @return bool |
||
219 | */ |
||
220 | public function isChildOf($child, $parent) |
||
228 | |||
229 | /** |
||
230 | * Determines if a type is an ancestor of another type. |
||
231 | * |
||
232 | * @param string $parent |
||
233 | * @param string $child |
||
234 | * @return bool |
||
235 | */ |
||
236 | public function isAncestorOf($parent, $child) |
||
240 | |||
241 | /** |
||
242 | * Determines if a type is a descendant of another type. |
||
243 | * |
||
244 | * @param string $child |
||
245 | * @param string $parent |
||
246 | * @return bool |
||
247 | */ |
||
248 | public function isDescendantOf($child, $parent) |
||
259 | |||
260 | public function validateResourceTypes($parentType, $childType) |
||
274 | |||
275 | /** |
||
276 | * Handles persistence specific metadata loading. |
||
277 | * |
||
278 | * @param EntityMetadata $metadata |
||
279 | * @return EntityMetadata |
||
280 | */ |
||
281 | private function loadPersistenceMetadata(EntityMetadata $metadata) |
||
290 | |||
291 | /** |
||
292 | * Handles search specific metadata loading. |
||
293 | * |
||
294 | * @param EntityMetadata $metadata |
||
295 | * @return EntityMetadata |
||
296 | */ |
||
297 | private function loadSearchMetadata(EntityMetadata $metadata) |
||
309 | |||
310 | /** |
||
311 | * Merges two sets of EntityMetadata. |
||
312 | * Is used for applying inheritance information. |
||
313 | * |
||
314 | * @param EntityMetadata &$metadata |
||
315 | * @param EntityMetadata $toAdd |
||
316 | */ |
||
317 | private function mergeMetadata(EntityMetadata &$metadata = null, EntityMetadata $toAdd) |
||
325 | |||
326 | /** |
||
327 | * Attempts to load a Metadata instance from a memory or cache source. |
||
328 | * |
||
329 | * @param string $type |
||
330 | * @return EntityMetadata|null |
||
331 | */ |
||
332 | private function doLoadMetadata($type) |
||
346 | |||
347 | /** |
||
348 | * Puts the Metadata instance into a cache source (if set) and memory. |
||
349 | * |
||
350 | * @param EntityMetadata $metadata |
||
351 | * @return self |
||
352 | */ |
||
353 | private function doPutMetadata(EntityMetadata $metadata) |
||
361 | |||
362 | /** |
||
363 | * Clears any loaded metadata objects from memory. |
||
364 | * |
||
365 | * @return self |
||
366 | */ |
||
367 | public function clearMemory() |
||
372 | |||
373 | /** |
||
374 | * Gets a Metadata instance for a type from memory. |
||
375 | * |
||
376 | * @return EntityMetadata|null |
||
377 | */ |
||
378 | private function getFromMemory($type) |
||
385 | |||
386 | /** |
||
387 | * Sets a Metadata instance to the memory cache. |
||
388 | * |
||
389 | * @param EntityMetadata $metadata |
||
390 | * @return self |
||
391 | */ |
||
392 | private function setToMemory(EntityMetadata $metadata) |
||
397 | |||
398 | /** |
||
399 | * Retrieves a Metadata instance for a type from cache. |
||
400 | * |
||
401 | * @return EntityMetadata|null |
||
402 | */ |
||
403 | private function getFromCache($type) |
||
410 | } |
||
411 |
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: