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 | View Code Duplication | public function addResourceReferencePropertyBidirectional( |
|
| 564 | |||
| 565 | /** |
||
| 566 | * To add a navigation property (resource set or resource reference) |
||
| 567 | * to a resource type. |
||
| 568 | * |
||
| 569 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
| 570 | * the resource reference |
||
| 571 | * or resource |
||
| 572 | * reference set property to |
||
| 573 | * @param string $name The name of the |
||
| 574 | * property to add |
||
| 575 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 576 | * resource reference |
||
| 577 | * or reference |
||
| 578 | * set property |
||
| 579 | * points to |
||
| 580 | * @param string $resourceMult The multiplicity of relation being added |
||
| 581 | */ |
||
| 582 | private function _addReferencePropertyInternal( |
||
| 637 | |||
| 638 | /** |
||
| 639 | * To add a navigation property (resource set or resource reference) |
||
| 640 | * to a resource type. |
||
| 641 | * |
||
| 642 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
| 643 | * the resource reference |
||
| 644 | * or resource reference set property to |
||
| 645 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
| 646 | * the resource reference |
||
| 647 | * or resource reference set property to |
||
| 648 | * @param string $sourceProperty The name of the |
||
| 649 | * property to add to source type |
||
| 650 | * @param string $targetProperty The name of the |
||
| 651 | * property to add to target type |
||
| 652 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
| 653 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
| 654 | */ |
||
| 655 | private function _addReferencePropertyInternalBidirectional( |
||
| 752 | |||
| 753 | /** |
||
| 754 | * To add a resource set reference property. |
||
| 755 | * |
||
| 756 | * @param ResourceEntityType $resourceType The resource type to add the |
||
| 757 | * resource reference set property to |
||
| 758 | * @param string $name The name of the property to add |
||
| 759 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 760 | * reference set property points to |
||
| 761 | */ |
||
| 762 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
| 771 | |||
| 772 | /** |
||
| 773 | * To add a M:N resource reference property. |
||
| 774 | * |
||
| 775 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 776 | * reference property from |
||
| 777 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 778 | * reference property to |
||
| 779 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 780 | * @param string $targetProperty The name of the property to add, on target type |
||
| 781 | */ |
||
| 782 | View Code Duplication | public function addResourceSetReferencePropertyBidirectional( |
|
| 808 | |||
| 809 | /** |
||
| 810 | * To add a 1-1 resource reference. |
||
| 811 | * |
||
| 812 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 813 | * reference property from |
||
| 814 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 815 | * reference property to |
||
| 816 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 817 | * @param string $targetProperty The name of the property to add, on target type |
||
| 818 | */ |
||
| 819 | View Code Duplication | public function addResourceReferenceSinglePropertyBidirectional( |
|
| 845 | |||
| 846 | /** |
||
| 847 | * To add a complex property to entity or complex type. |
||
| 848 | * |
||
| 849 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
| 850 | * @param string $name name of the complex property |
||
| 851 | * @param ResourceComplexType $complexResourceType complex resource type |
||
| 852 | * @param bool $isBag complex type is bag or not |
||
| 853 | * |
||
| 854 | * @return ResourceProperty |
||
| 855 | */ |
||
| 856 | public function addComplexProperty( |
||
| 887 | |||
| 888 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
| 912 | |||
| 913 | public function getSingletons() |
||
| 917 | |||
| 918 | public function callSingleton($name) |
||
| 927 | } |
||
| 928 |
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: