ProxyHelper.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. if (!\is_string($type)) {
  32. $name = $type->getName();
  33. if ($type->isBuiltin()) {
  34. return $noBuiltin ? null : $name;
  35. }
  36. }
  37. $lcName = strtolower($name);
  38. $prefix = $noBuiltin ? '' : '\\';
  39. if ('self' !== $lcName && 'parent' !== $lcName) {
  40. return $prefix.$name;
  41. }
  42. if (!$r instanceof \ReflectionMethod) {
  43. return null;
  44. }
  45. if ('self' === $lcName) {
  46. return $prefix.$r->getDeclaringClass()->name;
  47. }
  48. return ($parent = $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name : null;
  49. }
  50. }