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 |
||
| 16 | class SimpleMetadataProvider implements IMetadataProvider |
||
| 17 | { |
||
| 18 | public $oDataEntityMap = []; |
||
| 19 | protected $resourceSets = []; |
||
| 20 | protected $resourceTypes = []; |
||
| 21 | protected $associationSets = []; |
||
| 22 | protected $containerName; |
||
| 23 | protected $namespaceName; |
||
| 24 | private $metadataManager; |
||
| 25 | private $typeSetMapping = []; |
||
| 26 | protected $singletons = []; |
||
| 27 | private $baseTypes = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $containerName container name for the datasource |
||
| 31 | * @param string $namespaceName namespace for the datasource |
||
| 32 | */ |
||
| 33 | public function __construct($containerName, $namespaceName) |
||
| 39 | |||
| 40 | //Begin Implementation of IMetadataProvider |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return string|null |
||
| 44 | */ |
||
| 45 | public function getXML() |
||
| 49 | |||
| 50 | /* |
||
| 51 | * @return MetadataManager |
||
| 52 | */ |
||
| 53 | public function getMetadataManager() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * get the Container name for the data source. |
||
| 60 | * |
||
| 61 | * @return string container name |
||
| 62 | */ |
||
| 63 | public function getContainerName() |
||
| 67 | |||
| 68 | /** |
||
| 69 | * get Namespace name for the data source. |
||
| 70 | * |
||
| 71 | * @return string namespace |
||
| 72 | */ |
||
| 73 | public function getContainerNamespace() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * get all entity set information. |
||
| 80 | * |
||
| 81 | * @param null|mixed $params |
||
| 82 | * |
||
| 83 | * @throws \ErrorException |
||
| 84 | * @return ResourceSet[] |
||
| 85 | */ |
||
| 86 | public function getResourceSets($params = null) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * get all resource types in the data source. |
||
| 116 | * |
||
| 117 | * @return ResourceType[] |
||
| 118 | */ |
||
| 119 | public function getTypes() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * get a resource set based on the specified resource set name. |
||
| 126 | * |
||
| 127 | * @param string $name Name of the resource set |
||
| 128 | * |
||
| 129 | * @return ResourceSet|null resource set with the given name if found else NULL |
||
| 130 | */ |
||
| 131 | public function resolveResourceSet($name) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * get a resource type based on the resource type name. |
||
| 141 | * |
||
| 142 | * @param string $name Name of the resource type |
||
| 143 | * |
||
| 144 | * @return ResourceType|null resource type with the given resource type name if found else NULL |
||
| 145 | */ |
||
| 146 | public function resolveResourceType($name) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * get a singelton based on the specified singleton name. |
||
| 156 | * |
||
| 157 | * @param string $name Name of the resource set |
||
| 158 | * |
||
| 159 | * @return ResourceFunctionType|null singleton with the given name if found else NULL |
||
| 160 | */ |
||
| 161 | public function resolveSingleton($name) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * get a resource set based on the specified resource association set name. |
||
| 171 | * |
||
| 172 | * @param string $name Name of the resource assocation set |
||
| 173 | * |
||
| 174 | * @return ResourceAssociationSet|null resource association set with the given name if found else NULL |
||
| 175 | */ |
||
| 176 | public function resolveAssociationSet($name) |
||
| 183 | |||
| 184 | /* |
||
| 185 | * Get number of association sets hooked up |
||
| 186 | */ |
||
| 187 | public function getAssociationCount() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * The method must return a collection of all the types derived from |
||
| 194 | * $resourceType The collection returned should NOT include the type |
||
| 195 | * passed in as a parameter. |
||
| 196 | * |
||
| 197 | * @param ResourceEntityType $resourceType Resource to get derived resource types from |
||
| 198 | * |
||
| 199 | * @return ResourceType[] |
||
| 200 | */ |
||
| 201 | public function getDerivedTypes(ResourceEntityType $resourceType) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param ResourceEntityType $resourceType Resource to check for derived resource types |
||
| 214 | * |
||
| 215 | * @return bool true if $resourceType represents an Entity Type which has derived Entity Types, else false |
||
| 216 | */ |
||
| 217 | public function hasDerivedTypes(ResourceEntityType $resourceType) |
||
| 224 | |||
| 225 | //End Implementation of IMetadataProvider |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Gets the ResourceAssociationSet instance for the given source |
||
| 229 | * association end. |
||
| 230 | * |
||
| 231 | * @param ResourceSet $sourceResourceSet Resource set of the source association end |
||
| 232 | * @param ResourceEntityType $sourceResourceType Resource type of the source association end |
||
| 233 | * @param ResourceProperty $targetResourceProperty Resource property of the target association end |
||
| 234 | * |
||
| 235 | * @throws InvalidOperationException |
||
| 236 | * @return ResourceAssociationSet|null |
||
| 237 | */ |
||
| 238 | public function getResourceAssociationSet( |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Add an entity type. |
||
| 281 | * |
||
| 282 | * @param \ReflectionClass $refClass reflection class of the entity |
||
| 283 | * @param string $name name of the entity |
||
| 284 | * @param mixed $isAbstract |
||
| 285 | * @param null|mixed $baseType |
||
| 286 | * @throws InvalidOperationException when the name is already in use |
||
| 287 | * @return ResourceEntityType |
||
| 288 | * |
||
| 289 | * @internal param string $namespace namespace of the data source |
||
| 290 | */ |
||
| 291 | public function addEntityType(\ReflectionClass $refClass, $name, $isAbstract = false, $baseType = null) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param \ReflectionClass $refClass |
||
| 300 | * @param string $name |
||
| 301 | * @param $typeKind |
||
| 302 | * @param mixed $isAbstract |
||
| 303 | * @param null|mixed $baseType |
||
| 304 | * @throws InvalidOperationException |
||
| 305 | * @return ResourceEntityType|ResourceComplexType |
||
| 306 | * @internal param null|string $namespace |
||
| 307 | * @internal param null|ResourceType $baseResourceType |
||
| 308 | */ |
||
| 309 | private function createResourceType( |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Add a complex type. |
||
| 352 | * |
||
| 353 | * @param \ReflectionClass $refClass reflection class of the complex entity type |
||
| 354 | * @param string $name name of the entity |
||
| 355 | * @throws InvalidOperationException when the name is already in use |
||
| 356 | * @return ResourceComplexType |
||
| 357 | * |
||
| 358 | * @internal param string $namespace namespace of the data source |
||
| 359 | * @internal param ResourceType $baseResourceType base resource type |
||
| 360 | */ |
||
| 361 | public function addComplexType(\ReflectionClass $refClass, $name) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param string $name name of the resource set (now taken from resource type) |
||
| 370 | * @param ResourceEntityType $resourceType resource type |
||
| 371 | * |
||
| 372 | * @throws InvalidOperationException |
||
| 373 | * |
||
| 374 | * @return ResourceSet |
||
| 375 | */ |
||
| 376 | public function addResourceSet($name, ResourceEntityType $resourceType) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * To add a Key-primitive property to a resource (Complex/Entity). |
||
| 395 | * |
||
| 396 | * @param ResourceType $resourceType resource type to which key property |
||
| 397 | * is to be added |
||
| 398 | * @param string $name name of the key property |
||
| 399 | * @param TypeCode $typeCode type of the key property |
||
| 400 | */ |
||
| 401 | public function addKeyProperty($resourceType, $name, $typeCode) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * To add a Key/NonKey-primitive property to a resource (complex/entity). |
||
| 408 | * |
||
| 409 | * @param ResourceType $resourceType Resource type |
||
| 410 | * @param string $name name of the property |
||
| 411 | * @param TypeCode $typeCode type of property |
||
| 412 | * @param bool $isKey property is key or not |
||
| 413 | * @param bool $isBag property is bag or not |
||
| 414 | * @param bool $isETagProperty property is etag or not |
||
| 415 | * @param null|mixed $defaultValue |
||
| 416 | * @param mixed $nullable |
||
| 417 | * |
||
| 418 | * @throws InvalidOperationException |
||
| 419 | */ |
||
| 420 | private function addPrimitivePropertyInternal( |
||
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $name |
||
| 472 | * @param ResourceType $resourceType |
||
| 473 | * |
||
| 474 | * @throws InvalidOperationException |
||
| 475 | */ |
||
| 476 | private function checkInstanceProperty($name, ResourceType $resourceType) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * To add a NonKey-primitive property (Complex/Entity). |
||
| 499 | * |
||
| 500 | * @param ResourceType $resourceType resource type to which key property |
||
| 501 | * is to be added |
||
| 502 | * @param string $name name of the key property |
||
| 503 | * @param TypeCode $typeCode type of the key property |
||
| 504 | * @param bool $isBag property is bag or not |
||
| 505 | * @param null|mixed $defaultValue |
||
| 506 | * @param mixed $nullable |
||
| 507 | */ |
||
| 508 | View Code Duplication | public function addPrimitiveProperty( |
|
| 527 | |||
| 528 | /** |
||
| 529 | * To add a non-key etag property. |
||
| 530 | * |
||
| 531 | * @param ResourceType $resourceType resource type to which key property |
||
| 532 | * is to be added |
||
| 533 | * @param string $name name of the property |
||
| 534 | * @param TypeCode $typeCode type of the etag property |
||
| 535 | * @param null|mixed $defaultValue |
||
| 536 | * @param mixed $nullable |
||
| 537 | */ |
||
| 538 | View Code Duplication | public function addETagProperty($resourceType, $name, $typeCode, $defaultValue = null, $nullable = false) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * To add a resource reference property. |
||
| 554 | * |
||
| 555 | * @param ResourceEntityType $resourceType The resource type to add the resource |
||
| 556 | * reference property to |
||
| 557 | * @param string $name The name of the property to add |
||
| 558 | * @param ResourceSet $targetResourceSet The resource set the resource reference |
||
| 559 | * property points to |
||
| 560 | * @param mixed $flip |
||
| 561 | * @param mixed $many |
||
| 562 | * @param ResourceEntityType|null $concreteType Underlying concrete resource reference type, if set |
||
| 563 | */ |
||
| 564 | public function addResourceReferenceProperty( |
||
| 581 | |||
| 582 | /** |
||
| 583 | * To add a 1:N resource reference property. |
||
| 584 | * |
||
| 585 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 586 | * reference property from |
||
| 587 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 588 | * reference property to |
||
| 589 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 590 | * @param string $targetProperty The name of the property to add, on target type |
||
| 591 | */ |
||
| 592 | View Code Duplication | public function addResourceReferencePropertyBidirectional( |
|
| 618 | |||
| 619 | /** |
||
| 620 | * To add a navigation property (resource set or resource reference) |
||
| 621 | * to a resource type. |
||
| 622 | * |
||
| 623 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource reference |
||
| 624 | * or resource reference set property to |
||
| 625 | * @param string $name The name of the property to add |
||
| 626 | * @param ResourceSet $targetResourceSet The resource set the |
||
| 627 | * resource reference or reference |
||
| 628 | * set property points to |
||
| 629 | * @param string $resourceMult The multiplicity of relation being added |
||
| 630 | * @param mixed $many |
||
| 631 | * |
||
| 632 | * @throws InvalidOperationException |
||
| 633 | */ |
||
| 634 | private function addReferencePropertyInternal( |
||
| 697 | |||
| 698 | /** |
||
| 699 | * To add a navigation property (resource set or resource reference) |
||
| 700 | * to a resource type. |
||
| 701 | * |
||
| 702 | * @param ResourceEntityType $sourceResourceType The source resource type to add |
||
| 703 | * the resource reference |
||
| 704 | * or resource reference set property to |
||
| 705 | * @param ResourceEntityType $targetResourceType The target resource type to add |
||
| 706 | * the resource reference |
||
| 707 | * or resource reference set property to |
||
| 708 | * @param string $sourceProperty The name of the |
||
| 709 | * property to add to source type |
||
| 710 | * @param string $targetProperty The name of the |
||
| 711 | * property to add to target type |
||
| 712 | * @param string $sourceMultiplicity The multiplicity at the source end of relation |
||
| 713 | * @param string $targetMultiplicity The multiplicity at the target end of relation |
||
| 714 | * |
||
| 715 | * @throws InvalidOperationException |
||
| 716 | */ |
||
| 717 | private function addReferencePropertyInternalBidirectional( |
||
| 810 | |||
| 811 | /** |
||
| 812 | * To add a resource set reference property. |
||
| 813 | * |
||
| 814 | * @param ResourceEntityType $resourceType The resource type to add the |
||
| 815 | * resource reference set property to |
||
| 816 | * @param string $name The name of the property to add |
||
| 817 | * @param ResourceSet $targetResourceSet The resource set the resource |
||
| 818 | * reference set property points to |
||
| 819 | * @param ResourceEntityType|null $concreteType Underlying concrete resource type, if set |
||
| 820 | */ |
||
| 821 | public function addResourceSetReferenceProperty( |
||
| 837 | |||
| 838 | /** |
||
| 839 | * To add a M:N resource reference property. |
||
| 840 | * |
||
| 841 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 842 | * reference property from |
||
| 843 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 844 | * reference property to |
||
| 845 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 846 | * @param string $targetProperty The name of the property to add, on target type |
||
| 847 | */ |
||
| 848 | View Code Duplication | public function addResourceSetReferencePropertyBidirectional( |
|
| 874 | |||
| 875 | /** |
||
| 876 | * To add a 1-1 resource reference. |
||
| 877 | * |
||
| 878 | * @param ResourceEntityType $sourceResourceType The resource type to add the resource |
||
| 879 | * reference property from |
||
| 880 | * @param ResourceEntityType $targetResourceType The resource type to add the resource |
||
| 881 | * reference property to |
||
| 882 | * @param string $sourceProperty The name of the property to add, on source type |
||
| 883 | * @param string $targetProperty The name of the property to add, on target type |
||
| 884 | */ |
||
| 885 | View Code Duplication | public function addResourceReferenceSinglePropertyBidirectional( |
|
| 911 | |||
| 912 | /** |
||
| 913 | * To add a complex property to entity or complex type. |
||
| 914 | * |
||
| 915 | * @param ResourceType $targetResourceType The resource type to which the complex property needs to add |
||
| 916 | * @param string $name name of the complex property |
||
| 917 | * @param ResourceComplexType $complexResourceType complex resource type |
||
| 918 | * @param bool $isBag complex type is bag or not |
||
| 919 | * |
||
| 920 | * @throws InvalidOperationException |
||
| 921 | * @return ResourceProperty |
||
| 922 | */ |
||
| 923 | public function addComplexProperty( |
||
| 954 | |||
| 955 | public function createSingleton($name, ResourceType $returnType, $functionName) |
||
| 980 | |||
| 981 | public function getSingletons() |
||
| 985 | |||
| 986 | public function callSingleton($name) |
||
| 995 | } |
||
| 996 |