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 |
||
34 | class ProvidersWrapper |
||
35 | { |
||
36 | /** |
||
37 | * Holds reference to IMetadataProvider implementation. |
||
38 | * |
||
39 | * @var IMetadataProvider |
||
40 | */ |
||
41 | private $metaProvider; |
||
42 | |||
43 | /** |
||
44 | * Holds reference to IServiceConfiguration implementation. |
||
45 | * |
||
46 | * @var IServiceConfiguration |
||
47 | */ |
||
48 | private $config; |
||
49 | |||
50 | /* |
||
51 | * Holds reference to ProvidersQueryWrapper implementation |
||
52 | * |
||
53 | * @var ProvidersQueryWrapper |
||
54 | */ |
||
55 | private $providerWrapper; |
||
56 | |||
57 | /** |
||
58 | * Cache for ResourceProperties of a resource type that belongs to a |
||
59 | * resource set. An entry (ResourceProperty collection) in this cache |
||
60 | * contains only the visible properties of ResourceType. |
||
61 | * |
||
62 | * @var array(string, array(string, ResourceProperty)) |
||
63 | */ |
||
64 | private $propertyCache; |
||
65 | |||
66 | /** |
||
67 | * Cache for ResourceSetWrappers. If ResourceSet is invisible value will |
||
68 | * be null. |
||
69 | * |
||
70 | * @var ResourceSetWrapper[] indexed by resource set name |
||
71 | */ |
||
72 | private $setWrapperCache; |
||
73 | |||
74 | /** |
||
75 | * Cache for ResourceTypes. |
||
76 | * |
||
77 | * @var ResourceType[] indexed by resource type name |
||
78 | */ |
||
79 | private $typeCache; |
||
80 | |||
81 | /** |
||
82 | * Cache for ResourceAssociationSet. If ResourceAssociationSet is invisible |
||
83 | * value will be null. |
||
84 | * |
||
85 | * @var ResourceAssociationSet[] indexed by name |
||
86 | */ |
||
87 | private $associationSetCache; |
||
|
|||
88 | |||
89 | /** |
||
90 | * Creates a new instance of ProvidersWrapper. |
||
91 | * |
||
92 | * @param IMetadataProvider $meta Reference to IMetadataProvider implementation |
||
93 | * @param IQueryProvider $query Reference to IQueryProvider implementation |
||
94 | * @param IServiceConfiguration $config Reference to IServiceConfiguration implementation |
||
95 | */ |
||
96 | public function __construct(IMetadataProvider $meta, IQueryProvider $query, IServiceConfiguration $config) |
||
97 | { |
||
98 | $this->metaProvider = $meta; |
||
99 | $this->config = $config; |
||
100 | $this->providerWrapper = new ProvidersQueryWrapper($query); |
||
101 | $this->setWrapperCache = []; |
||
102 | $this->typeCache = []; |
||
103 | $this->propertyCache = []; |
||
104 | } |
||
105 | |||
106 | //Wrappers for IMetadataProvider methods |
||
107 | |||
108 | /** |
||
109 | * To get the Container name for the data source, |
||
110 | * Note: Wrapper for IMetadataProvider::getContainerName method |
||
111 | * implementation. |
||
112 | * |
||
113 | * @throws ODataException Exception if implementation returns empty container name |
||
114 | * |
||
115 | * @return string that contains the name of the container |
||
116 | */ |
||
117 | public function getContainerName() |
||
129 | |||
130 | /** |
||
131 | * To get Namespace name for the data source, |
||
132 | * Note: Wrapper for IMetadataProvider::getContainerNamespace method implementation. |
||
133 | * |
||
134 | * @throws ODataException Exception if implementation returns empty container namespace |
||
135 | * |
||
136 | * @return string that contains the namespace name |
||
137 | */ |
||
138 | public function getContainerNamespace() |
||
150 | |||
151 | /** |
||
152 | * To get the data service configuration. |
||
153 | * |
||
154 | * @return IServiceConfiguration |
||
155 | */ |
||
156 | public function getConfiguration() |
||
160 | |||
161 | /** |
||
162 | * To get all entity set information, |
||
163 | * Note: Wrapper for IMetadataProvider::getResourceSets method implementation, |
||
164 | * This method returns array of ResourceSetWrapper instances but the corresponding IDSMP method |
||
165 | * returns array of ResourceSet instances. |
||
166 | * |
||
167 | * @throws ODataException when two resource sets with the same name are encountered |
||
168 | * |
||
169 | * @return ResourceSetWrapper[] The ResourceSetWrappers for the visible ResourceSets |
||
170 | */ |
||
171 | public function getResourceSets() |
||
191 | |||
192 | /** |
||
193 | * This function perform the following operations |
||
194 | * (1) If the cache contain an entry [key, value] for the resourceset then |
||
195 | * return the entry-value |
||
196 | * (2) If the cache not contain an entry for the resourceset then validate |
||
197 | * the resourceset |
||
198 | * (a) If valid add entry as [resouceset_name, resourceSetWrapper] |
||
199 | * (b) if not valid add entry as [resouceset_name, null] |
||
200 | * Note: validating a resourceset means checking the resourceset is visible |
||
201 | * or not using configuration. |
||
202 | * |
||
203 | * @param ResourceSet $resourceSet The resourceset to validate and get the |
||
204 | * wrapper for |
||
205 | * |
||
206 | * @return ResourceSetWrapper|null Returns an instance if a resource set with the given name is visible |
||
207 | */ |
||
208 | private function _validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
222 | |||
223 | /** |
||
224 | * Validates the given instance of ResourceType. |
||
225 | * |
||
226 | * @param ResourceType $resourceType The ResourceType to validate |
||
227 | * |
||
228 | * @throws ODataException Exception if $resourceType is invalid |
||
229 | * |
||
230 | * @return ResourceType |
||
231 | */ |
||
232 | private function validateResourceType(ResourceType $resourceType) |
||
244 | |||
245 | /** |
||
246 | * To get all resource types in the data source, |
||
247 | * Note: Wrapper for IMetadataProvider::getTypes method implementation. |
||
248 | * |
||
249 | * @return ResourceType[] |
||
250 | */ |
||
251 | public function getTypes() |
||
269 | |||
270 | public function getSingletons() |
||
275 | |||
276 | /** |
||
277 | * To get a resource set based on the specified resource set name which is |
||
278 | * visible, |
||
279 | * Note: Wrapper for IMetadataProvider::resolveResourceSet method |
||
280 | * implementation. |
||
281 | * |
||
282 | * @param string $name Name of the resource set |
||
283 | * |
||
284 | * @return ResourceSetWrapper|null Returns resource set with the given name if found, |
||
285 | * NULL if resource set is set to invisible or not found |
||
286 | */ |
||
287 | public function resolveResourceSet($name) |
||
300 | |||
301 | /** |
||
302 | * To get a resource type based on the resource set name, |
||
303 | * Note: Wrapper for IMetadataProvider::resolveResourceType |
||
304 | * method implementation. |
||
305 | * |
||
306 | * @param string $name Name of the resource set |
||
307 | * |
||
308 | * @throws ODataException If the ResourceType is invalid |
||
309 | * |
||
310 | * @return ResourceType|null resource type with the given resource set name if found else NULL |
||
311 | */ |
||
312 | public function resolveResourceType($name) |
||
321 | |||
322 | |||
323 | public function resolveSingleton($name) |
||
331 | |||
332 | /** |
||
333 | * The method must return a collection of all the types derived from |
||
334 | * $resourceType The collection returned should NOT include the type |
||
335 | * passed in as a parameter |
||
336 | * Note: Wrapper for IMetadataProvider::getDerivedTypes |
||
337 | * method implementation. |
||
338 | * |
||
339 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
340 | * |
||
341 | * @throws InvalidOperationException when the meat provider doesn't return an array |
||
342 | * |
||
343 | * @return ResourceType[] |
||
344 | */ |
||
345 | public function getDerivedTypes(ResourceType $resourceType) |
||
360 | |||
361 | /** |
||
362 | * Returns true if $resourceType represents an Entity Type which has derived |
||
363 | * Entity Types, else false. |
||
364 | * Note: Wrapper for IMetadataProvider::hasDerivedTypes method |
||
365 | * implementation. |
||
366 | * |
||
367 | * @param ResourceType $resourceType Resource to check for derived resource |
||
368 | * types |
||
369 | * |
||
370 | * @throws ODataException If the ResourceType is invalid |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | public function hasDerivedTypes(ResourceType $resourceType) |
||
380 | |||
381 | /** |
||
382 | * Gets the visible resource properties for the given resource type from the given resource set wrapper. |
||
383 | * |
||
384 | * @param ResourceSetWrapper $setWrapper Resource set wrapper in question |
||
385 | * @param ResourceType $resourceType Resource type in question |
||
386 | * |
||
387 | * @return ResourceProperty[] Collection of visible resource properties from the given resource set wrapper |
||
388 | * and resource type |
||
389 | */ |
||
390 | public function getResourceProperties(ResourceSetWrapper $setWrapper, ResourceType $resourceType) |
||
421 | |||
422 | /** |
||
423 | * Gets the target resource set wrapper for the given navigation property, |
||
424 | * source resource set wrapper and the source resource type. |
||
425 | * |
||
426 | * @param ResourceSetWrapper $resourceSetWrapper Source resource set |
||
427 | * @param ResourceEntityType $resourceType Source resource type |
||
428 | * @param ResourceProperty $navigationResourceProperty Navigation property |
||
429 | * |
||
430 | * @return ResourceSetWrapper|null Returns instance of ResourceSetWrapper |
||
431 | * (describes the entity set and associated configuration) for the |
||
432 | * given navigation property. returns NULL if resourceset for the |
||
433 | * navigation property is invisible or if metadata provider returns |
||
434 | * null resource association set |
||
435 | */ |
||
436 | public function getResourceSetWrapperForNavigationProperty( |
||
460 | |||
461 | /** |
||
462 | * Gets the ResourceAssociationSet instance for the given source association end, |
||
463 | * Note: Wrapper for IMetadataProvider::getResourceAssociationSet |
||
464 | * method implementation. |
||
465 | * |
||
466 | * @param ResourceSet $set Resource set of the source association end |
||
467 | * @param ResourceEntityType $type Resource type of the source association end |
||
468 | * @param ResourceProperty $property Resource property of the source association end |
||
469 | * |
||
470 | * @return ResourceAssociationSet|null Returns ResourceAssociationSet for the source |
||
471 | * association end, NULL if no such |
||
472 | * association end or resource set in the |
||
473 | * other end of the association is invisible |
||
474 | */ |
||
475 | public function getResourceAssociationSet( |
||
541 | |||
542 | /** |
||
543 | * Gets the resource type on which the resource property is declared on, |
||
544 | * If property is not declared in the given resource type, then this |
||
545 | * function drill down to the inheritance hierarchy of the given resource |
||
546 | * type to find out the base class in which the property is declared. |
||
547 | * |
||
548 | * @param ResourceType $type The resource type to start looking |
||
549 | * @param ResourceProperty $property The resource property in question |
||
550 | * |
||
551 | * @return ResourceType|null Returns reference to the ResourceType on which |
||
552 | * the $property is declared, NULL if |
||
553 | * $property is not declared anywhere |
||
554 | * in the inheritance hierarchy |
||
555 | */ |
||
556 | private function getResourceTypeWherePropertyIsDeclared(ResourceType $type, ResourceProperty $property) |
||
568 | |||
569 | /** |
||
570 | * Wrapper function over _validateResourceSetAndGetWrapper function. |
||
571 | * |
||
572 | * @param ResourceSet $resourceSet see the comments of _validateResourceSetAndGetWrapper |
||
573 | * |
||
574 | * @return ResourceSetWrapper|null see the comments of _validateResourceSetAndGetWrapper |
||
575 | */ |
||
576 | public function validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
580 | |||
581 | /** |
||
582 | * Gets the Edm Schema version compliance to the metadata. |
||
583 | * |
||
584 | * @return EdmSchemaVersion |
||
585 | */ |
||
586 | public function getEdmSchemaVersion() |
||
591 | |||
592 | /** |
||
593 | * Gets the underlying custom expression provider, the end developer is |
||
594 | * responsible for implementing IExpressionProvider if he choose for. |
||
595 | * |
||
596 | * @return IExpressionProvider Instance of IExpressionProvider implementation |
||
597 | */ |
||
598 | public function getExpressionProvider() |
||
602 | |||
603 | /** |
||
604 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
605 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
606 | * perform the ordering and paging. |
||
607 | * |
||
608 | * @return bool True if the query provider can handle ordered paging, false if POData should perform the paging |
||
609 | */ |
||
610 | public function handlesOrderedPaging() |
||
614 | |||
615 | /** |
||
616 | * Gets collection of entities belongs to an entity set. |
||
617 | * |
||
618 | * @param QueryType $queryType Indicates if this is a query for a count, entities, or entities with a |
||
619 | * count |
||
620 | * @param ResourceSet $resourceSet The entity set containing the entities that need to be fetched |
||
621 | * @param FilterInfo $filterInfo Represents the $filter parameter of the OData query. |
||
622 | * NULL if no $filter specified |
||
623 | * @param InternalOrderByInfo $orderBy The orderBy information |
||
624 | * @param int $top The top count |
||
625 | * @param int $skip The skip count |
||
626 | * @param InternalSkipTokenInfo $skipToken The skip token |
||
627 | * |
||
628 | * @return QueryResult |
||
629 | */ |
||
630 | public function getResourceSet( |
||
649 | |||
650 | /** |
||
651 | * Gets an entity instance from an entity set identified by a key. |
||
652 | * |
||
653 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
654 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
655 | * |
||
656 | * @return object|null Returns entity instance if found else null |
||
657 | */ |
||
658 | public function getResourceFromResourceSet(ResourceSet $resourceSet, KeyDescriptor $keyDescriptor) |
||
662 | |||
663 | /** |
||
664 | * Puts an entity instance to entity set identified by a key. |
||
665 | * |
||
666 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
667 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
668 | * |
||
669 | * @return bool|null Returns result of executiong query |
||
670 | */ |
||
671 | public function putResource( |
||
682 | |||
683 | /** |
||
684 | * Get related resource set for a resource. |
||
685 | * |
||
686 | * @param QueryType $queryType Indicates if this is a query for a count, entities, or entities |
||
687 | * with a count |
||
688 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
689 | * @param object $sourceEntity The source entity instance |
||
690 | * @param ResourceSet $targetResourceSet The resource set containing the target of the navigation property |
||
691 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
692 | * @param FilterInfo|null $filterInfo Represents the $filter parameter of the OData query. |
||
693 | * NULL if no $filter specified |
||
694 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
695 | * @param int $top number of records which need to be retrieved |
||
696 | * @param int $skip number of records which need to be skipped |
||
697 | * @param SkipTokenInfo|null $skipToken value indicating what records to skip |
||
698 | * |
||
699 | * @throws ODataException |
||
700 | * |
||
701 | * @return QueryResult |
||
702 | */ |
||
703 | public function getRelatedResourceSet( |
||
728 | |||
729 | /** |
||
730 | * Gets a related entity instance from an entity set identified by a key. |
||
731 | * |
||
732 | * @param ResourceSet $sourceResourceSet The entity set related to the entity to be fetched |
||
733 | * @param object $sourceEntity The related entity instance |
||
734 | * @param ResourceSet $targetResourceSet The entity set from which entity needs to be fetched |
||
735 | * @param ResourceProperty $targetProperty The metadata of the target property |
||
736 | * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched |
||
737 | * |
||
738 | * @return object|null Returns entity instance if found else null |
||
739 | */ |
||
740 | public function getResourceFromRelatedResourceSet( |
||
755 | |||
756 | /** |
||
757 | * Get related resource for a resource. |
||
758 | * |
||
759 | * @param ResourceSet $sourceResourceSet The source resource set |
||
760 | * @param object $sourceEntity The source resource |
||
761 | * @param ResourceSet $targetResourceSet The resource set of the navigation |
||
762 | * property |
||
763 | * @param ResourceProperty $targetProperty The navigation property to be |
||
764 | * retrieved |
||
765 | * |
||
766 | * @return object|null The related resource if exists else null |
||
767 | */ |
||
768 | public function getRelatedResourceReference( |
||
781 | |||
782 | /** |
||
783 | * Updates a resource. |
||
784 | * |
||
785 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
786 | * @param object $sourceEntityInstance The source entity instance |
||
787 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
788 | * @param object $data The New data for the entity instance. |
||
789 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
790 | * |
||
791 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
792 | */ |
||
793 | public function updateResource( |
||
808 | |||
809 | /** |
||
810 | * Delete resource from a resource set. |
||
811 | * |
||
812 | * @param ResourceSet $sourceResourceSet |
||
813 | * @param object $sourceEntityInstance |
||
814 | * |
||
815 | * return bool true if resources sucessfully deteled, otherwise false. |
||
816 | */ |
||
817 | public function deleteResource( |
||
826 | |||
827 | /** |
||
828 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
829 | * @param object $sourceEntityInstance The source entity instance |
||
830 | * @param object $data The New data for the entity instance. |
||
831 | * |
||
832 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
833 | */ |
||
834 | public function createResourceforResourceSet( |
||
845 | |||
846 | public function getMetadataXML() |
||
850 | } |
||
851 |
This check marks private properties in classes that are never used. Those properties can be removed.