ProxyHelper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\DependencyInjection\LazyProxy;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. *
  14. * @internal
  15. */
  16. class ProxyHelper
  17. {
  18. /**
  19. * @return string|null The FQCN or builtin name of the type hint, or null when the type hint references an invalid self|parent context
  20. */
  21. public static function getTypeHint(\ReflectionFunctionAbstract $r, \ReflectionParameter $p = null, bool $noBuiltin = false): ?string
  22. {
  23. if ($p instanceof \ReflectionParameter) {
  24. $type = $p->getType();
  25. } else {
  26. $type = $r->getReturnType();
  27. }
  28. if (!$type) {
  29. return null;
  30. }
  31. $types = [];
  32. foreach ($type instanceof \ReflectionUnionType ? $type->getTypes() : [$type] as $type) {
  33. $name = $type instanceof \ReflectionNamedType ? $type->getName() : (string) $type;
  34. if ($type->isBuiltin()) {
  35. if (!$noBuiltin) {
  36. $types[] = $name;
  37. }
  38. continue;
  39. }
  40. $lcName = strtolower($name);
  41. $prefix = $noBuiltin ? '' : '\\';
  42. if ('self' !== $lcName && 'parent' !== $lcName) {
  43. $types[] = '' !== $prefix ? $prefix.$name : $name;
  44. continue;
  45. }
  46. if (!$r instanceof \ReflectionMethod) {
  47. continue;
  48. }
  49. if ('self' === $lcName) {
  50. $types[] = $prefix.$r->getDeclaringClass()->name;
  51. } else {
  52. $types[] = ($parent = $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name : null;
  53. }
  54. }
  55. return $types ? implode('|', $types) : null;
  56. }
  57. }