ProxyHelper.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, $noBuiltin = false)
  22. {
  23. if ($p instanceof \ReflectionParameter) {
  24. if (method_exists($p, 'getType')) {
  25. $type = $p->getType();
  26. } elseif (preg_match('/^(?:[^ ]++ ){4}([a-zA-Z_\x7F-\xFF][^ ]++)/', $p, $type)) {
  27. $name = $type = $type[1];
  28. if ('callable' === $name || 'array' === $name) {
  29. return $noBuiltin ? null : $name;
  30. }
  31. }
  32. } else {
  33. $type = method_exists($r, 'getReturnType') ? $r->getReturnType() : null;
  34. }
  35. if (!$type) {
  36. return;
  37. }
  38. if (!is_string($type)) {
  39. $name = $type instanceof \ReflectionNamedType ? $type->getName() : $type->__toString();
  40. if ($type->isBuiltin()) {
  41. return $noBuiltin ? null : $name;
  42. }
  43. }
  44. $lcName = strtolower($name);
  45. $prefix = $noBuiltin ? '' : '\\';
  46. if ('self' !== $lcName && 'parent' !== $lcName) {
  47. return $prefix.$name;
  48. }
  49. if (!$r instanceof \ReflectionMethod) {
  50. return;
  51. }
  52. if ('self' === $lcName) {
  53. return $prefix.$r->getDeclaringClass()->name;
  54. }
  55. if ($parent = $r->getDeclaringClass()->getParentClass()) {
  56. return $prefix.$parent->name;
  57. }
  58. }
  59. private static function export($value)
  60. {
  61. if (!is_array($value)) {
  62. return var_export($value, true);
  63. }
  64. $code = array();
  65. foreach ($value as $k => $v) {
  66. $code[] = sprintf('%s => %s', var_export($k, true), self::export($v));
  67. }
  68. return sprintf('array(%s)', implode(', ', $code));
  69. }
  70. }