Complex classes like ExpandProjectionParser 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 ExpandProjectionParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class ExpandProjectionParser |
||
|
|||
38 | { |
||
39 | /** |
||
40 | * The wrapper of IMetadataProvider and IQueryProvider |
||
41 | * . |
||
42 | * |
||
43 | * @var ProvidersWrapper |
||
44 | */ |
||
45 | private $providerWrapper; |
||
46 | |||
47 | /** |
||
48 | * Holds reference to the root of 'Projection Tree'. |
||
49 | * |
||
50 | * @var RootProjectionNode |
||
51 | */ |
||
52 | private $rootProjectionNode; |
||
53 | |||
54 | /** |
||
55 | * Creates new instance of ExpandProjectionParser. |
||
56 | * |
||
57 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
58 | */ |
||
59 | private function __construct(ProvidersWrapper $providerWrapper) |
||
63 | |||
64 | /** |
||
65 | * Parse the given expand and select clause, validate them |
||
66 | * and build 'Projection Tree'. |
||
67 | * |
||
68 | * @param ResourceSetWrapper $resourceSetWrapper The resource set identified by the resource path uri |
||
69 | * @param ResourceType $resourceType The resource type of entities identified by the resource path uri |
||
70 | * @param InternalOrderByInfo $internalOrderInfo The top level sort information, this will be set if the $skip, |
||
71 | * $top is specified in the |
||
72 | * request uri or Server side paging is |
||
73 | * enabled for top level resource |
||
74 | * @param int $skipCount The value of $skip option applied to the top level resource |
||
75 | * set identified by the |
||
76 | * resource path uri |
||
77 | * null means $skip |
||
78 | * option is not present |
||
79 | * @param int $takeCount The minimum among the value of $top option applied to and |
||
80 | * page size configured |
||
81 | * for the top level |
||
82 | * resource |
||
83 | * set identified |
||
84 | * by the resource |
||
85 | * path uri. |
||
86 | * null means $top option |
||
87 | * is not present and/or |
||
88 | * page size is not |
||
89 | * configured for top |
||
90 | * level resource set |
||
91 | * @param string $expand The value of $expand clause |
||
92 | * @param string $select The value of $select clause |
||
93 | * @param ProvidersWrapper $providerWrapper Reference to metadata and query provider wrapper |
||
94 | * |
||
95 | * @throws ODataException If any error occur while parsing expand and/or select clause |
||
96 | * |
||
97 | * @return RootProjectionNode Returns root of the 'Projection Tree' |
||
98 | */ |
||
99 | public static function parseExpandAndSelectClause( |
||
123 | |||
124 | /** |
||
125 | * Read the given expand clause and build 'Projection Tree', |
||
126 | * do nothing if the clause is null. |
||
127 | * |
||
128 | * @param string $expand Value of $expand clause |
||
129 | * |
||
130 | * @throws ODataException If any error occurs while reading expand clause |
||
131 | * or building the projection tree |
||
132 | */ |
||
133 | private function parseExpand($expand) |
||
141 | |||
142 | /** |
||
143 | * Read the given select clause and apply selection to the |
||
144 | * 'Projection Tree', mark the entire tree as selected if this |
||
145 | * clause is null |
||
146 | * Note: _parseExpand should to be called before the invocation |
||
147 | * of this function so that basic 'Projection Tree' with expand |
||
148 | * information will be ready. |
||
149 | * |
||
150 | * @param string $select Value of $select clause |
||
151 | * |
||
152 | * @throws ODataException If any error occurs while reading expand clause |
||
153 | * or applying selection to projection tree |
||
154 | */ |
||
155 | private function parseSelect($select) |
||
169 | |||
170 | /** |
||
171 | * Build 'Projection Tree' from the given expand path segments. |
||
172 | * |
||
173 | * @param array<array> $expandPathSegments Collection of expand paths |
||
174 | * |
||
175 | * @throws ODataException If any error occurs while processing the expand path segments |
||
176 | */ |
||
177 | private function buildProjectionTree($expandPathSegments) |
||
178 | { |
||
179 | foreach ($expandPathSegments as $expandSubPathSegments) { |
||
180 | $currentNode = $this->rootProjectionNode; |
||
181 | foreach ($expandSubPathSegments as $expandSubPathSegment) { |
||
182 | $resourceSetWrapper = $currentNode->getResourceSetWrapper(); |
||
183 | $resourceType = $currentNode->getResourceType(); |
||
184 | assert($resourceType instanceof ResourceEntityType); |
||
185 | $resourceProperty = $resourceType->resolveProperty($expandSubPathSegment); |
||
186 | assert($resourceProperty instanceof ResourceProperty); |
||
187 | $keyType = ResourceAssociationSet::keyNameFromTypeAndProperty($resourceType, $resourceProperty); |
||
188 | $assoc = $this->getProviderWrapper()->getMetaProvider()->resolveAssociationSet($keyType); |
||
189 | $concreteType = isset($assoc) ? $assoc->getEnd2()->getConcreteType() : $resourceType; |
||
190 | if (null === $resourceProperty) { |
||
191 | throw ODataException::createSyntaxError( |
||
192 | Messages::expandProjectionParserPropertyNotFound( |
||
193 | $resourceType->getFullName(), |
||
194 | $expandSubPathSegment, |
||
195 | false |
||
196 | ) |
||
197 | ); |
||
198 | } elseif ($resourceProperty->getTypeKind() != ResourceTypeKind::ENTITY()) { |
||
199 | throw ODataException::createBadRequestError( |
||
200 | Messages::expandProjectionParserExpandCanOnlyAppliedToEntity( |
||
201 | $resourceType->getFullName(), |
||
202 | $expandSubPathSegment |
||
203 | ) |
||
204 | ); |
||
205 | } |
||
206 | assert($resourceType instanceof ResourceEntityType); |
||
207 | |||
208 | $resourceSetWrapper = $this->providerWrapper |
||
209 | ->getResourceSetWrapperForNavigationProperty( |
||
210 | $resourceSetWrapper, |
||
211 | $resourceType, |
||
212 | $resourceProperty |
||
213 | ); |
||
214 | |||
215 | if (null === $resourceSetWrapper) { |
||
216 | throw ODataException::createBadRequestError( |
||
217 | Messages::badRequestInvalidPropertyNameSpecified( |
||
218 | $resourceType->getFullName(), |
||
219 | $expandSubPathSegment |
||
220 | ) |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | $singleResult |
||
225 | = $resourceProperty->isKindOf( |
||
226 | ResourcePropertyKind::RESOURCE_REFERENCE |
||
227 | ); |
||
228 | $resourceSetWrapper->checkResourceSetRightsForRead($singleResult); |
||
229 | $pageSize = $resourceSetWrapper->getResourceSetPageSize(); |
||
230 | $internalOrderByInfo = null; |
||
231 | if ($pageSize != 0 && !$singleResult) { |
||
232 | $this->rootProjectionNode->setPagedExpandedResult(true); |
||
233 | $rt = $resourceSetWrapper->getResourceType(); |
||
234 | $payloadType = $rt->isAbstract() ? $concreteType : $rt; |
||
235 | |||
236 | $keys = array_keys($rt->getKeyProperties()); |
||
237 | $orderBy = null; |
||
238 | foreach ($keys as $key) { |
||
239 | $orderBy = $orderBy . $key . ', '; |
||
240 | } |
||
241 | |||
242 | $orderBy = rtrim($orderBy, ', '); |
||
243 | $internalOrderByInfo = OrderByParser::parseOrderByClause( |
||
244 | $resourceSetWrapper, |
||
245 | $payloadType, |
||
246 | $orderBy, |
||
247 | $this->providerWrapper |
||
248 | ); |
||
249 | } |
||
250 | |||
251 | $node = $currentNode->findNode($expandSubPathSegment); |
||
252 | if (null === $node) { |
||
253 | $maxResultCount = $this->providerWrapper |
||
254 | ->getConfiguration()->getMaxResultsPerCollection(); |
||
255 | $node = new ExpandedProjectionNode( |
||
256 | $expandSubPathSegment, |
||
257 | $resourceSetWrapper, |
||
258 | $internalOrderByInfo, |
||
259 | null, |
||
260 | $pageSize == 0 ? null : $pageSize, |
||
261 | $maxResultCount == PHP_INT_MAX ? null : $maxResultCount, |
||
262 | $resourceProperty |
||
263 | ); |
||
264 | $currentNode->addNode($node); |
||
265 | } |
||
266 | |||
267 | $currentNode = $node; |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Modify the 'Projection Tree' to include selection details. |
||
274 | * |
||
275 | * @param array<array<string>> $selectPathSegments Collection of select |
||
276 | * paths |
||
277 | * |
||
278 | * @throws ODataException If any error occurs while processing select |
||
279 | * path segments |
||
280 | */ |
||
281 | private function applySelectionToProjectionTree($selectPathSegments) |
||
373 | |||
374 | /** |
||
375 | * Read expand or select clause. |
||
376 | * |
||
377 | * @param string $value expand or select clause to read |
||
378 | * @param bool $isSelect true means $value is value of select clause |
||
379 | * else value of expand clause |
||
380 | * |
||
381 | * @return array<array> An array of 'PathSegment's, each of which is array of 'SubPathSegment's |
||
382 | */ |
||
383 | private function readExpandOrSelect($value, $isSelect) |
||
418 | |||
419 | /** |
||
420 | * @return ProvidersWrapper |
||
421 | */ |
||
422 | public function getProviderWrapper() |
||
426 | } |
||
427 |