Complex classes like SimpleMetadataProvider 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 SimpleMetadataProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class SimpleMetadataProvider implements IMetadataProvider |
||
11 | { |
||
12 | protected $resourceSets = array(); |
||
13 | protected $resourceTypes = array(); |
||
14 | protected $associationSets = array(); |
||
15 | protected $containerName; |
||
16 | protected $namespaceName; |
||
17 | public $mappedDetails = null; |
||
18 | |||
19 | //Begin Implementation of IMetadataProvider |
||
20 | /** |
||
21 | * get the Container name for the data source. |
||
22 | * |
||
23 | * @return string container name |
||
24 | */ |
||
25 | public function getContainerName() |
||
29 | |||
30 | /** |
||
31 | * get Namespace name for the data source. |
||
32 | * |
||
33 | * @return string namespace |
||
34 | */ |
||
35 | public function getContainerNamespace() |
||
39 | |||
40 | /** |
||
41 | * get all entity set information. |
||
42 | * |
||
43 | * @return ResourceSet[] |
||
44 | */ |
||
45 | public function getResourceSets($params = null) |
||
46 | { |
||
47 | $parameters = []; |
||
48 | if (is_string($params)) { |
||
49 | $parameters[] = $params; |
||
50 | } elseif (isset($params) && !is_array($params)) { |
||
51 | throw new \ErrorException('Input parameter must be absent, null, string or array'); |
||
52 | } else { |
||
53 | $parameters = $params; |
||
54 | } |
||
55 | if (!is_array($parameters) || 0 == count($parameters)) { |
||
56 | return array_values($this->resourceSets); |
||
57 | } |
||
58 | assert(is_array($parameters)); |
||
59 | $return = []; |
||
60 | $counter = 0; |
||
61 | foreach ($this->resourceSets as $resource) { |
||
62 | $resName = $resource->getName(); |
||
63 | if (in_array($resName, $parameters)) { |
||
64 | $return[] = $resource; |
||
65 | $counter++; |
||
66 | } |
||
67 | } |
||
68 | assert($counter == count($return)); |
||
69 | return $return; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * get all resource types in the data source. |
||
74 | * |
||
75 | * @return ResourceType[] |
||
76 | */ |
||
77 | public function getTypes() |
||
81 | |||
82 | /** |
||
83 | * get a resource set based on the specified resource set name. |
||
84 | * |
||
85 | * @param string $name Name of the resource set |
||
86 | * |
||
87 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
88 | */ |
||
89 | public function resolveResourceSet($name) |
||
97 | |||
98 | /** |
||
99 | * get a resource type based on the resource set name. |
||
100 | * |
||
101 | * @param string $name Name of the resource set |
||
102 | * |
||
103 | * @return ResourceType|null resource type with the given resource set name if found else NULL |
||
104 | */ |
||
105 | public function resolveResourceType($name) |
||
113 | |||
114 | /** |
||
115 | * The method must return a collection of all the types derived from |
||
116 | * $resourceType The collection returned should NOT include the type |
||
117 | * passed in as a parameter. |
||
118 | * |
||
119 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
120 | * |
||
121 | * @return ResourceType[] |
||
122 | */ |
||
123 | public function getDerivedTypes(ResourceType $resourceType) |
||
127 | |||
128 | /** |
||
129 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
130 | * |
||
131 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
132 | */ |
||
133 | public function hasDerivedTypes(ResourceType $resourceType) |
||
137 | |||
138 | /** |
||
139 | * Gets the ResourceAssociationSet instance for the given source |
||
140 | * association end. |
||
141 | * |
||
142 | * @param ResourceSet $sourceResourceSet Resource set |
||
143 | * of the source |
||
144 | * association end |
||
145 | * @param ResourceType $sourceResourceType Resource type of the source |
||
146 | * association end |
||
147 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
148 | * the source |
||
149 | * association end |
||
150 | * |
||
151 | * @return ResourceAssociationSet |
||
152 | */ |
||
153 | public function getResourceAssociationSet(ResourceSet $sourceResourceSet, ResourceType $sourceResourceType, ResourceProperty $targetResourceProperty) |
||
181 | |||
182 | //End Implementation of IMetadataProvider |
||
183 | |||
184 | /** |
||
185 | * @param string $containerName container name for the datasource |
||
186 | * @param string $namespaceName namespace for the datasource |
||
187 | */ |
||
188 | public function __construct($containerName, $namespaceName) |
||
193 | |||
194 | /** |
||
195 | * Add an entity type. |
||
196 | * |
||
197 | * @param \ReflectionClass $refClass reflection class of the entity |
||
198 | * @param string $name name of the entity |
||
199 | * @param string $namespace namespace of the data source |
||
200 | * |
||
201 | * @return ResourceType |
||
202 | * |
||
203 | * @throws InvalidOperationException when the name is already in use |
||
204 | */ |
||
205 | public function addEntityType(\ReflectionClass $refClass, $name, $namespace = null) |
||
209 | |||
210 | /** |
||
211 | * Add a complex type. |
||
212 | * |
||
213 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
214 | * @param string $name name of the entity |
||
215 | * @param string $namespace namespace of the data source |
||
216 | * @param ResourceType $baseResourceType base resource type |
||
217 | * |
||
218 | * @return ResourceType |
||
219 | * |
||
220 | * @throws InvalidOperationException when the name is already in use |
||
221 | */ |
||
222 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
226 | |||
227 | /** |
||
228 | * @param string $name name of the resource set |
||
229 | * @param ResourceType $resourceType resource type |
||
230 | * |
||
231 | * @throws InvalidOperationException |
||
232 | * |
||
233 | * @return ResourceSet |
||
234 | */ |
||
235 | public function addResourceSet($name, ResourceType $resourceType) |
||
249 | |||
250 | /** |
||
251 | * To add a Key-primitive property to a resouce (Complex/Entuty). |
||
252 | * |
||
253 | * @param ResourceType $resourceType resource type to which key property |
||
254 | * is to be added |
||
255 | * @param string $name name of the key property |
||
256 | * @param TypeCode $typeCode type of the key property |
||
257 | */ |
||
258 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
262 | |||
263 | /** |
||
264 | * To add a NonKey-primitive property (Complex/Entity). |
||
265 | * |
||
266 | * @param ResourceType $resourceType resource type to which key property |
||
267 | * is to be added |
||
268 | * @param string $name name of the key property |
||
269 | * @param TypeCode $typeCode type of the key property |
||
270 | * @param bool $isBag property is bag or not |
||
271 | */ |
||
272 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) |
||
276 | |||
277 | /** |
||
278 | * To add a non-key etag property. |
||
279 | * |
||
280 | * @param ResourceType $resourceType resource type to which key property |
||
281 | * is to be added |
||
282 | * @param string $name name of the property |
||
283 | * @param string $typeCode type of the etag property |
||
284 | */ |
||
285 | public function addETagProperty($resourceType, $name, $typeCode) |
||
289 | |||
290 | /** |
||
291 | * To add a resource reference property. |
||
292 | * |
||
293 | * @param ResourceType $resourceType The resource type to add the resource |
||
294 | * reference property to |
||
295 | * @param string $name The name of the property to add |
||
296 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
297 | * property points to |
||
298 | */ |
||
299 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
308 | |||
309 | /** |
||
310 | * To add a resource set reference property. |
||
311 | * |
||
312 | * @param ResourceType $resourceType The resource type to add the |
||
313 | * resource reference set property to |
||
314 | * @param string $name The name of the property to add |
||
315 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
316 | * reference set property points to |
||
317 | */ |
||
318 | public function addResourceSetReferenceProperty($resourceType, $name, $targetResourceSet) |
||
327 | |||
328 | /** |
||
329 | * To add a complex property to entity or complex type. |
||
330 | * |
||
331 | * @param ResourceType $resourceType The resource type to which the |
||
332 | * complex property needs to add |
||
333 | * @param string $name name of the complex property |
||
334 | * @param ResourceType $complexResourceType complex resource type |
||
335 | * @param bool $isBag complex type is bag or not |
||
336 | * |
||
337 | * @return ResourceProperty |
||
338 | */ |
||
339 | public function addComplexProperty($resourceType, $name, $complexResourceType, $isBag = false) |
||
340 | { |
||
341 | if ($resourceType->getResourceTypeKind() != ResourceTypeKind::ENTITY |
||
342 | && $resourceType->getResourceTypeKind() != ResourceTypeKind::COMPLEX |
||
343 | ) { |
||
344 | throw new InvalidOperationException('Complex property can be added to an entity or another complex type'); |
||
345 | } |
||
346 | |||
347 | $this->checkInstanceProperty($name, $resourceType); |
||
348 | |||
349 | $kind = ResourcePropertyKind::COMPLEX_TYPE; |
||
350 | if ($isBag) { |
||
351 | $kind = $kind | ResourcePropertyKind::BAG; |
||
352 | } |
||
353 | |||
354 | $resourceProperty = new ResourceProperty($name, null, $kind, $complexResourceType); |
||
355 | $resourceType->addProperty($resourceProperty); |
||
356 | |||
357 | return $resourceProperty; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
362 | * |
||
363 | * @param ResourceType $resourceType Resource type |
||
364 | * @param string $name name of the property |
||
365 | * @param TypeCode $typeCode type of property |
||
366 | * @param bool $isKey property is key or not |
||
367 | * @param bool $isBag property is bag or not |
||
368 | * @param bool $isETagProperty property is etag or not |
||
369 | */ |
||
370 | private function _addPrimitivePropertyInternal($resourceType, $name, $typeCode, $isKey = false, $isBag = false, $isETagProperty = false) |
||
392 | |||
393 | /** |
||
394 | * To add a navigation property (resource set or resource reference) |
||
395 | * to a resource type. |
||
396 | * |
||
397 | * @param ResourceType $resourceType The resource type to add |
||
398 | * the resource reference |
||
399 | * or resource |
||
400 | * reference set property to |
||
401 | * @param string $name The name of the |
||
402 | * property to add |
||
403 | * @param ResourceSet $targetResourceSet The resource set the |
||
404 | * resource reference |
||
405 | * or reference |
||
406 | * set property |
||
407 | * ponits to |
||
408 | * @param ResourcePropertyKind $resourcePropertyKind The property kind |
||
409 | */ |
||
410 | private function _addReferencePropertyInternal( |
||
411 | ResourceType $resourceType, |
||
412 | $name, |
||
413 | ResourceSet $targetResourceSet, |
||
414 | $resourcePropertyKind |
||
415 | ) { |
||
416 | $this->checkInstanceProperty($name, $resourceType); |
||
417 | |||
418 | if (!($resourcePropertyKind == ResourcePropertyKind::RESOURCESET_REFERENCE |
||
419 | || $resourcePropertyKind == ResourcePropertyKind::RESOURCE_REFERENCE) |
||
420 | ) { |
||
421 | throw new InvalidOperationException( |
||
422 | 'Property kind should be ResourceSetReference or ResourceReference' |
||
423 | ); |
||
424 | } |
||
425 | |||
426 | $targetResourceType = $targetResourceSet->getResourceType(); |
||
427 | $resourceProperty = new ResourceProperty($name, null, $resourcePropertyKind, $targetResourceType); |
||
428 | $resourceType->addProperty($resourceProperty); |
||
429 | |||
430 | //Create instance of AssociationSet for this relationship |
||
431 | $sourceResourceSet = $resourceType->getCustomState(); |
||
432 | if (is_null($sourceResourceSet)) { |
||
433 | throw new InvalidOperationException('Failed to retrieve the custom state from ' . $resourceType->getName()); |
||
434 | } |
||
435 | |||
436 | //Customer_Orders_Orders, Order_Customer_Customers |
||
437 | //(source type::name _ source property::name _ target set::name) |
||
438 | $setKey = $resourceType->getName() . '_' . $name . '_' . $targetResourceSet->getName(); |
||
439 | $set = new ResourceAssociationSet( |
||
440 | $setKey, |
||
441 | new ResourceAssociationSetEnd($sourceResourceSet, $resourceType, $resourceProperty), |
||
442 | new ResourceAssociationSetEnd($targetResourceSet, $targetResourceSet->getResourceType(), null) |
||
443 | ); |
||
444 | $this->associationSets[$setKey] = $set; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @param \ReflectionClass $refClass |
||
449 | * @param $name |
||
450 | * @param $namespace |
||
451 | * @param $typeKind |
||
452 | * @param $baseResourceType |
||
453 | * @return ResourceType |
||
454 | * @throws InvalidOperationException |
||
455 | */ |
||
456 | private function createResourceType( |
||
473 | |||
474 | /** |
||
475 | * @param $name |
||
476 | * @param ResourceType $resourceType |
||
477 | * @throws InvalidOperationException |
||
478 | */ |
||
479 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
493 | } |
||
494 |
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: