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 |
||
13 | class SimpleMetadataProvider implements IMetadataProvider |
||
14 | { |
||
15 | protected $resourceSets = array(); |
||
16 | protected $resourceTypes = array(); |
||
17 | protected $associationSets = array(); |
||
18 | protected $containerName; |
||
19 | protected $namespaceName; |
||
20 | public $mappedDetails = null; |
||
21 | |||
22 | //Begin Implementation of IMetadataProvider |
||
23 | /** |
||
24 | * get the Container name for the data source. |
||
25 | * |
||
26 | * @return string container name |
||
27 | */ |
||
28 | public function getContainerName() |
||
32 | |||
33 | /** |
||
34 | * get Namespace name for the data source. |
||
35 | * |
||
36 | * @return string namespace |
||
37 | */ |
||
38 | public function getContainerNamespace() |
||
42 | |||
43 | /** |
||
44 | * get all entity set information. |
||
45 | * |
||
46 | * @return ResourceSet[] |
||
47 | */ |
||
48 | public function getResourceSets($params = null) |
||
74 | |||
75 | /** |
||
76 | * get all resource types in the data source. |
||
77 | * |
||
78 | * @return ResourceType[] |
||
79 | */ |
||
80 | public function getTypes() |
||
84 | |||
85 | /** |
||
86 | * get a resource set based on the specified resource set name. |
||
87 | * |
||
88 | * @param string $name Name of the resource set |
||
89 | * |
||
90 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
91 | */ |
||
92 | public function resolveResourceSet($name) |
||
100 | |||
101 | /** |
||
102 | * get a resource type based on the resource set name. |
||
103 | * |
||
104 | * @param string $name Name of the resource set |
||
105 | * |
||
106 | * @return ResourceType|null resource type with the given resource set name if found else NULL |
||
107 | */ |
||
108 | public function resolveResourceType($name) |
||
116 | |||
117 | /** |
||
118 | * The method must return a collection of all the types derived from |
||
119 | * $resourceType The collection returned should NOT include the type |
||
120 | * passed in as a parameter. |
||
121 | * |
||
122 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
123 | * |
||
124 | * @return ResourceType[] |
||
125 | */ |
||
126 | public function getDerivedTypes(ResourceType $resourceType) |
||
130 | |||
131 | /** |
||
132 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
133 | * |
||
134 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
135 | */ |
||
136 | public function hasDerivedTypes(ResourceType $resourceType) |
||
140 | |||
141 | /** |
||
142 | * Gets the ResourceAssociationSet instance for the given source |
||
143 | * association end. |
||
144 | * |
||
145 | * @param ResourceSet $sourceResourceSet Resource set |
||
146 | * of the source |
||
147 | * association end |
||
148 | * @param ResourceType $sourceResourceType Resource type of the source |
||
149 | * association end |
||
150 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
151 | * the source |
||
152 | * association end |
||
153 | * |
||
154 | * @return ResourceAssociationSet |
||
155 | */ |
||
156 | public function getResourceAssociationSet(ResourceSet $sourceResourceSet, ResourceType $sourceResourceType, ResourceProperty $targetResourceProperty) |
||
184 | |||
185 | //End Implementation of IMetadataProvider |
||
186 | |||
187 | /** |
||
188 | * @param string $containerName container name for the datasource |
||
189 | * @param string $namespaceName namespace for the datasource |
||
190 | */ |
||
191 | public function __construct($containerName, $namespaceName) |
||
196 | |||
197 | /** |
||
198 | * Add an entity type. |
||
199 | * |
||
200 | * @param \ReflectionClass $refClass reflection class of the entity |
||
201 | * @param string $name name of the entity |
||
202 | * @param string $namespace namespace of the data source |
||
203 | * |
||
204 | * @return ResourceType |
||
205 | * |
||
206 | * @throws InvalidOperationException when the name is already in use |
||
207 | */ |
||
208 | public function addEntityType(\ReflectionClass $refClass, $name, $namespace = null) |
||
212 | |||
213 | /** |
||
214 | * Add a complex type. |
||
215 | * |
||
216 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
217 | * @param string $name name of the entity |
||
218 | * @param string $namespace namespace of the data source |
||
219 | * @param ResourceType $baseResourceType base resource type |
||
220 | * |
||
221 | * @return ResourceType |
||
222 | * |
||
223 | * @throws InvalidOperationException when the name is already in use |
||
224 | */ |
||
225 | public function addComplexType(\ReflectionClass $refClass, $name, $namespace = null, $baseResourceType = null) |
||
229 | |||
230 | /** |
||
231 | * @param string $name name of the resource set |
||
232 | * @param ResourceType $resourceType resource type |
||
233 | * |
||
234 | * @throws InvalidOperationException |
||
235 | * |
||
236 | * @return ResourceSet |
||
237 | */ |
||
238 | public function addResourceSet($name, ResourceType $resourceType) |
||
252 | |||
253 | /** |
||
254 | * To add a Key-primitive property to a resouce (Complex/Entuty). |
||
255 | * |
||
256 | * @param ResourceType $resourceType resource type to which key property |
||
257 | * is to be added |
||
258 | * @param string $name name of the key property |
||
259 | * @param TypeCode $typeCode type of the key property |
||
260 | */ |
||
261 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
265 | |||
266 | /** |
||
267 | * To add a NonKey-primitive property (Complex/Entity). |
||
268 | * |
||
269 | * @param ResourceType $resourceType resource type to which key property |
||
270 | * is to be added |
||
271 | * @param string $name name of the key property |
||
272 | * @param TypeCode $typeCode type of the key property |
||
273 | * @param bool $isBag property is bag or not |
||
274 | */ |
||
275 | public function addPrimitiveProperty($resourceType, $name, $typeCode, $isBag = false) |
||
279 | |||
280 | /** |
||
281 | * To add a non-key etag property. |
||
282 | * |
||
283 | * @param ResourceType $resourceType resource type to which key property |
||
284 | * is to be added |
||
285 | * @param string $name name of the property |
||
286 | * @param TypeCode $typeCode type of the etag property |
||
287 | */ |
||
288 | public function addETagProperty($resourceType, $name, $typeCode) |
||
292 | |||
293 | /** |
||
294 | * To add a resource reference property. |
||
295 | * |
||
296 | * @param ResourceType $resourceType The resource type to add the resource |
||
297 | * reference property to |
||
298 | * @param string $name The name of the property to add |
||
299 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
300 | * property points to |
||
301 | */ |
||
302 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
311 | |||
312 | /** |
||
313 | * To add a resource set reference property. |
||
314 | * |
||
315 | * @param ResourceType $resourceType The resource type to add the |
||
316 | * resource reference set property to |
||
317 | * @param string $name The name of the property to add |
||
318 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
319 | * reference set property points to |
||
320 | */ |
||
321 | public function addResourceSetReferenceProperty($resourceType, $name, $targetResourceSet) |
||
330 | |||
331 | /** |
||
332 | * To add a complex property to entity or complex type. |
||
333 | * |
||
334 | * @param ResourceType $resourceType The resource type to which the |
||
335 | * complex property needs to add |
||
336 | * @param string $name name of the complex property |
||
337 | * @param ResourceType $complexResourceType complex resource type |
||
338 | * @param bool $isBag complex type is bag or not |
||
339 | * |
||
340 | * @return ResourceProperty |
||
341 | */ |
||
342 | public function addComplexProperty($resourceType, $name, $complexResourceType, $isBag = false) |
||
362 | |||
363 | /** |
||
364 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
365 | * |
||
366 | * @param ResourceType $resourceType Resource type |
||
367 | * @param string $name name of the property |
||
368 | * @param TypeCode $typeCode type of property |
||
369 | * @param bool $isKey property is key or not |
||
370 | * @param bool $isBag property is bag or not |
||
371 | * @param bool $isETagProperty property is etag or not |
||
372 | */ |
||
373 | private function _addPrimitivePropertyInternal($resourceType, $name, $typeCode, $isKey = false, $isBag = false, $isETagProperty = false) |
||
397 | |||
398 | /** |
||
399 | * To add a navigation property (resource set or resource reference) |
||
400 | * to a resource type. |
||
401 | * |
||
402 | * @param ResourceType $resourceType The resource type to add |
||
403 | * the resource reference |
||
404 | * or resource |
||
405 | * reference set property to |
||
406 | * @param string $name The name of the |
||
407 | * property to add |
||
408 | * @param ResourceSet $targetResourceSet The resource set the |
||
409 | * resource reference |
||
410 | * or reference |
||
411 | * set property |
||
412 | * ponits to |
||
413 | * @param ResourcePropertyKind $resourcePropertyKind The property kind |
||
414 | */ |
||
415 | private function _addReferencePropertyInternal( |
||
451 | |||
452 | /** |
||
453 | * @param \ReflectionClass $refClass |
||
454 | * @param $name |
||
455 | * @param $namespace |
||
456 | * @param $typeKind |
||
457 | * @param $baseResourceType |
||
458 | * @return ResourceType |
||
459 | * @throws InvalidOperationException |
||
460 | */ |
||
461 | private function createResourceType( |
||
478 | |||
479 | /** |
||
480 | * @param $name |
||
481 | * @param ResourceType $resourceType |
||
482 | * @throws InvalidOperationException |
||
483 | */ |
||
484 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
501 | } |
||
502 |
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: