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 |
||
17 | class SimpleMetadataProvider implements IMetadataProvider |
||
18 | { |
||
19 | public $OdataEntityMap = []; |
||
20 | protected $resourceSets = []; |
||
21 | protected $resourceTypes = []; |
||
22 | protected $associationSets = []; |
||
23 | protected $containerName; |
||
24 | protected $namespaceName; |
||
25 | private $metadataManager; |
||
26 | private $typeSetMapping = []; |
||
27 | |||
28 | /** |
||
29 | * @param string $containerName container name for the datasource |
||
30 | * @param string $namespaceName namespace for the datasource |
||
31 | */ |
||
32 | public function __construct($containerName, $namespaceName) |
||
38 | |||
39 | //Begin Implementation of IMetadataProvider |
||
40 | |||
41 | public function getXML() |
||
45 | |||
46 | /** |
||
47 | * get the Container name for the data source. |
||
48 | * |
||
49 | * @return string container name |
||
50 | */ |
||
51 | public function getContainerName() |
||
55 | |||
56 | /** |
||
57 | * get Namespace name for the data source. |
||
58 | * |
||
59 | * @return string namespace |
||
60 | */ |
||
61 | public function getContainerNamespace() |
||
65 | |||
66 | /** |
||
67 | * get all entity set information. |
||
68 | * |
||
69 | * @return ResourceSet[] |
||
70 | */ |
||
71 | public function getResourceSets($params = null) |
||
98 | |||
99 | /** |
||
100 | * get all resource types in the data source. |
||
101 | * |
||
102 | * @return ResourceType[] |
||
103 | */ |
||
104 | public function getTypes() |
||
108 | |||
109 | /** |
||
110 | * get a resource set based on the specified resource set name. |
||
111 | * |
||
112 | * @param string $name Name of the resource set |
||
113 | * |
||
114 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
115 | */ |
||
116 | public function resolveResourceSet($name) |
||
123 | |||
124 | /** |
||
125 | * get a resource type based on the resource type name. |
||
126 | * |
||
127 | * @param string $name Name of the resource type |
||
128 | * |
||
129 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
130 | */ |
||
131 | public function resolveResourceType($name) |
||
138 | |||
139 | /** |
||
140 | * get a resource set based on the specified resource association set name. |
||
141 | * |
||
142 | * @param string $name Name of the resource assocation set |
||
143 | * |
||
144 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
145 | */ |
||
146 | public function resolveAssociationSet($name) |
||
153 | |||
154 | /* |
||
155 | * Get number of association sets hooked up |
||
156 | */ |
||
157 | public function getAssociationCount() |
||
161 | |||
162 | /** |
||
163 | * The method must return a collection of all the types derived from |
||
164 | * $resourceType The collection returned should NOT include the type |
||
165 | * passed in as a parameter. |
||
166 | * |
||
167 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
168 | * |
||
169 | * @return ResourceType[] |
||
170 | */ |
||
171 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
175 | |||
176 | /** |
||
177 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
178 | * |
||
179 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
180 | */ |
||
181 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
185 | |||
186 | //End Implementation of IMetadataProvider |
||
187 | |||
188 | /** |
||
189 | * Gets the ResourceAssociationSet instance for the given source |
||
190 | * association end. |
||
191 | * |
||
192 | * @param ResourceSet $sourceResourceSet Resource set |
||
193 | * of the source |
||
194 | * association end |
||
195 | * @param ResourceType $sourceResourceType Resource type of the source |
||
196 | * association end |
||
197 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
198 | * the source |
||
199 | * association end |
||
200 | * |
||
201 | * @return ResourceAssociationSet|null |
||
202 | */ |
||
203 | public function getResourceAssociationSet( |
||
242 | |||
243 | /** |
||
244 | * Add an entity type. |
||
245 | * |
||
246 | * @param \ReflectionClass $refClass reflection class of the entity |
||
247 | * @param string $name name of the entity |
||
248 | * @return ResourceType when the name is already in use |
||
249 | * |
||
250 | * @throws InvalidOperationException when the name is already in use |
||
251 | * @internal param string $namespace namespace of the data source |
||
252 | * |
||
253 | */ |
||
254 | public function addEntityType(\ReflectionClass $refClass, $name) |
||
258 | |||
259 | /** |
||
260 | * @param \ReflectionClass $refClass |
||
261 | * @param string $name |
||
262 | * @param $typeKind |
||
263 | * @return ResourceType |
||
264 | * @throws InvalidOperationException |
||
265 | * @internal param null|string $namespace |
||
266 | * @internal param null|ResourceType $baseResourceType |
||
267 | * |
||
268 | */ |
||
269 | private function createResourceType( |
||
301 | |||
302 | /** |
||
303 | * Add a complex type. |
||
304 | * |
||
305 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
306 | * @param string $name name of the entity |
||
307 | * @return ResourceType when the name is already in use |
||
308 | * |
||
309 | * @throws InvalidOperationException when the name is already in use |
||
310 | * @internal param string $namespace namespace of the data source |
||
311 | * @internal param ResourceType $baseResourceType base resource type |
||
312 | * |
||
313 | */ |
||
314 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
318 | |||
319 | /** |
||
320 | * @param string $name name of the resource set (now taken from resource type) |
||
321 | * @param ResourceEntityType $resourceType resource type |
||
322 | * |
||
323 | * @throws InvalidOperationException |
||
324 | * |
||
325 | * @return ResourceSet |
||
326 | */ |
||
327 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
343 | |||
344 | /** |
||
345 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
346 | * |
||
347 | * @param ResourceType $resourceType resource type to which key property |
||
348 | * is to be added |
||
349 | * @param string $name name of the key property |
||
350 | * @param TypeCode $typeCode type of the key property |
||
351 | */ |
||
352 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
356 | |||
357 | /** |
||
358 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
359 | * |
||
360 | * @param ResourceType $resourceType Resource type |
||
361 | * @param string $name name of the property |
||
362 | * @param TypeCode $typeCode type of property |
||
363 | * @param bool $isKey property is key or not |
||
364 | * @param bool $isBag property is bag or not |
||
365 | * @param bool $isETagProperty property is etag or not |
||
366 | */ |
||
367 | private function _addPrimitivePropertyInternal( |
||
416 | |||
417 | /** |
||
418 | * @param string $name |
||
419 | * @param ResourceType $resourceType |
||
420 | * |
||
421 | * @throws InvalidOperationException |
||
422 | */ |
||
423 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
440 | |||
441 | /** |
||
442 | * To add a NonKey-primitive property (Complex/Entity). |
||
443 | * |
||
444 | * @param ResourceType $resourceType resource type to which key property |
||
445 | * is to be added |
||
446 | * @param string $name name of the key property |
||
447 | * @param TypeCode $typeCode type of the key property |
||
448 | * @param bool $isBag property is bag or not |
||
449 | */ |
||
450 | View Code Duplication | public function addPrimitiveProperty( |
|
469 | |||
470 | /** |
||
471 | * To add a non-key etag property. |
||
472 | * |
||
473 | * @param ResourceType $resourceType resource type to which key property |
||
474 | * is to be added |
||
475 | * @param string $name name of the property |
||
476 | * @param TypeCode $typeCode type of the etag property |
||
477 | */ |
||
478 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
491 | |||
492 | /** |
||
493 | * To add a resource reference property. |
||
494 | * |
||
495 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
496 | * reference property to |
||
497 | * @param string $name The name of the property to add |
||
498 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
499 | * property points to |
||
500 | */ |
||
501 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
510 | |||
511 | /** |
||
512 | * To add a 1:N resource reference property. |
||
513 | * |
||
514 | * @param ResourceType $sourceResourceType The resource type to add the resource |
||
515 | * reference property from |
||
516 | * @param ResourceType $targetResourceType The resource type to add the resource |
||
517 | * reference property to |
||
518 | * @param string $sourceProperty The name of the property to add, on source type |
||
519 | * @param string $targetProperty The name of the property to add, on target type |
||
520 | */ |
||
521 | public function addResourceReferencePropertyBidirectional( |
||
536 | |||
537 | /** |
||
538 | * To add a navigation property (resource set or resource reference) |
||
539 | * to a resource type. |
||
540 | * |
||
541 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
542 | * the resource reference |
||
543 | * or resource |
||
544 | * reference set property to |
||
545 | * @param string $name The name of the |
||
546 | * property to add |
||
547 | * @param ResourceSet $targetResourceSet The resource set the |
||
548 | * resource reference |
||
549 | * or reference |
||
550 | * set property |
||
551 | * points to |
||
552 | * @param string $resourceMult The multiplicity of relation being added |
||
553 | */ |
||
554 | private function _addReferencePropertyInternal( |
||
609 | |||
610 | /** |
||
611 | * To add a navigation property (resource set or resource reference) |
||
612 | * to a resource type. |
||
613 | * |
||
614 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
615 | * the resource reference |
||
616 | * or resource reference set property to |
||
617 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
618 | * the resource reference |
||
619 | * or resource reference set property to |
||
620 | * @param string $sourceProperty The name of the |
||
621 | * property to add to source type |
||
622 | * @param string $targetProperty The name of the |
||
623 | * property to add to target type |
||
624 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
625 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
626 | */ |
||
627 | private function _addReferencePropertyInternalBidirectional( |
||
716 | |||
717 | /** |
||
718 | * To add a resource set reference property. |
||
719 | * |
||
720 | * @param ResourceEntityType $resourceType The resource type to add the |
||
721 | * resource reference set property to |
||
722 | * @param string $name The name of the property to add |
||
723 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
724 | * reference set property points to |
||
725 | */ |
||
726 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
735 | |||
736 | /** |
||
737 | * To add a M:N resource reference property. |
||
738 | * |
||
739 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
740 | * reference property from |
||
741 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
742 | * reference property to |
||
743 | * @param string $sourceProperty The name of the property to add, on source type |
||
744 | * @param string $targetProperty The name of the property to add, on target type |
||
745 | */ |
||
746 | public function addResourceSetReferencePropertyBidirectional( |
||
761 | |||
762 | /** |
||
763 | * To add a 1-1 resource reference. |
||
764 | * |
||
765 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
766 | * reference property from |
||
767 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
768 | * reference property to |
||
769 | * @param string $sourceProperty The name of the property to add, on source type |
||
770 | * @param string $targetProperty The name of the property to add, on target type |
||
771 | */ |
||
772 | public function addResourceReferenceSinglePropertyBidirectional( |
||
787 | |||
788 | /** |
||
789 | * To add a complex property to entity or complex type. |
||
790 | * |
||
791 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
792 | * @param string $name name of the complex property |
||
793 | * @param ResourceComplexType $complexResourceType complex resource type |
||
794 | * @param bool $isBag complex type is bag or not |
||
795 | * |
||
796 | * @return ResourceProperty |
||
797 | */ |
||
798 | public function addComplexProperty( |
||
829 | } |
||
830 |
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: