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 resource set based on the specified resource association set name. |
||
| 143 | * |
||
| 144 | * @param string $name Name of the resource assocation set |
||
| 145 | * |
||
| 146 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 147 | */ |
||
| 148 | public function resolveAssociationSet($name) |
||
| 155 | |||
| 156 | /* |
||
| 157 | * Get number of association sets hooked up |
||
| 158 | */ |
||
| 159 | public function getAssociationCount() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The method must return a collection of all the types derived from |
||
| 166 | * $resourceType The collection returned should NOT include the type |
||
| 167 | * passed in as a parameter. |
||
| 168 | * |
||
| 169 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
| 170 | * |
||
| 171 | * @return ResourceType[] |
||
| 172 | */ |
||
| 173 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param ResourceType $resourceType Resource to check for derived resource types |
||
| 180 | * |
||
| 181 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 182 | */ |
||
| 183 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
| 187 | |||
| 188 | //End Implementation of IMetadataProvider |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Gets the ResourceAssociationSet instance for the given source |
||
| 192 | * association end. |
||
| 193 | * |
||
| 194 | * @param ResourceSet $sourceResourceSet Resource set |
||
| 195 | * of the source |
||
| 196 | * association end |
||
| 197 | * @param ResourceType $sourceResourceType Resource type of the source |
||
| 198 | * association end |
||
| 199 | * @param ResourceProperty $targetResourceProperty Resource property of |
||
| 200 | * the source |
||
| 201 | * association end |
||
| 202 | * |
||
| 203 | * @return ResourceAssociationSet|null |
||
| 204 | */ |
||
| 205 | public function getResourceAssociationSet( |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Add an entity type. |
||
| 247 | * |
||
| 248 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 249 | * @param string $name name of the entity |
||
| 250 | * @return ResourceType when the name is already in use |
||
| 251 | * |
||
| 252 | * @throws InvalidOperationException when the name is already in use |
||
| 253 | * @internal param string $namespace namespace of the data source |
||
| 254 | * |
||
| 255 | */ |
||
| 256 | public function addEntityType(\ReflectionClass $refClass, $name) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @param \ReflectionClass $refClass |
||
| 263 | * @param string $name |
||
| 264 | * @param $typeKind |
||
| 265 | * @return ResourceType |
||
| 266 | * @throws InvalidOperationException |
||
| 267 | * @internal param null|string $namespace |
||
| 268 | * @internal param null|ResourceType $baseResourceType |
||
| 269 | * |
||
| 270 | */ |
||
| 271 | private function createResourceType( |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Add a complex type. |
||
| 306 | * |
||
| 307 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 308 | * @param string $name name of the entity |
||
| 309 | * @return ResourceType when the name is already in use |
||
| 310 | * |
||
| 311 | * @throws InvalidOperationException when the name is already in use |
||
| 312 | * @internal param string $namespace namespace of the data source |
||
| 313 | * @internal param ResourceType $baseResourceType base resource type |
||
| 314 | * |
||
| 315 | */ |
||
| 316 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $name name of the resource set (now taken from resource type) |
||
| 323 | * @param ResourceEntityType $resourceType resource type |
||
| 324 | * |
||
| 325 | * @throws InvalidOperationException |
||
| 326 | * |
||
| 327 | * @return ResourceSet |
||
| 328 | */ |
||
| 329 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
| 348 | * |
||
| 349 | * @param ResourceType $resourceType resource type to which key property |
||
| 350 | * is to be added |
||
| 351 | * @param string $name name of the key property |
||
| 352 | * @param TypeCode $typeCode type of the key property |
||
| 353 | */ |
||
| 354 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 361 | * |
||
| 362 | * @param ResourceType $resourceType Resource type |
||
| 363 | * @param string $name name of the property |
||
| 364 | * @param TypeCode $typeCode type of property |
||
| 365 | * @param bool $isKey property is key or not |
||
| 366 | * @param bool $isBag property is bag or not |
||
| 367 | * @param bool $isETagProperty property is etag or not |
||
| 368 | */ |
||
| 369 | private function _addPrimitivePropertyInternal( |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $name |
||
| 421 | * @param ResourceType $resourceType |
||
| 422 | * |
||
| 423 | * @throws InvalidOperationException |
||
| 424 | */ |
||
| 425 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 445 | * |
||
| 446 | * @param ResourceType $resourceType resource type to which key property |
||
| 447 | * is to be added |
||
| 448 | * @param string $name name of the key property |
||
| 449 | * @param TypeCode $typeCode type of the key property |
||
| 450 | * @param bool $isBag property is bag or not |
||
| 451 | */ |
||
| 452 | View Code Duplication | public function addPrimitiveProperty( |
|
| 471 | |||
| 472 | /** |
||
| 473 | * To add a non-key etag property. |
||
| 474 | * |
||
| 475 | * @param ResourceType $resourceType resource type to which key property |
||
| 476 | * is to be added |
||
| 477 | * @param string $name name of the property |
||
| 478 | * @param TypeCode $typeCode type of the etag property |
||
| 479 | */ |
||
| 480 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * To add a resource reference property. |
||
| 496 | * |
||
| 497 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
| 498 | * reference property to |
||
| 499 | * @param string $name The name of the property to add |
||
| 500 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 501 | * property points to |
||
| 502 | */ |
||
| 503 | public function addResourceReferenceProperty($resourceType, $name, $targetResourceSet) |
||
| 512 | |||
| 513 | /** |
||
| 514 | * To add a 1:N resource reference property. |
||
| 515 | * |
||
| 516 | * @param ResourceType $sourceResourceType The resource type to add the resource |
||
| 517 | * reference property from |
||
| 518 | * @param ResourceType $targetResourceType The resource type to add the resource |
||
| 519 | * reference property to |
||
| 520 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 521 | * @param string $targetProperty The name of the property to add, on target type |
||
| 522 | */ |
||
| 523 | public function addResourceReferencePropertyBidirectional( |
||
| 538 | |||
| 539 | /** |
||
| 540 | * To add a navigation property (resource set or resource reference) |
||
| 541 | * to a resource type. |
||
| 542 | * |
||
| 543 | * @param ResourceEntityType $sourceResourceType The resource type to add |
||
| 544 | * the resource reference |
||
| 545 | * or resource |
||
| 546 | * reference set property to |
||
| 547 | * @param string $name The name of the |
||
| 548 | * property to add |
||
| 549 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 550 | * resource reference |
||
| 551 | * or reference |
||
| 552 | * set property |
||
| 553 | * points to |
||
| 554 | * @param string $resourceMult The multiplicity of relation being added |
||
| 555 | */ |
||
| 556 | private function _addReferencePropertyInternal( |
||
| 611 | |||
| 612 | /** |
||
| 613 | * To add a navigation property (resource set or resource reference) |
||
| 614 | * to a resource type. |
||
| 615 | * |
||
| 616 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
| 617 | * the resource reference |
||
| 618 | * or resource reference set property to |
||
| 619 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
| 620 | * the resource reference |
||
| 621 | * or resource reference set property to |
||
| 622 | * @param string $sourceProperty The name of the |
||
| 623 | * property to add to source type |
||
| 624 | * @param string $targetProperty The name of the |
||
| 625 | * property to add to target type |
||
| 626 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
| 627 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
| 628 | */ |
||
| 629 | private function _addReferencePropertyInternalBidirectional( |
||
| 718 | |||
| 719 | /** |
||
| 720 | * To add a resource set reference property. |
||
| 721 | * |
||
| 722 | * @param ResourceEntityType $resourceType The resource type to add the |
||
| 723 | * resource reference set property to |
||
| 724 | * @param string $name The name of the property to add |
||
| 725 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 726 | * reference set property points to |
||
| 727 | */ |
||
| 728 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
| 737 | |||
| 738 | /** |
||
| 739 | * To add a M:N resource reference property. |
||
| 740 | * |
||
| 741 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 742 | * reference property from |
||
| 743 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 744 | * reference property to |
||
| 745 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 746 | * @param string $targetProperty The name of the property to add, on target type |
||
| 747 | */ |
||
| 748 | public function addResourceSetReferencePropertyBidirectional( |
||
| 763 | |||
| 764 | /** |
||
| 765 | * To add a 1-1 resource reference. |
||
| 766 | * |
||
| 767 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 768 | * reference property from |
||
| 769 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 770 | * reference property to |
||
| 771 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 772 | * @param string $targetProperty The name of the property to add, on target type |
||
| 773 | */ |
||
| 774 | public function addResourceReferenceSinglePropertyBidirectional( |
||
| 789 | |||
| 790 | /** |
||
| 791 | * To add a complex property to entity or complex type. |
||
| 792 | * |
||
| 793 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
| 794 | * @param string $name name of the complex property |
||
| 795 | * @param ResourceComplexType $complexResourceType complex resource type |
||
| 796 | * @param bool $isBag complex type is bag or not |
||
| 797 | * |
||
| 798 | * @return ResourceProperty |
||
| 799 | */ |
||
| 800 | public function addComplexProperty( |
||
| 831 | |||
| 832 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
| 852 | |||
| 853 | public function getSingletons() |
||
| 857 | |||
| 858 | public function callSingleton($name) |
||
| 867 | } |
||
| 868 |
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: