No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NameScope.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\PropertyInfo\PhpStan;
  11. /**
  12. * NameScope class adapted from PHPStan code.
  13. *
  14. * @copyright Copyright (c) 2016, PHPStan https://github.com/phpstan/phpstan-src
  15. * @copyright Copyright (c) 2016, Ondřej Mirtes
  16. * @author Baptiste Leduc <baptiste.leduc@gmail.com>
  17. *
  18. * @internal
  19. */
  20. final class NameScope
  21. {
  22. private $calledClassName;
  23. private $namespace;
  24. /** @var array<string, string> alias(string) => fullName(string) */
  25. private $uses;
  26. public function __construct(string $calledClassName, string $namespace, array $uses = [])
  27. {
  28. $this->calledClassName = $calledClassName;
  29. $this->namespace = $namespace;
  30. $this->uses = $uses;
  31. }
  32. public function resolveStringName(string $name): string
  33. {
  34. if (0 === strpos($name, '\\')) {
  35. return ltrim($name, '\\');
  36. }
  37. $nameParts = explode('\\', $name);
  38. $firstNamePart = $nameParts[0];
  39. if (isset($this->uses[$firstNamePart])) {
  40. if (1 === \count($nameParts)) {
  41. return $this->uses[$firstNamePart];
  42. }
  43. array_shift($nameParts);
  44. return sprintf('%s\\%s', $this->uses[$firstNamePart], implode('\\', $nameParts));
  45. }
  46. if (null !== $this->namespace) {
  47. return sprintf('%s\\%s', $this->namespace, $name);
  48. }
  49. return $name;
  50. }
  51. public function resolveRootClass(): string
  52. {
  53. return $this->resolveStringName($this->calledClassName);
  54. }
  55. }