Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
18 | class SimpleMetadataProvider implements IMetadataProvider |
||
19 | { |
||
20 | public $OdataEntityMap = []; |
||
21 | protected $resourceSets = []; |
||
22 | protected $resourceTypes = []; |
||
23 | protected $associationSets = []; |
||
24 | protected $containerName; |
||
25 | protected $namespaceName; |
||
26 | private $metadataManager; |
||
27 | private $typeSetMapping = []; |
||
28 | protected $singletons = []; |
||
29 | |||
30 | /** |
||
31 | * @param string $containerName container name for the datasource |
||
32 | * @param string $namespaceName namespace for the datasource |
||
33 | */ |
||
34 | public function __construct($containerName, $namespaceName) |
||
40 | |||
41 | //Begin Implementation of IMetadataProvider |
||
42 | |||
43 | public function getXML() |
||
47 | |||
48 | /** |
||
49 | * get the Container name for the data source. |
||
50 | * |
||
51 | * @return string container name |
||
52 | */ |
||
53 | public function getContainerName() |
||
57 | |||
58 | /** |
||
59 | * get Namespace name for the data source. |
||
60 | * |
||
61 | * @return string namespace |
||
62 | */ |
||
63 | public function getContainerNamespace() |
||
67 | |||
68 | /** |
||
69 | * get all entity set information. |
||
70 | * |
||
71 | * @return ResourceSet[] |
||
72 | */ |
||
73 | public function getResourceSets($params = null) |
||
100 | |||
101 | /** |
||
102 | * get all resource types in the data source. |
||
103 | * |
||
104 | * @return ResourceType[] |
||
105 | */ |
||
106 | public function getTypes() |
||
110 | |||
111 | /** |
||
112 | * get a resource set based on the specified resource set name. |
||
113 | * |
||
114 | * @param string $name Name of the resource set |
||
115 | * |
||
116 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
117 | */ |
||
118 | public function resolveResourceSet($name) |
||
125 | |||
126 | /** |
||
127 | * get a resource type based on the resource type name. |
||
128 | * |
||
129 | * @param string $name Name of the resource type |
||
130 | * |
||
131 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
132 | */ |
||
133 | public function resolveResourceType($name) |
||
140 | |||
141 | /** |
||
142 | * get a singelton based on the specified singleton name. |
||
143 | * |
||
144 | * @param string $name Name of the resource set |
||
145 | * |
||
146 | * @return ResourceFunctionType|null singleton with the given name if found else NULL |
||
147 | */ |
||
148 | public function resolveSingleton($name) |
||
155 | |||
156 | /** |
||
157 | * get a resource set based on the specified resource association set name. |
||
158 | * |
||
159 | * @param string $name Name of the resource assocation set |
||
160 | * |
||
161 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
162 | */ |
||
163 | public function resolveAssociationSet($name) |
||
170 | |||
171 | /* |
||
172 | * Get number of association sets hooked up |
||
173 | */ |
||
174 | public function getAssociationCount() |
||
178 | |||
179 | /** |
||
180 | * The method must return a collection of all the types derived from |
||
181 | * $resourceType The collection returned should NOT include the type |
||
182 | * passed in as a parameter. |
||
183 | * |
||
184 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
185 | * |
||
186 | * @return ResourceType[] |
||
187 | */ |
||
188 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
192 | |||
193 | /** |
||
194 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
195 | * |
||
196 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
197 | */ |
||
198 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
202 | |||
203 | //End Implementation of IMetadataProvider |
||
204 | |||
205 | /** |
||
206 | * Gets the ResourceAssociationSet instance for the given source |
||
207 | * association end. |
||
208 | * |
||
209 | * @param ResourceSet $sourceResourceSet Resource set |
||
210 | * of the source |
||
211 | * association end |
||
212 | * @param ResourceType $sourceResourceType Resource type of the source |
||
213 | * association end |
||
214 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
215 | * the source |
||
216 | * association end |
||
217 | * |
||
218 | * @return ResourceAssociationSet|null |
||
219 | */ |
||
220 | public function getResourceAssociationSet( |
||
259 | |||
260 | /** |
||
261 | * Add an entity type. |
||
262 | * |
||
263 | * @param \ReflectionClass $refClass reflection class of the entity |
||
264 | * @param string $name name of the entity |
||
265 | * @return ResourceType when the name is already in use |
||
266 | * |
||
267 | * @throws InvalidOperationException when the name is already in use |
||
268 | * @internal param string $namespace namespace of the data source |
||
269 | * |
||
270 | */ |
||
271 | public function addEntityType(\ReflectionClass $refClass, $name) |
||
275 | |||
276 | /** |
||
277 | * @param \ReflectionClass $refClass |
||
278 | * @param string $name |
||
279 | * @param $typeKind |
||
280 | * @return ResourceType |
||
281 | * @throws InvalidOperationException |
||
282 | * @internal param null|string $namespace |
||
283 | * @internal param null|ResourceType $baseResourceType |
||
284 | * |
||
285 | */ |
||
286 | private function createResourceType( |
||
318 | |||
319 | /** |
||
320 | * Add a complex type. |
||
321 | * |
||
322 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
323 | * @param string $name name of the entity |
||
324 | * @return ResourceType when the name is already in use |
||
325 | * |
||
326 | * @throws InvalidOperationException when the name is already in use |
||
327 | * @internal param string $namespace namespace of the data source |
||
328 | * @internal param ResourceType $baseResourceType base resource type |
||
329 | * |
||
330 | */ |
||
331 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
335 | |||
336 | /** |
||
337 | * @param string $name name of the resource set (now taken from resource type) |
||
338 | * @param ResourceEntityType $resourceType resource type |
||
339 | * |
||
340 | * @throws InvalidOperationException |
||
341 | * |
||
342 | * @return ResourceSet |
||
343 | */ |
||
344 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
360 | |||
361 | /** |
||
362 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
363 | * |
||
364 | * @param ResourceType $resourceType resource type to which key property |
||
365 | * is to be added |
||
366 | * @param string $name name of the key property |
||
367 | * @param TypeCode $typeCode type of the key property |
||
368 | */ |
||
369 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
373 | |||
374 | /** |
||
375 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
376 | * |
||
377 | * @param ResourceType $resourceType Resource type |
||
378 | * @param string $name name of the property |
||
379 | * @param TypeCode $typeCode type of property |
||
380 | * @param bool $isKey property is key or not |
||
381 | * @param bool $isBag property is bag or not |
||
382 | * @param bool $isETagProperty property is etag or not |
||
383 | */ |
||
384 | private function _addPrimitivePropertyInternal( |
||
433 | |||
434 | /** |
||
435 | * @param string $name |
||
436 | * @param ResourceType $resourceType |
||
437 | * |
||
438 | * @throws InvalidOperationException |
||
439 | */ |
||
440 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
457 | |||
458 | /** |
||
459 | * To add a NonKey-primitive property (Complex/Entity). |
||
460 | * |
||
461 | * @param ResourceType $resourceType resource type to which key property |
||
462 | * is to be added |
||
463 | * @param string $name name of the key property |
||
464 | * @param TypeCode $typeCode type of the key property |
||
465 | * @param bool $isBag property is bag or not |
||
466 | */ |
||
467 | View Code Duplication | public function addPrimitiveProperty( |
|
486 | |||
487 | /** |
||
488 | * To add a non-key etag property. |
||
489 | * |
||
490 | * @param ResourceType $resourceType resource type to which key property |
||
491 | * is to be added |
||
492 | * @param string $name name of the property |
||
493 | * @param TypeCode $typeCode type of the etag property |
||
494 | */ |
||
495 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
508 | |||
509 | /** |
||
510 | * To add a resource reference property. |
||
511 | * |
||
512 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
513 | * reference property to |
||
514 | * @param string $name The name of the property to add |
||
515 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
516 | * property points to |
||
517 | */ |
||
518 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
527 | |||
528 | /** |
||
529 | * To add a 1:N resource reference property. |
||
530 | * |
||
531 | * @param ResourceType $sourceResourceType The resource type to add the resource |
||
532 | * reference property from |
||
533 | * @param ResourceType $targetResourceType The resource type to add the resource |
||
534 | * reference property to |
||
535 | * @param string $sourceProperty The name of the property to add, on source type |
||
536 | * @param string $targetProperty The name of the property to add, on target type |
||
537 | */ |
||
538 | public function addResourceReferencePropertyBidirectional( |
||
553 | |||
554 | /** |
||
555 | * To add a navigation property (resource set or resource reference) |
||
556 | * to a resource type. |
||
557 | * |
||
558 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
559 | * the resource reference |
||
560 | * or resource |
||
561 | * reference set property to |
||
562 | * @param string $name The name of the |
||
563 | * property to add |
||
564 | * @param ResourceSet $targetResourceSet The resource set the |
||
565 | * resource reference |
||
566 | * or reference |
||
567 | * set property |
||
568 | * points to |
||
569 | * @param string $resourceMult The multiplicity of relation being added |
||
570 | */ |
||
571 | private function _addReferencePropertyInternal( |
||
626 | |||
627 | /** |
||
628 | * To add a navigation property (resource set or resource reference) |
||
629 | * to a resource type. |
||
630 | * |
||
631 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
632 | * the resource reference |
||
633 | * or resource reference set property to |
||
634 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
635 | * the resource reference |
||
636 | * or resource reference set property to |
||
637 | * @param string $sourceProperty The name of the |
||
638 | * property to add to source type |
||
639 | * @param string $targetProperty The name of the |
||
640 | * property to add to target type |
||
641 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
642 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
643 | */ |
||
644 | private function _addReferencePropertyInternalBidirectional( |
||
733 | |||
734 | /** |
||
735 | * To add a resource set reference property. |
||
736 | * |
||
737 | * @param ResourceEntityType $resourceType The resource type to add the |
||
738 | * resource reference set property to |
||
739 | * @param string $name The name of the property to add |
||
740 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
741 | * reference set property points to |
||
742 | */ |
||
743 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
752 | |||
753 | /** |
||
754 | * To add a M:N resource reference property. |
||
755 | * |
||
756 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
757 | * reference property from |
||
758 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
759 | * reference property to |
||
760 | * @param string $sourceProperty The name of the property to add, on source type |
||
761 | * @param string $targetProperty The name of the property to add, on target type |
||
762 | */ |
||
763 | public function addResourceSetReferencePropertyBidirectional( |
||
778 | |||
779 | /** |
||
780 | * To add a 1-1 resource reference. |
||
781 | * |
||
782 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
783 | * reference property from |
||
784 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
785 | * reference property to |
||
786 | * @param string $sourceProperty The name of the property to add, on source type |
||
787 | * @param string $targetProperty The name of the property to add, on target type |
||
788 | */ |
||
789 | public function addResourceReferenceSinglePropertyBidirectional( |
||
804 | |||
805 | /** |
||
806 | * To add a complex property to entity or complex type. |
||
807 | * |
||
808 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
809 | * @param string $name name of the complex property |
||
810 | * @param ResourceComplexType $complexResourceType complex resource type |
||
811 | * @param bool $isBag complex type is bag or not |
||
812 | * |
||
813 | * @return ResourceProperty |
||
814 | */ |
||
815 | public function addComplexProperty( |
||
846 | |||
847 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
871 | |||
872 | public function getSingletons() |
||
876 | |||
877 | public function callSingleton($name) |
||
886 | } |
||
887 |
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: