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 |
||
32 | class ProvidersWrapper |
||
33 | { |
||
34 | /** |
||
35 | * Holds reference to IMetadataProvider implementation. |
||
36 | * |
||
37 | * @var IMetadataProvider |
||
38 | */ |
||
39 | private $metaProvider; |
||
40 | |||
41 | /** |
||
42 | * Holds reference to IQueryProvider implementation. |
||
43 | * |
||
44 | * @var IQueryProvider |
||
45 | */ |
||
46 | private $queryProvider; |
||
47 | |||
48 | /** |
||
49 | * Holds reference to IServiceConfiguration implementation. |
||
50 | * |
||
51 | * @var ServiceConfiguration |
||
52 | */ |
||
53 | private $config; |
||
54 | |||
55 | /** |
||
56 | * Cache for ResourceProperties of a resource type that belongs to a |
||
57 | * resource set. An entry (ResourceProperty collection) in this cache |
||
58 | * contains only the visible properties of ResourceType. |
||
59 | * |
||
60 | * @var array(string, array(string, ResourceProperty)) |
||
61 | */ |
||
62 | private $propertyCache; |
||
63 | |||
64 | /** |
||
65 | * Cache for ResourceSetWrappers. If ResourceSet is invisible value will |
||
66 | * be null. |
||
67 | * |
||
68 | * @var ResourceSetWrapper[] indexed by resource set name |
||
69 | */ |
||
70 | private $setWrapperCache; |
||
71 | |||
72 | /** |
||
73 | * Cache for ResourceTypes. |
||
74 | * |
||
75 | * @var ResourceType[] indexed by resource type name |
||
76 | */ |
||
77 | private $typeCache; |
||
78 | |||
79 | /** |
||
80 | * Cache for ResourceAssociationSet. If ResourceAssociationSet is invisible |
||
81 | * value will be null. |
||
82 | * |
||
83 | * @var ResourceAssociationSet[] indexed by name |
||
84 | */ |
||
85 | private $associationSetCache; |
||
86 | |||
87 | /** |
||
88 | * Creates a new instance of ProvidersWrapper. |
||
89 | * |
||
90 | * @param IMetadataProvider $metadataProvider Reference to IMetadataProvider implementation |
||
91 | * @param IQueryProvider $queryProvider Reference to IQueryProvider implementation |
||
92 | * @param ServiceConfiguration $configuration Reference to IServiceConfiguration implementation |
||
93 | */ |
||
94 | public function __construct(IMetadataProvider $metadataProvider, IQueryProvider $queryProvider, ServiceConfiguration $configuration) |
||
104 | |||
105 | //Wrappers for IMetadataProvider methods |
||
106 | |||
107 | /** |
||
108 | * To get the Container name for the data source, |
||
109 | * Note: Wrapper for IMetadataProvider::getContainerName method |
||
110 | * implementation. |
||
111 | * |
||
112 | * @return string that contains the name of the container |
||
113 | * |
||
114 | * @throws ODataException Exception if implementation returns empty container name |
||
115 | */ |
||
116 | public function getContainerName() |
||
128 | |||
129 | /** |
||
130 | * To get Namespace name for the data source, |
||
131 | * Note: Wrapper for IMetadataProvider::getContainerNamespace method implementation. |
||
132 | * |
||
133 | * @return string that contains the namespace name |
||
134 | * |
||
135 | * @throws ODataException Exception if implementation returns empty container namespace |
||
136 | */ |
||
137 | public function getContainerNamespace() |
||
149 | |||
150 | /** |
||
151 | * To get the data service configuration. |
||
152 | * |
||
153 | * @return ServiceConfiguration |
||
154 | */ |
||
155 | public function getConfiguration() |
||
159 | |||
160 | /** |
||
161 | * To get all entity set information, |
||
162 | * Note: Wrapper for IMetadataProvider::getResourceSets method implementation, |
||
163 | * This method returns array of ResourceSetWrapper instances but the corresponding IDSMP method returns array of ResourceSet instances. |
||
164 | * |
||
165 | * @return ResourceSetWrapper[] The ResourceSetWrappers for the visible ResourceSets |
||
166 | * |
||
167 | * @throws ODataException when two resource sets with the same name are encountered |
||
168 | */ |
||
169 | public function getResourceSets() |
||
189 | |||
190 | /** |
||
191 | * To get all resource types in the data source, |
||
192 | * Note: Wrapper for IMetadataProvider::getTypes method implementation. |
||
193 | * |
||
194 | * @return ResourceType[] |
||
195 | */ |
||
196 | public function getTypes() |
||
214 | |||
215 | /** |
||
216 | * To get a resource set based on the specified resource set name which is |
||
217 | * visible, |
||
218 | * Note: Wrapper for IMetadataProvider::resolveResourceSet method |
||
219 | * implementation. |
||
220 | * |
||
221 | * @param string $name Name of the resource set |
||
222 | * |
||
223 | * @return ResourceSetWrapper|null Returns resource set with the given name if found, NULL if resource set is set to invisible or not found |
||
224 | */ |
||
225 | public function resolveResourceSet($name) |
||
238 | |||
239 | /** |
||
240 | * To get a resource type based on the resource set name, |
||
241 | * Note: Wrapper for IMetadataProvider::resolveResourceType |
||
242 | * method implementation. |
||
243 | * |
||
244 | * @param string $name Name of the resource set |
||
245 | * |
||
246 | * @return ResourceType|null resource type with the given resource set name if found else NULL |
||
247 | * |
||
248 | * @throws ODataException If the ResourceType is invalid |
||
249 | */ |
||
250 | public function resolveResourceType($name) |
||
259 | |||
260 | /** |
||
261 | * The method must return a collection of all the types derived from |
||
262 | * $resourceType The collection returned should NOT include the type |
||
263 | * passed in as a parameter |
||
264 | * Note: Wrapper for IMetadataProvider::getDerivedTypes |
||
265 | * method implementation. |
||
266 | * |
||
267 | * @param ResourceType $resourceType Resource to get derived resource types from |
||
268 | * |
||
269 | * @return ResourceType[] |
||
270 | * |
||
271 | * @throws InvalidOperationException when the meat provider doesn't return an array |
||
272 | */ |
||
273 | public function getDerivedTypes(ResourceType $resourceType) |
||
286 | |||
287 | /** |
||
288 | * Returns true if $resourceType represents an Entity Type which has derived |
||
289 | * Entity Types, else false. |
||
290 | * Note: Wrapper for IMetadataProvider::hasDerivedTypes method |
||
291 | * implementation. |
||
292 | * |
||
293 | * @param ResourceType $resourceType Resource to check for derived resource |
||
294 | * types |
||
295 | * |
||
296 | * @return bool |
||
297 | * |
||
298 | * @throws ODataException If the ResourceType is invalid |
||
299 | */ |
||
300 | public function hasDerivedTypes(ResourceType $resourceType) |
||
306 | |||
307 | /** |
||
308 | * Gets the ResourceAssociationSet instance for the given source association end, |
||
309 | * Note: Wrapper for IMetadataProvider::getResourceAssociationSet |
||
310 | * method implementation. |
||
311 | * |
||
312 | * @param ResourceSet $set Resource set of the source association end |
||
313 | * @param ResourceType $type Resource type of the source association end |
||
314 | * @param ResourceProperty $property Resource property of the source association end |
||
315 | * |
||
316 | * @return ResourceAssociationSet|null Returns ResourceAssociationSet for the source |
||
317 | * association end, NULL if no such |
||
318 | * association end or resource set in the |
||
319 | * other end of the association is invisible |
||
320 | */ |
||
321 | public function getResourceAssociationSet( |
||
386 | |||
387 | /** |
||
388 | * Gets the target resource set wrapper for the given navigation property, |
||
389 | * source resource set wrapper and the source resource type. |
||
390 | * |
||
391 | * @param ResourceSetWrapper $resourceSetWrapper Source resource set |
||
392 | * @param ResourceType $resourceType Source resource type |
||
393 | * @param ResourceProperty $navigationResourceProperty Navigation property |
||
394 | * |
||
395 | * @return ResourceSetWrapper|null Returns instance of ResourceSetWrapper |
||
396 | * (describes the entity set and associated configuration) for the |
||
397 | * given navigation property. returns NULL if resourceset for the |
||
398 | * navigation property is invisible or if metadata provider returns |
||
399 | * null resource association set |
||
400 | */ |
||
401 | public function getResourceSetWrapperForNavigationProperty( |
||
426 | |||
427 | /** |
||
428 | * Gets the visible resource properties for the given resource type from the given resource set wrapper. |
||
429 | * |
||
430 | * @param ResourceSetWrapper $setWrapper Resource set wrapper in question |
||
431 | * @param ResourceType $resourceType Resource type in question |
||
432 | * |
||
433 | * @return ResourceProperty[] Collection of visible resource properties from the given resource set wrapper and resource type |
||
434 | */ |
||
435 | public function getResourceProperties(ResourceSetWrapper $setWrapper, ResourceType $resourceType) |
||
462 | |||
463 | /** |
||
464 | * Wrapper function over _validateResourceSetAndGetWrapper function. |
||
465 | * |
||
466 | * @param ResourceSet $resourceSet see the comments of _validateResourceSetAndGetWrapper |
||
467 | * |
||
468 | * @return ResourceSetWrapper|null see the comments of _validateResourceSetAndGetWrapper |
||
469 | */ |
||
470 | public function validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
474 | |||
475 | /** |
||
476 | * Gets the Edm Schema version compliance to the metadata. |
||
477 | * |
||
478 | * @return EdmSchemaVersion |
||
479 | */ |
||
480 | public function getEdmSchemaVersion() |
||
485 | |||
486 | /** |
||
487 | * This function perform the following operations |
||
488 | * (1) If the cache contain an entry [key, value] for the resourceset then |
||
489 | * return the entry-value |
||
490 | * (2) If the cache not contain an entry for the resourceset then validate |
||
491 | * the resourceset |
||
492 | * (a) If valid add entry as [resouceset_name, resourceSetWrapper] |
||
493 | * (b) if not valid add entry as [resouceset_name, null] |
||
494 | * Note: validating a resourceset means checking the resourceset is visible |
||
495 | * or not using configuration. |
||
496 | * |
||
497 | * @param ResourceSet $resourceSet The resourceset to validate and get the |
||
498 | * wrapper for |
||
499 | * |
||
500 | * @return ResourceSetWrapper|null Returns an instance if a resource set with the given name is visible |
||
501 | */ |
||
502 | private function _validateResourceSetAndGetWrapper(ResourceSet $resourceSet) |
||
519 | |||
520 | /** |
||
521 | * Validates the given instance of ResourceType. |
||
522 | * |
||
523 | * @param ResourceType $resourceType The ResourceType to validate |
||
524 | * |
||
525 | * @return ResourceType |
||
526 | * |
||
527 | * @throws ODataException Exception if $resourceType is invalid |
||
528 | */ |
||
529 | private function _validateResourceType(ResourceType $resourceType) |
||
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 $resourceType The resource type to start looking |
||
549 | * @param ResourceProperty $resourceProperty The resource property in question |
||
550 | * |
||
551 | * @return ResourceType|null Returns reference to the ResourceType on which |
||
552 | * the $resourceProperty is declared, NULL if |
||
553 | * $resourceProperty is not declared anywhere |
||
554 | * in the inheritance hierarchy |
||
555 | */ |
||
556 | private function _getResourceTypeWherePropertyIsDeclared( |
||
571 | |||
572 | /** |
||
573 | * Gets the underlying custom expression provider, the end developer is |
||
574 | * responsible for implementing IExpressionProvider if he choose for. |
||
575 | * |
||
576 | * @return IExpressionProvider Instance of IExpressionProvider implementation |
||
577 | */ |
||
578 | public function getExpressionProvider() |
||
591 | |||
592 | /** |
||
593 | * Indicates if the QueryProvider can handle ordered paging, this means respecting order, skip, and top parameters |
||
594 | * If the query provider can not handle ordered paging, it must return the entire result set and POData will |
||
595 | * perform the ordering and paging. |
||
596 | * |
||
597 | * @return bool True if the query provider can handle ordered paging, false if POData should perform the paging |
||
598 | */ |
||
599 | public function handlesOrderedPaging() |
||
603 | |||
604 | /** |
||
605 | * @param QueryResult $queryResult |
||
606 | * @param string $methodName |
||
607 | */ |
||
608 | private function ValidateQueryResult($queryResult, QueryType $queryType, $methodName) |
||
638 | |||
639 | /** |
||
640 | * Gets collection of entities belongs to an entity set. |
||
641 | * |
||
642 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
643 | * @param ResourceSet $resourceSet The entity set containing the entities that need to be fetched |
||
644 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
645 | * @param InternalOrderByInfo $orderBy The orderBy information |
||
646 | * @param int $top The top count |
||
647 | * @param int $skip The skip count |
||
648 | * @param InternalSkipTokenInfo $skipToken The skip token |
||
649 | * |
||
650 | * @return QueryResult |
||
651 | */ |
||
652 | public function getResourceSet(QueryType $queryType, ResourceSet $resourceSet, FilterInfo $filterInfo = null, InternalOrderByInfo $orderBy = null, $top = null, $skip = null, InternalSkipTokenInfo $skipToken = null) |
||
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) |
||
689 | |||
690 | /** |
||
691 | * Puts an entity instance to entity set identified by a key. |
||
692 | * |
||
693 | * @param ResourceSet $resourceSet The entity set containing the entity to update |
||
694 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to update |
||
695 | * |
||
696 | * @return bool|null Returns result of executiong query |
||
697 | */ |
||
698 | public function putResource( |
||
711 | |||
712 | /** |
||
713 | * Get related resource set for a resource. |
||
714 | * |
||
715 | * @param QueryType $queryType indicates if this is a query for a count, entities, or entities with a count |
||
716 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
717 | * @param object $sourceEntity The source entity instance |
||
718 | * @param ResourceSet $targetResourceSet The resource set of containing the target of the navigation property |
||
719 | * @param ResourceProperty $targetProperty The navigation property to retrieve |
||
720 | * @param FilterInfo $filterInfo represents the $filter parameter of the OData query. NULL if no $filter specified |
||
721 | * @param mixed $orderBy sorted order if we want to get the data in some specific order |
||
722 | * @param int $top number of records which need to be skip |
||
723 | * @param string $skip value indicating what records to skip |
||
724 | * |
||
725 | * @return QueryResult |
||
726 | * |
||
727 | * @throws ODataException |
||
728 | */ |
||
729 | public function getRelatedResourceSet( |
||
756 | |||
757 | /** |
||
758 | * Gets a related entity instance from an entity set identified by a key. |
||
759 | * |
||
760 | * @param ResourceSet $sourceResourceSet The entity set related to the entity to be fetched |
||
761 | * @param object $sourceEntity The related entity instance |
||
762 | * @param ResourceSet $targetResourceSet The entity set from which entity needs to be fetched |
||
763 | * @param ResourceProperty $targetProperty The metadata of the target property |
||
764 | * @param KeyDescriptor $keyDescriptor The key to identify the entity to be fetched |
||
765 | * |
||
766 | * @return object|null Returns entity instance if found else null |
||
767 | */ |
||
768 | public function getResourceFromRelatedResourceSet( |
||
792 | |||
793 | /** |
||
794 | * Get related resource for a resource. |
||
795 | * |
||
796 | * @param ResourceSet $sourceResourceSet The source resource set |
||
797 | * @param object $sourceEntity The source resource |
||
798 | * @param ResourceSet $targetResourceSet The resource set of the navigation |
||
799 | * property |
||
800 | * @param ResourceProperty $targetProperty The navigation property to be |
||
801 | * retrieved |
||
802 | * |
||
803 | * @return object|null The related resource if exists else null |
||
804 | */ |
||
805 | public function getRelatedResourceReference( |
||
862 | |||
863 | /** |
||
864 | * Validate the given entity instance. |
||
865 | * |
||
866 | * @param object $entityInstance Entity instance to validate |
||
867 | * @param ResourceSet &$resourceSet Resource set to which the entity |
||
868 | * instance belongs to |
||
869 | * @param KeyDescriptor &$keyDescriptor The key descriptor |
||
870 | * @param string $methodName Method from which this function |
||
871 | * invoked |
||
872 | * |
||
873 | * @throws ODataException |
||
874 | */ |
||
875 | private function _validateEntityInstance( |
||
924 | |||
925 | /** |
||
926 | * Updates a resource |
||
927 | * |
||
928 | * @param ResourceSet $sourceResourceSet The entity set containing the source entity |
||
929 | * @param object $sourceEntityInstance The source entity instance |
||
930 | * @param KeyDescriptor $keyDescriptor The key identifying the entity to fetch |
||
931 | * @param object $data The New data for the entity instance. |
||
932 | * @param bool $shouldUpdate Should undefined values be updated or reset to default |
||
933 | * |
||
934 | * @return object|null The new resource value if it is assignable or throw exception for null. |
||
935 | */ |
||
936 | public function updateResource( |
||
951 | /** |
||
952 | * Delete resource from a resource set. |
||
953 | * @param ResourceSet|null $sourceResourceSet |
||
954 | * @param object $sourceEntityInstance |
||
955 | * |
||
956 | * return bool true if resources sucessfully deteled, otherwise false. |
||
957 | */ |
||
958 | public function deleteResource( |
||
967 | /** |
||
968 | * @param ResourceSet $resourceSet The entity set containing the entity to fetch |
||
969 | * @param object $sourceEntityInstance The source entity instance |
||
970 | * @param object $data The New data for the entity instance. |
||
971 | * |
||
972 | * returns object|null returns the newly created model if sucessful or null if model creation failed. |
||
973 | */ |
||
974 | public function createResourceforResourceSet( |
||
985 | |||
986 | /** |
||
987 | * Assert that the given condition is true. |
||
988 | * |
||
989 | * @param bool $condition Condition to be asserted |
||
990 | * @param string $conditionAsString String containing message incase |
||
991 | * if assertion fails |
||
992 | * |
||
993 | * @throws InvalidOperationException Incase if assertion fails |
||
994 | */ |
||
995 | protected function assert($condition, $conditionAsString) |
||
1001 | } |
||
1002 |
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: