Conditions | 28 |
Paths | 457 |
Total Lines | 176 |
Code Lines | 115 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
226 | private function buildOrderByTree(array &$orderByPathSegments) |
||
227 | { |
||
228 | foreach ($orderByPathSegments as $index1 => &$orderBySubPathSegments) { |
||
229 | /** @var OrderByNode $currentNode */ |
||
230 | $currentNode = $this->rootOrderByNode; |
||
231 | $currentObject = $this->dummyObject; |
||
232 | $ascending = true; |
||
233 | $subPathCount = count($orderBySubPathSegments); |
||
234 | // Check sort order is specified in the path, if so set a |
||
235 | // flag and remove that segment |
||
236 | if ($subPathCount > 1) { |
||
237 | if ('*desc' === $orderBySubPathSegments[$subPathCount - 1]) { |
||
238 | $ascending = false; |
||
239 | unset($orderBySubPathSegments[$subPathCount - 1]); |
||
240 | --$subPathCount; |
||
241 | } elseif ('*asc' === $orderBySubPathSegments[$subPathCount - 1]) { |
||
242 | unset($orderBySubPathSegments[$subPathCount - 1]); |
||
243 | --$subPathCount; |
||
244 | } |
||
245 | } |
||
246 | $ancestors = [$this->rootOrderByNode->getResourceSetWrapper()->getName()]; |
||
247 | foreach ($orderBySubPathSegments as $index2 => $orderBySubPathSegment) { |
||
248 | $isLastSegment = ($index2 == $subPathCount - 1); |
||
249 | $resourceSetWrapper = null; |
||
250 | /** @var ResourceEntityType $resourceType */ |
||
251 | $resourceType = $currentNode->getResourceType(); |
||
252 | /** @var ResourceProperty $resourceProperty */ |
||
253 | $resourceProperty = $resourceType->resolveProperty($orderBySubPathSegment); |
||
254 | if (null === $resourceProperty) { |
||
255 | throw ODataException::createSyntaxError( |
||
256 | Messages::orderByParserPropertyNotFound( |
||
257 | $resourceType->getFullName(), |
||
258 | $orderBySubPathSegment |
||
259 | ) |
||
260 | ); |
||
261 | } |
||
262 | /** @var ResourcePropertyKind $rKind */ |
||
263 | $rKind = $resourceProperty->getKind(); |
||
264 | $rawKind = ($rKind instanceof ResourcePropertyKind) ? $rKind : $rKind; |
||
265 | |||
266 | if ($resourceProperty->isKindOf(ResourcePropertyKind::BAG())) { |
||
267 | throw ODataException::createBadRequestError( |
||
268 | Messages::orderByParserBagPropertyNotAllowed( |
||
269 | $resourceProperty->getName() |
||
270 | ) |
||
271 | ); |
||
272 | } elseif ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE())) { |
||
273 | if (!$isLastSegment) { |
||
274 | throw ODataException::createBadRequestError( |
||
275 | Messages::orderByParserPrimitiveAsIntermediateSegment( |
||
276 | $resourceProperty->getName() |
||
277 | ) |
||
278 | ); |
||
279 | } |
||
280 | |||
281 | $type = $resourceProperty->getInstanceType(); |
||
282 | if ($type instanceof Binary) { |
||
283 | throw ODataException::createBadRequestError( |
||
284 | Messages::orderByParserSortByBinaryPropertyNotAllowed($resourceProperty->getName()) |
||
285 | ); |
||
286 | } |
||
287 | } elseif ($rawKind == ResourcePropertyKind::RESOURCESET_REFERENCE() |
||
288 | || $rawKind == ResourcePropertyKind::RESOURCE_REFERENCE() |
||
289 | ) { |
||
290 | $this->assertion($currentNode instanceof OrderByRootNode || $currentNode instanceof OrderByNode); |
||
291 | $resourceSetWrapper = $currentNode->getResourceSetWrapper(); |
||
292 | $this->assertion(null !== $resourceSetWrapper); |
||
293 | $resourceSetWrapper |
||
294 | = $this->providerWrapper->getResourceSetWrapperForNavigationProperty( |
||
295 | $resourceSetWrapper, |
||
296 | $resourceType, |
||
297 | $resourceProperty |
||
298 | ); |
||
299 | if (null === $resourceSetWrapper) { |
||
300 | throw ODataException::createBadRequestError( |
||
301 | Messages::badRequestInvalidPropertyNameSpecified( |
||
302 | $resourceType->getFullName(), |
||
303 | $orderBySubPathSegment |
||
304 | ) |
||
305 | ); |
||
306 | } |
||
307 | |||
308 | if ($rKind == ResourcePropertyKind::RESOURCESET_REFERENCE()) { |
||
309 | throw ODataException::createBadRequestError( |
||
310 | Messages::orderByParserResourceSetReferenceNotAllowed( |
||
311 | $resourceProperty->getName(), |
||
312 | $resourceType->getFullName() |
||
313 | ) |
||
314 | ); |
||
315 | } |
||
316 | |||
317 | $resourceSetWrapper->checkResourceSetRightsForRead(true); |
||
318 | if ($isLastSegment) { |
||
319 | throw ODataException::createBadRequestError( |
||
320 | Messages::orderByParserSortByNavigationPropertyIsNotAllowed( |
||
321 | $resourceProperty->getName() |
||
322 | ) |
||
323 | ); |
||
324 | } |
||
325 | |||
326 | $ancestors[] = $orderBySubPathSegment; |
||
327 | } elseif ($resourceProperty->isKindOf(ResourcePropertyKind::COMPLEX_TYPE())) { |
||
328 | if ($isLastSegment) { |
||
329 | throw ODataException::createBadRequestError( |
||
330 | Messages::orderByParserSortByComplexPropertyIsNotAllowed( |
||
331 | $resourceProperty->getName() |
||
332 | ) |
||
333 | ); |
||
334 | } |
||
335 | |||
336 | $ancestors[] = $orderBySubPathSegment; |
||
337 | } else { |
||
338 | throw ODataException::createInternalServerError( |
||
339 | Messages::orderByParserUnexpectedPropertyType() |
||
340 | ); |
||
341 | } |
||
342 | |||
343 | $node = $currentNode->findNode($orderBySubPathSegment); |
||
344 | if (null === $node) { |
||
345 | if ($resourceProperty->isKindOf(ResourcePropertyKind::PRIMITIVE())) { |
||
346 | $node = new OrderByLeafNode( |
||
347 | $orderBySubPathSegment, |
||
348 | $resourceProperty, |
||
349 | $ascending |
||
350 | ); |
||
351 | $this->comparisonFunctions[] = $node->buildComparisonFunction($ancestors); |
||
352 | } elseif ($resourceProperty->getKind() == ResourcePropertyKind::RESOURCE_REFERENCE()) { |
||
353 | $node = new OrderByNode( |
||
354 | $orderBySubPathSegment, |
||
355 | $resourceProperty, |
||
356 | $resourceSetWrapper |
||
357 | ); |
||
358 | // Initialize this member variable (identified by |
||
359 | // $resourceProperty) of parent object. |
||
360 | try { |
||
361 | $instance = $resourceProperty->getInstanceType(); |
||
362 | assert($instance instanceof ReflectionClass, get_class($instance)); |
||
363 | $object = $instance->newInstanceArgs(); |
||
364 | $resourceType->setPropertyValue($currentObject, $resourceProperty->getName(), $object); |
||
365 | $currentObject = $object; |
||
366 | } catch (ReflectionException $reflectionException) { |
||
367 | $this->throwBadAccessOrInitException($resourceProperty, $resourceType); |
||
368 | } |
||
369 | } elseif ($resourceProperty->getKind() == ResourcePropertyKind::COMPLEX_TYPE()) { |
||
370 | $node = new OrderByNode($orderBySubPathSegment, $resourceProperty, null); |
||
371 | // Initialize this member variable |
||
372 | // (identified by $resourceProperty)of parent object. |
||
373 | try { |
||
374 | $instance = $resourceProperty->getInstanceType(); |
||
375 | assert($instance instanceof ReflectionClass, get_class($instance)); |
||
376 | $object = $instance->newInstanceArgs(); |
||
377 | $resourceType->setPropertyValue($currentObject, $resourceProperty->getName(), $object); |
||
378 | $currentObject = $object; |
||
379 | } catch (ReflectionException $reflectionException) { |
||
380 | $this->throwBadAccessOrInitException($resourceProperty, $resourceType); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | $currentNode->addNode($node); |
||
385 | } else { |
||
386 | try { |
||
387 | $currentObject = ReflectionHandler::getProperty($currentObject, $resourceProperty->getName()); |
||
388 | } catch (ReflectionException $reflectionException) { |
||
389 | $this->throwBadAccessOrInitException($resourceProperty, $resourceType); |
||
390 | } |
||
391 | |||
392 | if ($node instanceof OrderByLeafNode) { |
||
393 | //remove duplicate orderby path |
||
394 | unset($orderByPathSegments[$index1]); |
||
395 | } |
||
396 | } |
||
397 | |||
398 | $currentNode = $node; |
||
399 | } |
||
400 | } |
||
401 | return null; |
||
402 | } |
||
507 |