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 nullbool $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.         return self::getTypeHintForType($type$r$noBuiltin);
  32.     }
  33.     private static function getTypeHintForType(\ReflectionType $type\ReflectionFunctionAbstract $rbool $noBuiltin): ?string
  34.     {
  35.         $types = [];
  36.         $glue '|';
  37.         if ($type instanceof \ReflectionUnionType) {
  38.             $reflectionTypes $type->getTypes();
  39.         } elseif ($type instanceof \ReflectionIntersectionType) {
  40.             $reflectionTypes $type->getTypes();
  41.             $glue '&';
  42.         } elseif ($type instanceof \ReflectionNamedType) {
  43.             $reflectionTypes = [$type];
  44.         } else {
  45.             return null;
  46.         }
  47.         foreach ($reflectionTypes as $type) {
  48.             if ($type instanceof \ReflectionIntersectionType) {
  49.                 $typeHint self::getTypeHintForType($type$r$noBuiltin);
  50.                 if (null === $typeHint) {
  51.                     return null;
  52.                 }
  53.                 $types[] = sprintf('(%s)'$typeHint);
  54.                 continue;
  55.             }
  56.             if ($type->isBuiltin()) {
  57.                 if (!$noBuiltin) {
  58.                     $types[] = $type->getName();
  59.                 }
  60.                 continue;
  61.             }
  62.             $lcName strtolower($type->getName());
  63.             $prefix $noBuiltin '' '\\';
  64.             if ('self' !== $lcName && 'parent' !== $lcName) {
  65.                 $types[] = $prefix.$type->getName();
  66.                 continue;
  67.             }
  68.             if (!$r instanceof \ReflectionMethod) {
  69.                 continue;
  70.             }
  71.             if ('self' === $lcName) {
  72.                 $types[] = $prefix.$r->getDeclaringClass()->name;
  73.             } else {
  74.                 $types[] = ($parent $r->getDeclaringClass()->getParentClass()) ? $prefix.$parent->name null;
  75.             }
  76.         }
  77.         sort($types);
  78.         return $types implode($glue$types) : null;
  79.     }
  80. }