vendor/symfony/dependency-injection/LazyProxy/ProxyHelper.php line 24

Open in your IDE?
  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.         $glue = '|';
  33.         if ($type instanceof \ReflectionUnionType) {
  34.             $reflectionTypes = $type->getTypes();
  35.         } elseif ($type instanceof \ReflectionIntersectionType) {
  36.             $reflectionTypes = $type->getTypes();
  37.             $glue = '&';
  38.         } elseif ($type instanceof \ReflectionNamedType) {
  39.             $reflectionTypes = [$type];
  40.         } else {
  41.             return null;
  42.         }
  43.         foreach ($reflectionTypes as $type) {
  44.             if ($type->isBuiltin()) {
  45.                 if (!$noBuiltin) {
  46.                     $types[] = $type->getName();
  47.                 }
  48.                 continue;
  49.             }
  50.             $lcName = strtolower($type->getName());
  51.             $prefix = $noBuiltin ? '' : '\\';
  52.             if ('self' !== $lcName && 'parent' !== $lcName) {
  53.                 $types[] = $prefix.$type->getName();
  54.                 continue;
  55.             }
  56.             if (!$r instanceof \ReflectionMethod) {
  57.                 continue;
  58.             }
  59.             if ('self' === $lcName) {
  60.                 $types[] = $prefix.$r->getDeclaringClass()->name;
  61.             } else {
  62.                 $types[] = ($parent = $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name : null;
  63.             }
  64.         }
  65.         sort($types);
  66.         return $types ? implode($glue, $types) : null;
  67.     }
  68. }