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 ProvidersWrapper 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 ProvidersWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class ProvidersWrapper |
||
| 34 | { |
||
| 35 | /** |
||
| 36 | * Holds reference to IMetadataProvider implementation |
||
| 37 | * |
||
| 38 | * @var IMetadataProvider |
||
| 39 | */ |
||
| 40 | private $metaProvider; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Holds reference to IQueryProvider implementation |
||
| 44 | * |
||
| 45 | * @var IQueryProvider |
||
| 46 | * |
||
| 47 | */ |
||
| 48 | private $queryProvider; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Holds reference to IServiceConfiguration implementation |
||
| 52 | * |
||
| 53 | * @var ServiceConfiguration |
||
| 54 | */ |
||
| 55 | private $config; |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Cache for ResourceProperties of a resource type that belongs to a |
||
| 60 | * resource set. An entry (ResourceProperty collection) in this cache |
||
| 61 | * contains only the visible properties of ResourceType. |
||
| 62 | * |
||
| 63 | * @var array(string, array(string, ResourceProperty)) |
||
| 64 | */ |
||
| 65 | private $propertyCache; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Cache for ResourceSetWrappers. If ResourceSet is invisible value will |
||
| 69 | * be null. |
||
| 70 | * |
||
| 71 | * @var ResourceSetWrapper[] indexed by resource set name |
||
| 72 | */ |
||
| 73 | private $setWrapperCache; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Cache for ResourceTypes |
||
| 77 | * |
||
| 78 | * @var ResourceType[] indexed by resource type name |
||
| 79 | */ |
||
| 80 | private $typeCache; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Cache for ResourceAssociationSet. If ResourceAssociationSet is invisible |
||
| 84 | * value will be null. |
||
| 85 | * |
||
| 86 | * @var ResourceAssociationSet[] indexed by name |
||
| 87 | */ |
||
| 88 | private $associationSetCache; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Creates a new instance of ProvidersWrapper |
||
| 92 | * |
||
| 93 | * @param IMetadataProvider $metadataProvider Reference to IMetadataProvider implementation |
||
| 94 | * @param IQueryProvider $queryProvider Reference to IQueryProvider implementation |
||
| 95 | * @param ServiceConfiguration $configuration Reference to IServiceConfiguration implementation |
||
| 96 | */ |
||
| 97 | public function __construct(IMetadataProvider $metadataProvider, IQueryProvider $queryProvider, ServiceConfiguration $configuration) |
||
| 107 | |||
| 108 | //Wrappers for IMetadataProvider methods |
||
| 109 | |||
| 110 | /** |
||
| 111 | * To get the Container name for the data source, |
||
| 112 | * Note: Wrapper for IMetadataProvider::getContainerName method |
||
| 113 | * implementation |
||
| 114 | * |
||
| 115 | * @return string that contains the name of the container |
||
| 116 | * |
||
| 117 | * @throws ODataException Exception if implementation returns empty container name |
||
| 118 | * |
||
| 119 | */ |
||
| 120 | public function getContainerName() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * To get Namespace name for the data source, |
||
| 135 | * Note: Wrapper for IMetadataProvider::getContainerNamespace method implementation |
||
| 136 | * |
||
| 137 | * @return string that contains the namespace name. |
||
| 138 | * |
||
| 139 | * @throws ODataException Exception if implementation returns empty container namespace |
||
| 140 | * |
||
| 141 | */ |
||
| 142 | public function getContainerNamespace() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * To get the data service configuration |
||
| 157 | * |
||
| 158 | * @return ServiceConfiguration |
||
| 159 | */ |
||
| 160 | public function getConfiguration() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * To get all entity set information, |
||
| 167 | * Note: Wrapper for IMetadataProvider::getResourceSets method implementation, |
||
| 168 | * This method returns array of ResourceSetWrapper instances but the corresponding IDSMP method returns array of ResourceSet instances |
||
| 169 | * |
||
| 170 | * @return ResourceSetWrapper[] The ResourceSetWrappers for the visible ResourceSets |
||
| 171 | * @throws ODataException when two resource sets with the same name are encountered |
||
| 172 | * |
||
| 173 | */ |
||
| 174 | public function getResourceSets() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * To get all resource types in the data source, |
||
| 197 | * Note: Wrapper for IMetadataProvider::getTypes method implementation |
||
| 198 | * |
||
| 199 | * @return ResourceType[] |
||
| 200 | */ |
||
| 201 | public function getTypes() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * To get a resource set based on the specified resource set name which is |
||
| 222 | * visible, |
||
| 223 | * Note: Wrapper for IMetadataProvider::resolveResourceSet method |
||
| 224 | * implementation |
||
| 225 | * |
||
| 226 | * @param string $name Name of the resource set |
||
| 227 | * |
||
| 228 | * @return ResourceSetWrapper|null Returns resource set with the given name if found, NULL if resource set is set to invisible or not found |
||
| 229 | * |
||
| 230 | */ |
||
| 231 | public function resolveResourceSet($name) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * To get a resource type based on the resource set name, |
||
| 247 | * Note: Wrapper for IMetadataProvider::resolveResourceType |
||
| 248 | * method implementation |
||
| 249 | * |
||
| 250 | * @param string $name Name of the resource set |
||
| 251 | * |
||
| 252 | * @return ResourceType|null resource type with the given resource set name if found else NULL |
||
| 253 | * |
||
| 254 | * |
||
| 255 | * @throws ODataException If the ResourceType is invalid |
||
| 256 | */ |
||
| 257 | public function resolveResourceType($name) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * The method must return a collection of all the types derived from |
||
| 269 | * $resourceType The collection returned should NOT include the type |
||
| 270 | * passed in as a parameter |
||
| 271 | * Note: Wrapper for IMetadataProvider::getDerivedTypes |
||
| 272 | * method implementation |
||
| 273 | * |
||
| 274 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
| 275 | * |
||
| 276 | * @return ResourceType[] |
||
| 277 | * |
||
| 278 | * @throws InvalidOperationException when the meat provider doesn't return an array |
||
| 279 | */ |
||
| 280 | public function getDerivedTypes(ResourceType $resourceType) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Returns true if $resourceType represents an Entity Type which has derived |
||
| 296 | * Entity Types, else false. |
||
| 297 | * Note: Wrapper for IMetadataProvider::hasDerivedTypes method |
||
| 298 | * implementation |
||
| 299 | * |
||
| 300 | * @param ResourceType $resourceType Resource to check for derived resource |
||
| 301 | * types. |
||
| 302 | * |
||
| 303 | * @return boolean |
||
| 304 | * |
||
| 305 | * @throws ODataException If the ResourceType is invalid |
||
| 306 | */ |
||
| 307 | public function hasDerivedTypes(ResourceType $resourceType) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Gets the ResourceAssociationSet instance for the given source association end, |
||
| 315 | * Note: Wrapper for IMetadataProvider::getResourceAssociationSet |
||
| 316 | * method implementation |
||
| 317 | * |
||
| 318 | * @param ResourceSet $set Resource set of the source association end |
||
| 319 | * @param ResourceType $type Resource type of the source association end |
||
| 320 | * @param ResourceProperty $property Resource property of the source association end |
||
| 321 | * |
||
| 322 | * |
||
| 323 | * @return ResourceAssociationSet|null Returns ResourceAssociationSet for the source |
||
| 324 | * association end, NULL if no such |
||
| 325 | * association end or resource set in the |
||
| 326 | * other end of the association is invisible |
||
| 327 | */ |
||
| 328 | public function getResourceAssociationSet( |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Gets the target resource set wrapper for the given navigation property, |
||
| 395 | * source resource set wrapper and the source resource type |
||
| 396 | * |
||
| 397 | * @param ResourceSetWrapper $resourceSetWrapper Source resource set. |
||
| 398 | * @param ResourceType $resourceType Source resource type. |
||
| 399 | * @param ResourceProperty $navigationResourceProperty Navigation property. |
||
| 400 | * |
||
| 401 | * @return ResourceSetWrapper|null Returns instance of ResourceSetWrapper |
||
| 402 | * (describes the entity set and associated configuration) for the |
||
| 403 | * given navigation property. returns NULL if resourceset for the |
||
| 404 | * navigation property is invisible or if metadata provider returns |
||
| 405 | * null resource association set |
||
| 406 | */ |
||
| 407 | public function getResourceSetWrapperForNavigationProperty( |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Gets the visible resource properties for the given resource type from the given resource set wrapper. |
||
| 434 | * |
||
| 435 | * @param ResourceSetWrapper $setWrapper Resource set wrapper in question. |
||
| 436 | * @param ResourceType $resourceType Resource type in question. |
||
| 437 | * @return ResourceProperty[] Collection of visible resource properties from the given resource set wrapper and resource type. |
||
| 438 | */ |
||
| 439 | public function getResourceProperties(ResourceSetWrapper $setWrapper, ResourceType $resourceType) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Wrapper function over _validateResourceSetAndGetWrapper function |
||
| 468 | * |
||
| 469 | * @param ResourceSet $resourceSet see the comments of _validateResourceSetAndGetWrapper |
||
| 470 | * |
||
| 471 | * @return ResourceSetWrapper|null see the comments of _validateResourceSetAndGetWrapper |
||
| 472 | */ |
||
| 473 | public function validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Gets the Edm Schema version compliance to the metadata |
||
| 480 | * |
||
| 481 | * @return EdmSchemaVersion |
||
| 482 | */ |
||
| 483 | public function getEdmSchemaVersion() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * This function perform the following operations |
||
| 491 | * (1) If the cache contain an entry [key, value] for the resourceset then |
||
| 492 | * return the entry-value |
||
| 493 | * (2) If the cache not contain an entry for the resourceset then validate |
||
| 494 | * the resourceset |
||
| 495 | * (a) If valid add entry as [resouceset_name, resourceSetWrapper] |
||
| 496 | * (b) if not valid add entry as [resouceset_name, null] |
||
| 497 | * Note: validating a resourceset means checking the resourceset is visible |
||
| 498 | * or not using configuration |
||
| 499 | * |
||
| 500 | * @param ResourceSet $resourceSet The resourceset to validate and get the |
||
| 501 | * wrapper for |
||
| 502 | * |
||
| 503 | * @return ResourceSetWrapper|null Returns an instance if a resource set with the given name is visible |
||
| 504 | */ |
||
| 505 | private function _validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Validates the given instance of ResourceType |
||
| 525 | * |
||
| 526 | * @param ResourceType $resourceType The ResourceType to validate |
||
| 527 | * |
||
| 528 | * @return ResourceType |
||
| 529 | * |
||
| 530 | * @throws ODataException Exception if $resourceType is invalid |
||
| 531 | */ |
||
| 532 | private function _validateResourceType(ResourceType $resourceType) |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Gets the resource type on which the resource property is declared on, |
||
| 546 | * If property is not declared in the given resource type, then this |
||
| 547 | * function drill down to the inheritance hierarchy of the given resource |
||
| 548 | * type to find out the base class in which the property is declared |
||
| 549 | * |
||
| 550 | * @param ResourceType $resourceType The resource type to start looking |
||
| 551 | * @param ResourceProperty $resourceProperty The resource property in question |
||
| 552 | * |
||
| 553 | * @return ResourceType|null Returns reference to the ResourceType on which |
||
| 554 | * the $resourceProperty is declared, NULL if |
||
| 555 | * $resourceProperty is not declared anywhere |
||
| 556 | * in the inheritance hierarchy |
||
| 557 | */ |
||
| 558 | private function _getResourceTypeWherePropertyIsDeclared(ResourceType $resourceType, |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Gets the underlying custom expression provider, the end developer is |
||
| 575 | * responsible for implementing IExpressionProvider if he choose for |
||
| 576 | * |
||
| 577 | * @return IExpressionProvider Instance of IExpressionProvider implementation. |
||
| 578 | * |
||
| 579 | */ |
||
| 580 | public function getExpressionProvider() |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
| 597 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
| 598 | * perform the ordering and paging |
||
| 599 | * |
||
| 600 | * @return Boolean True if the query provider can handle ordered paging, false if POData should perform the paging |
||
| 601 | */ |
||
| 602 | public function handlesOrderedPaging() |
||
| 606 | |||
| 607 | |||
| 608 | private function ValidateQueryResult($queryResult, QueryType $queryType, $methodName){ |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Gets collection of entities belongs to an entity set |
||
| 640 | * |
||
| 641 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 642 | * @param ResourceSet $resourceSet The entity set containing the entities that need to be fetched |
||
| 643 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 644 | * @param InternalOrderByInfo $orderBy The orderBy information |
||
| 645 | * @param int $top The top count |
||
| 646 | * @param int $skip The skip count |
||
| 647 | * |
||
| 648 | * @return QueryResult |
||
| 649 | */ |
||
| 650 | public function getResourceSet(QueryType $queryType, ResourceSet $resourceSet, $filterInfo, $orderBy, $top, $skip) |
||
| 666 | |||
| 667 | |||
| 668 | |||
| 669 | /** |
||
| 670 | * Gets an entity instance from an entity set identified by a key |
||
| 671 | * |
||
| 672 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
| 673 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
| 674 | * |
||
| 675 | * @return object|null Returns entity instance if found else null |
||
| 676 | */ |
||
| 677 | public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Get related resource set for a resource |
||
| 691 | * |
||
| 692 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
| 693 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
| 694 | * @param object $sourceEntity The source entity instance. |
||
| 695 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
| 696 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
| 697 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
| 698 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
| 699 | * @param int $top number of records which need to be skip |
||
| 700 | * @param String $skip value indicating what records to skip |
||
| 701 | * |
||
| 702 | * @return QueryResult |
||
| 703 | * |
||
| 704 | * @throws ODataException |
||
| 705 | */ |
||
| 706 | public function getRelatedResourceSet( |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Gets a related entity instance from an entity set identified by a key |
||
| 739 | * |
||
| 740 | * @param ResourceSet $sourceResourceSet The entity set related to the entity to be fetched. |
||
| 741 | * @param object $sourceEntity The related entity instance. |
||
| 742 | * @param ResourceSet $targetResourceSet The entity set from which entity needs to be fetched. |
||
| 743 | * @param ResourceProperty $targetProperty The metadata of the target property. |
||
| 744 | * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched. |
||
| 745 | * |
||
| 746 | * |
||
| 747 | * @return object|null Returns entity instance if found else null |
||
| 748 | */ |
||
| 749 | public function getResourceFromRelatedResourceSet(ResourceSet $sourceResourceSet, |
||
| 768 | |||
| 769 | /** |
||
| 770 | * Get related resource for a resource |
||
| 771 | * |
||
| 772 | * @param ResourceSet $sourceResourceSet The source resource set |
||
| 773 | * @param object $sourceEntity The source resource |
||
| 774 | * @param ResourceSet $targetResourceSet The resource set of the navigation |
||
| 775 | * property |
||
| 776 | * @param ResourceProperty $targetProperty The navigation property to be |
||
| 777 | * retrieved |
||
| 778 | * |
||
| 779 | * @return object|null The related resource if exists else null |
||
| 780 | */ |
||
| 781 | public function getRelatedResourceReference(ResourceSet $sourceResourceSet, |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Validate the given entity instance. |
||
| 842 | * |
||
| 843 | * @param object $entityInstance Entity instance to validate |
||
| 844 | * @param ResourceSet &$resourceSet Resource set to which the entity |
||
| 845 | * instance belongs to. |
||
| 846 | * @param KeyDescriptor &$keyDescriptor The key descriptor. |
||
| 847 | * @param string $methodName Method from which this function |
||
| 848 | * invoked. |
||
| 849 | * |
||
| 850 | * @return void |
||
| 851 | * |
||
| 852 | * @throws ODataException |
||
| 853 | */ |
||
| 854 | private function _validateEntityInstance($entityInstance, |
||
| 902 | |||
| 903 | /** |
||
| 904 | * Assert that the given condition is true. |
||
| 905 | * |
||
| 906 | * @param boolean $condition Condition to be asserted. |
||
| 907 | * @param string $conditionAsString String containing message incase |
||
| 908 | * if assertion fails. |
||
| 909 | * |
||
| 910 | * @throws InvalidOperationException Incase if assertion fails. |
||
| 911 | * |
||
| 912 | * @return void |
||
| 913 | */ |
||
| 914 | protected function assert($condition, $conditionAsString) |
||
| 920 | } |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: