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 | private $baseTypes = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $containerName container name for the datasource |
||
| 33 | * @param string $namespaceName namespace for the datasource |
||
| 34 | */ |
||
| 35 | public function __construct($containerName, $namespaceName) |
||
| 41 | |||
| 42 | //Begin Implementation of IMetadataProvider |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return mixed |
||
| 46 | */ |
||
| 47 | public function getXML() |
||
| 51 | |||
| 52 | /* |
||
| 53 | * @return MetadataManager |
||
| 54 | */ |
||
| 55 | public function getMetadataManager() |
||
| 56 | { |
||
| 57 | return $this->metadataManager; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * get the Container name for the data source. |
||
| 62 | * |
||
| 63 | * @return string container name |
||
| 64 | */ |
||
| 65 | public function getContainerName() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * get Namespace name for the data source. |
||
| 72 | * |
||
| 73 | * @return string namespace |
||
| 74 | */ |
||
| 75 | public function getContainerNamespace() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * get all entity set information. |
||
| 82 | * |
||
| 83 | * @param null|mixed $params |
||
| 84 | * |
||
| 85 | * @throws \ErrorException |
||
| 86 | * @return ResourceSet[] |
||
| 87 | */ |
||
| 88 | public function getResourceSets($params = null) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * get all resource types in the data source. |
||
| 118 | * |
||
| 119 | * @return ResourceType[] |
||
| 120 | */ |
||
| 121 | public function getTypes() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * get a resource set based on the specified resource set name. |
||
| 128 | * |
||
| 129 | * @param string $name Name of the resource set |
||
| 130 | * |
||
| 131 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
| 132 | */ |
||
| 133 | public function resolveResourceSet($name) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * get a resource type based on the resource type name. |
||
| 143 | * |
||
| 144 | * @param string $name Name of the resource type |
||
| 145 | * |
||
| 146 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
| 147 | */ |
||
| 148 | public function resolveResourceType($name) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * get a singelton based on the specified singleton name. |
||
| 158 | * |
||
| 159 | * @param string $name Name of the resource set |
||
| 160 | * |
||
| 161 | * @return ResourceFunctionType|null singleton with the given name if found else NULL |
||
| 162 | */ |
||
| 163 | public function resolveSingleton($name) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * get a resource set based on the specified resource association set name. |
||
| 173 | * |
||
| 174 | * @param string $name Name of the resource assocation set |
||
| 175 | * |
||
| 176 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 177 | */ |
||
| 178 | public function resolveAssociationSet($name) |
||
| 185 | |||
| 186 | /* |
||
| 187 | * Get number of association sets hooked up |
||
| 188 | */ |
||
| 189 | public function getAssociationCount() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * The method must return a collection of all the types derived from |
||
| 196 | * $resourceType The collection returned should NOT include the type |
||
| 197 | * passed in as a parameter. |
||
| 198 | * |
||
| 199 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
| 200 | * |
||
| 201 | * @return ResourceType[] |
||
| 202 | */ |
||
| 203 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param ResourceEntityType $resourceType Resource to check for derived resource types |
||
| 216 | * |
||
| 217 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 218 | */ |
||
| 219 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
| 226 | |||
| 227 | //End Implementation of IMetadataProvider |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Gets the ResourceAssociationSet instance for the given source |
||
| 231 | * association end. |
||
| 232 | * |
||
| 233 | * @param ResourceSet $sourceResourceSet Resource set of the source association end |
||
| 234 | * @param ResourceEntityType $sourceResourceType Resource type of the source association end |
||
| 235 | * @param ResourceProperty $targetResourceProperty Resource property of the target association end |
||
| 236 | * |
||
| 237 | * @throws InvalidOperationException |
||
| 238 | * @return ResourceAssociationSet|null |
||
| 239 | */ |
||
| 240 | public function getResourceAssociationSet( |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Add an entity type. |
||
| 283 | * |
||
| 284 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 285 | * @param string $name name of the entity |
||
| 286 | * @param mixed $isAbstract |
||
| 287 | * @param null|mixed $baseType |
||
| 288 | * @throws InvalidOperationException when the name is already in use |
||
| 289 | * @return ResourceEntityType |
||
| 290 | * |
||
| 291 | * @internal param string $namespace namespace of the data source |
||
| 292 | */ |
||
| 293 | public function addEntityType(\ReflectionClass $refClass, $name, $isAbstract = false, $baseType = null) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @param \ReflectionClass $refClass |
||
| 302 | * @param string $name |
||
| 303 | * @param $typeKind |
||
| 304 | * @param mixed $isAbstract |
||
| 305 | * @param null|mixed $baseType |
||
| 306 | * @throws InvalidOperationException |
||
| 307 | * @return ResourceEntityType|ResourceComplexType |
||
| 308 | * @internal param null|string $namespace |
||
| 309 | * @internal param null|ResourceType $baseResourceType |
||
| 310 | */ |
||
| 311 | private function createResourceType( |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add a complex type. |
||
| 354 | * |
||
| 355 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 356 | * @param string $name name of the entity |
||
| 357 | * @throws InvalidOperationException when the name is already in use |
||
| 358 | * @return ResourceComplexType |
||
| 359 | * |
||
| 360 | * @internal param string $namespace namespace of the data source |
||
| 361 | * @internal param ResourceType $baseResourceType base resource type |
||
| 362 | */ |
||
| 363 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $name name of the resource set (now taken from resource type) |
||
| 372 | * @param ResourceEntityType $resourceType resource type |
||
| 373 | * |
||
| 374 | * @throws InvalidOperationException |
||
| 375 | * |
||
| 376 | * @return ResourceSet |
||
| 377 | */ |
||
| 378 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
| 397 | * |
||
| 398 | * @param ResourceType $resourceType resource type to which key property |
||
| 399 | * is to be added |
||
| 400 | * @param string $name name of the key property |
||
| 401 | * @param TypeCode $typeCode type of the key property |
||
| 402 | */ |
||
| 403 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 410 | * |
||
| 411 | * @param ResourceType $resourceType Resource type |
||
| 412 | * @param string $name name of the property |
||
| 413 | * @param TypeCode $typeCode type of property |
||
| 414 | * @param bool $isKey property is key or not |
||
| 415 | * @param bool $isBag property is bag or not |
||
| 416 | * @param bool $isETagProperty property is etag or not |
||
| 417 | * @param null|mixed $defaultValue |
||
| 418 | * @param mixed $nullable |
||
| 419 | * |
||
| 420 | * @throws InvalidOperationException |
||
| 421 | */ |
||
| 422 | private function addPrimitivePropertyInternal( |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @param string $name |
||
| 474 | * @param ResourceType $resourceType |
||
| 475 | * |
||
| 476 | * @throws InvalidOperationException |
||
| 477 | */ |
||
| 478 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 498 | |||
| 499 | /** |
||
| 500 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 501 | * |
||
| 502 | * @param ResourceType $resourceType resource type to which key property |
||
| 503 | * is to be added |
||
| 504 | * @param string $name name of the key property |
||
| 505 | * @param TypeCode $typeCode type of the key property |
||
| 506 | * @param bool $isBag property is bag or not |
||
| 507 | * @param null|mixed $defaultValue |
||
| 508 | * @param mixed $nullable |
||
| 509 | */ |
||
| 510 | View Code Duplication | public function addPrimitiveProperty( |
|
| 529 | |||
| 530 | /** |
||
| 531 | * To add a non-key etag property. |
||
| 532 | * |
||
| 533 | * @param ResourceType $resourceType resource type to which key property |
||
| 534 | * is to be added |
||
| 535 | * @param string $name name of the property |
||
| 536 | * @param TypeCode $typeCode type of the etag property |
||
| 537 | * @param null|mixed $defaultValue |
||
| 538 | * @param mixed $nullable |
||
| 539 | */ |
||
| 540 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
| 553 | |||
| 554 | /** |
||
| 555 | * To add a resource reference property. |
||
| 556 | * |
||
| 557 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
| 558 | * reference property to |
||
| 559 | * @param string $name The name of the property to add |
||
| 560 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 561 | * property points to |
||
| 562 | */ |
||
| 563 | public function addResourceReferenceProperty( |
||
| 578 | |||
| 579 | /** |
||
| 580 | * To add a 1:N resource reference property. |
||
| 581 | * |
||
| 582 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 583 | * reference property from |
||
| 584 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 585 | * reference property to |
||
| 586 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 587 | * @param string $targetProperty The name of the property to add, on target type |
||
| 588 | */ |
||
| 589 | View Code Duplication | public function addResourceReferencePropertyBidirectional( |
|
| 615 | |||
| 616 | /** |
||
| 617 | * To add a navigation property (resource set or resource reference) |
||
| 618 | * to a resource type. |
||
| 619 | * |
||
| 620 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource reference |
||
| 621 | * or resource reference set property to |
||
| 622 | * @param string $name The name of the property to add |
||
| 623 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 624 | * resource reference or reference |
||
| 625 | * set property points to |
||
| 626 | * @param string $resourceMult The multiplicity of relation being added |
||
| 627 | * |
||
| 628 | * @throws InvalidOperationException |
||
| 629 | */ |
||
| 630 | private function addReferencePropertyInternal( |
||
| 685 | |||
| 686 | /** |
||
| 687 | * To add a navigation property (resource set or resource reference) |
||
| 688 | * to a resource type. |
||
| 689 | * |
||
| 690 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
| 691 | * the resource reference |
||
| 692 | * or resource reference set property to |
||
| 693 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
| 694 | * the resource reference |
||
| 695 | * or resource reference set property to |
||
| 696 | * @param string $sourceProperty The name of the |
||
| 697 | * property to add to source type |
||
| 698 | * @param string $targetProperty The name of the |
||
| 699 | * property to add to target type |
||
| 700 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
| 701 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
| 702 | * |
||
| 703 | * @throws InvalidOperationException |
||
| 704 | */ |
||
| 705 | private function addReferencePropertyInternalBidirectional( |
||
| 798 | |||
| 799 | /** |
||
| 800 | * To add a resource set reference property. |
||
| 801 | * |
||
| 802 | * @param ResourceEntityType $resourceType The resource type to add the |
||
| 803 | * resource reference set property to |
||
| 804 | * @param string $name The name of the property to add |
||
| 805 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 806 | * reference set property points to |
||
| 807 | */ |
||
| 808 | public function addResourceSetReferenceProperty(ResourceEntityType $resourceType, $name, $targetResourceSet) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * To add a M:N resource reference property. |
||
| 820 | * |
||
| 821 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 822 | * reference property from |
||
| 823 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 824 | * reference property to |
||
| 825 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 826 | * @param string $targetProperty The name of the property to add, on target type |
||
| 827 | */ |
||
| 828 | View Code Duplication | public function addResourceSetReferencePropertyBidirectional( |
|
| 854 | |||
| 855 | /** |
||
| 856 | * To add a 1-1 resource reference. |
||
| 857 | * |
||
| 858 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 859 | * reference property from |
||
| 860 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 861 | * reference property to |
||
| 862 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 863 | * @param string $targetProperty The name of the property to add, on target type |
||
| 864 | */ |
||
| 865 | View Code Duplication | public function addResourceReferenceSinglePropertyBidirectional( |
|
| 891 | |||
| 892 | /** |
||
| 893 | * To add a complex property to entity or complex type. |
||
| 894 | * |
||
| 895 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
| 896 | * @param string $name name of the complex property |
||
| 897 | * @param ResourceComplexType $complexResourceType complex resource type |
||
| 898 | * @param bool $isBag complex type is bag or not |
||
| 899 | * |
||
| 900 | * @throws InvalidOperationException |
||
| 901 | * @return ResourceProperty |
||
| 902 | */ |
||
| 903 | public function addComplexProperty( |
||
| 934 | |||
| 935 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
| 960 | |||
| 961 | public function getSingletons() |
||
| 965 | |||
| 966 | public function callSingleton($name) |
||
| 975 | } |
||
| 976 |