/vendor/phpdocumentor/type-resolver/src/FqsenResolver.php

https://bitbucket.org/alan_cordova/api-sb-map · PHP · 77 lines · 35 code · 13 blank · 29 comment · 4 complexity · 3f5474dec4d12cedbcde17f7fc06cc78 MD5 · raw file

  1. <?php
  2. /**
  3. * This file is part of phpDocumentor.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright 2010-2015 Mike van Riel<mike@phpdoc.org>
  9. * @license http://www.opensource.org/licenses/mit-license.php MIT
  10. * @link http://phpdoc.org
  11. */
  12. namespace phpDocumentor\Reflection;
  13. use phpDocumentor\Reflection\Types\Context;
  14. class FqsenResolver
  15. {
  16. /** @var string Definition of the NAMESPACE operator in PHP */
  17. const OPERATOR_NAMESPACE = '\\';
  18. public function resolve($fqsen, Context $context = null)
  19. {
  20. if ($context === null) {
  21. $context = new Context('');
  22. }
  23. if ($this->isFqsen($fqsen)) {
  24. return new Fqsen($fqsen);
  25. }
  26. return $this->resolvePartialStructuralElementName($fqsen, $context);
  27. }
  28. /**
  29. * Tests whether the given type is a Fully Qualified Structural Element Name.
  30. *
  31. * @param string $type
  32. *
  33. * @return bool
  34. */
  35. private function isFqsen($type)
  36. {
  37. return strpos($type, self::OPERATOR_NAMESPACE) === 0;
  38. }
  39. /**
  40. * Resolves a partial Structural Element Name (i.e. `Reflection\DocBlock`) to its FQSEN representation
  41. * (i.e. `\phpDocumentor\Reflection\DocBlock`) based on the Namespace and aliases mentioned in the Context.
  42. *
  43. * @param string $type
  44. * @param Context $context
  45. *
  46. * @return Fqsen
  47. * @throws \InvalidArgumentException when type is not a valid FQSEN.
  48. */
  49. private function resolvePartialStructuralElementName($type, Context $context)
  50. {
  51. $typeParts = explode(self::OPERATOR_NAMESPACE, $type, 2);
  52. $namespaceAliases = $context->getNamespaceAliases();
  53. // if the first segment is not an alias; prepend namespace name and return
  54. if (!isset($namespaceAliases[$typeParts[0]])) {
  55. $namespace = $context->getNamespace();
  56. if ('' !== $namespace) {
  57. $namespace .= self::OPERATOR_NAMESPACE;
  58. }
  59. return new Fqsen(self::OPERATOR_NAMESPACE . $namespace . $type);
  60. }
  61. $typeParts[0] = $namespaceAliases[$typeParts[0]];
  62. return new Fqsen(self::OPERATOR_NAMESPACE . implode(self::OPERATOR_NAMESPACE, $typeParts));
  63. }
  64. }