| Conditions | 37 |
| Paths | 54 |
| Total Lines | 188 |
| Code Lines | 126 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| 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 |
||
| 338 | private function createNextSegment( |
||
| 339 | SegmentDescriptor $previous, |
||
| 340 | string $segment, |
||
| 341 | bool $checkRights |
||
| 342 | ): SegmentDescriptor { |
||
| 343 | $previousKind = $previous->getTargetKind(); |
||
| 344 | if ($previousKind->isTerminal()) { |
||
| 345 | //All these targets are terminal segments, there cannot be anything after them. |
||
| 346 | throw ODataException::resourceNotFoundError( |
||
| 347 | Messages::segmentParserMustBeLeafSegment($previous->getIdentifier()) |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | $keyPredicate = null; |
||
| 352 | $identifier = $this->extractSegmentIdentifierAndKeyPredicate($segment, $keyPredicate); |
||
| 353 | $hasPredicate = null !== $keyPredicate; |
||
| 354 | |||
| 355 | $singleton = $this->providerWrapper->resolveSingleton($identifier); |
||
| 356 | if (null !== $singleton) { |
||
| 357 | throw ODataException::createSyntaxError('Singleton must be first element'); |
||
| 358 | } |
||
| 359 | |||
| 360 | if ($previousKind == TargetKind::PRIMITIVE()) { |
||
| 361 | if ($identifier !== ODataConstants::URI_VALUE_SEGMENT) { |
||
| 362 | throw ODataException::resourceNotFoundError( |
||
| 363 | Messages::segmentParserOnlyValueSegmentAllowedAfterPrimitivePropertySegment( |
||
| 364 | $identifier, |
||
| 365 | $previous->getIdentifier() |
||
| 366 | ) |
||
| 367 | ); |
||
| 368 | } |
||
| 369 | |||
| 370 | $this->assertion(!$hasPredicate); |
||
| 371 | $current = SegmentDescriptor::createFrom($previous); |
||
| 372 | $current->setIdentifier(ODataConstants::URI_VALUE_SEGMENT); |
||
| 373 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
| 374 | $current->setSingleResult(true); |
||
| 375 | } elseif (null !== $previous->getPrevious() |
||
| 376 | && $previous->getPrevious()->getIdentifier() === ODataConstants::URI_LINK_SEGMENT |
||
| 377 | && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
| 378 | throw ODataException::createBadRequestError( |
||
| 379 | Messages::segmentParserNoSegmentAllowedAfterPostLinkSegment($identifier) |
||
| 380 | ); |
||
| 381 | } elseif ($previousKind == TargetKind::RESOURCE() |
||
| 382 | && $previous->isSingleResult() |
||
| 383 | && $identifier === ODataConstants::URI_LINK_SEGMENT |
||
| 384 | ) { |
||
| 385 | $this->assertion(!$hasPredicate); |
||
| 386 | $current = SegmentDescriptor::createFrom($previous); |
||
| 387 | $current->setIdentifier(ODataConstants::URI_LINK_SEGMENT); |
||
| 388 | $current->setTargetKind(TargetKind::LINK()); |
||
| 389 | } else { |
||
| 390 | //Do a sanity check here |
||
| 391 | if ($previousKind != TargetKind::COMPLEX_OBJECT() |
||
| 392 | && $previousKind != TargetKind::RESOURCE() |
||
| 393 | && $previousKind != TargetKind::LINK() |
||
| 394 | ) { |
||
| 395 | throw ODataException::createInternalServerError( |
||
| 396 | Messages::segmentParserInconsistentTargetKindState() |
||
| 397 | ); |
||
| 398 | } |
||
| 399 | |||
| 400 | if (!$previous->isSingleResult() && $identifier !== ODataConstants::URI_COUNT_SEGMENT) { |
||
| 401 | throw ODataException::createBadRequestError( |
||
| 402 | Messages::segmentParserCannotQueryCollection($previous->getIdentifier()) |
||
| 403 | ); |
||
| 404 | } |
||
| 405 | |||
| 406 | $current = new SegmentDescriptor(); |
||
| 407 | $current->setIdentifier($identifier); |
||
| 408 | $current->setTargetSource(TargetSource::PROPERTY()); |
||
| 409 | $previousType = $previous->getTargetResourceType(); |
||
| 410 | $projectedProperty = $previousType->resolveProperty($identifier); |
||
| 411 | $current->setProjectedProperty($projectedProperty); |
||
| 412 | |||
| 413 | if ($identifier === ODataConstants::URI_COUNT_SEGMENT) { |
||
| 414 | if ($previousKind != TargetKind::RESOURCE()) { |
||
| 415 | throw ODataException::createBadRequestError( |
||
| 416 | Messages::segmentParserCountCannotBeApplied($previous->getIdentifier()) |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | |||
| 420 | if ($previous->isSingleResult()) { |
||
| 421 | throw ODataException::createBadRequestError( |
||
| 422 | Messages::segmentParserCountCannotFollowSingleton($previous->getIdentifier()) |
||
| 423 | ); |
||
| 424 | } |
||
| 425 | |||
| 426 | $current->setTargetKind(TargetKind::PRIMITIVE_VALUE()); |
||
| 427 | $current->setSingleResult(true); |
||
| 428 | $current->setTargetResourceSetWrapper( |
||
| 429 | $previous->getTargetResourceSetWrapper() |
||
| 430 | ); |
||
| 431 | $current->setTargetResourceType( |
||
| 432 | $previous->getTargetResourceType() |
||
| 433 | ); |
||
| 434 | } elseif ($identifier === ODataConstants::URI_VALUE_SEGMENT |
||
| 435 | && $previousKind == TargetKind::RESOURCE() |
||
| 436 | ) { |
||
| 437 | $current->setSingleResult(true); |
||
| 438 | $current->setTargetResourceType( |
||
| 439 | $previous->getTargetResourceType() |
||
| 440 | ); |
||
| 441 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
| 442 | } elseif (null === $projectedProperty) { |
||
| 443 | if (null !== $previous->getTargetResourceType() |
||
| 444 | && null !== $previous->getTargetResourceType()->tryResolveNamedStreamByName($identifier) |
||
| 445 | ) { |
||
| 446 | $current->setTargetKind(TargetKind::MEDIA_RESOURCE()); |
||
| 447 | $current->setSingleResult(true); |
||
| 448 | $current->setTargetResourceType( |
||
| 449 | $previous->getTargetResourceType() |
||
| 450 | ); |
||
| 451 | } else { |
||
| 452 | throw ODataException::createResourceNotFoundError($identifier); |
||
| 453 | } |
||
| 454 | } else { |
||
| 455 | $current->setTargetResourceType($projectedProperty->getResourceType()); |
||
| 456 | $rawKind = $projectedProperty->getKind(); |
||
| 457 | $current->setSingleResult($rawKind != ResourcePropertyKind::RESOURCESET_REFERENCE()); |
||
| 458 | if ($previousKind == TargetKind::LINK() |
||
| 459 | && $projectedProperty->getTypeKind() != ResourceTypeKind::ENTITY() |
||
| 460 | ) { |
||
| 461 | throw ODataException::createBadRequestError( |
||
| 462 | Messages::segmentParserLinkSegmentMustBeFollowedByEntitySegment( |
||
| 463 | $identifier |
||
| 464 | ) |
||
| 465 | ); |
||
| 466 | } |
||
| 467 | |||
| 468 | switch ($rawKind) { |
||
| 469 | case ResourcePropertyKind::COMPLEX_TYPE(): |
||
| 470 | $current->setTargetKind(TargetKind::COMPLEX_OBJECT()); |
||
| 471 | break; |
||
| 472 | case ResourcePropertyKind::BAG()->setPRIMITIVE(true): |
||
| 473 | case ResourcePropertyKind::BAG()->setCOMPLEX_TYPE(true): |
||
| 474 | $current->setTargetKind(TargetKind::BAG()); |
||
| 475 | break; |
||
| 476 | case ResourcePropertyKind::RESOURCE_REFERENCE(): |
||
| 477 | case ResourcePropertyKind::RESOURCESET_REFERENCE(): |
||
| 478 | $current->setTargetKind(TargetKind::RESOURCE()); |
||
| 479 | $prevResource = $previous->getTargetResourceType(); |
||
| 480 | $this->assertion($prevResource instanceof ResourceEntityType); |
||
| 481 | $resourceSetWrapper = $this->providerWrapper->getResourceSetWrapperForNavigationProperty( |
||
| 482 | $previous->getTargetResourceSetWrapper(), |
||
| 483 | $prevResource, |
||
| 484 | $projectedProperty |
||
| 485 | ); |
||
| 486 | if (null === $resourceSetWrapper) { |
||
| 487 | throw ODataException::createResourceNotFoundError($projectedProperty->getName()); |
||
| 488 | } |
||
| 489 | |||
| 490 | $current->setTargetResourceSetWrapper($resourceSetWrapper); |
||
| 491 | break; |
||
| 492 | default: |
||
| 493 | if (!$projectedProperty->isKindOf(ResourcePropertyKind::PRIMITIVE())) { |
||
| 494 | throw ODataException::createInternalServerError( |
||
| 495 | Messages::segmentParserUnExpectedPropertyKind('Primitive') |
||
| 496 | ); |
||
| 497 | } |
||
| 498 | |||
| 499 | $current->setTargetKind(TargetKind::PRIMITIVE()); |
||
| 500 | break; |
||
| 501 | } |
||
| 502 | |||
| 503 | if ($hasPredicate) { |
||
|
|
|||
| 504 | $this->assertion(!$current->isSingleResult()); |
||
| 505 | $keyDescriptor = $this->createKeyDescriptor( |
||
| 506 | $identifier . '(' . $keyPredicate . ')', |
||
| 507 | $projectedProperty->getResourceType(), |
||
| 508 | $keyPredicate |
||
| 509 | ); |
||
| 510 | $current->setKeyDescriptor($keyDescriptor); |
||
| 511 | if (!$keyDescriptor->isEmpty()) { |
||
| 512 | $current->setSingleResult(true); |
||
| 513 | } |
||
| 514 | } |
||
| 515 | |||
| 516 | if ($checkRights && null !== $current->getTargetResourceSetWrapper()) { |
||
| 517 | $current->getTargetResourceSetWrapper() |
||
| 518 | ->checkResourceSetRightsForRead( |
||
| 519 | $current->isSingleResult() |
||
| 520 | ); |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | |||
| 525 | return $current; |
||
| 526 | } |
||
| 528 |