| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\TypeInfo\TypeContext; |
| 13 |
|
| 14 |
use Symfony\Component\TypeInfo\Exception\LogicException; |
| 15 |
use Symfony\Component\TypeInfo\Type; |
| 16 |
|
| 17 |
/** |
| 18 |
* Type resolving context. |
| 19 |
* |
| 20 |
* Helps to retrieve declaring class, called class, parent class, templates |
| 21 |
* and normalize classes according to the current namespace and uses. |
| 22 |
* |
| 23 |
* @author Mathias Arlaud <[email protected]> |
| 24 |
* @author Baptiste Leduc <[email protected]> |
| 25 |
*/ |
| 26 |
final class TypeContext |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var array<string, bool> |
| 30 |
*/ |
| 31 |
private static array $classExistCache = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param array<string, string> $uses |
| 35 |
* @param array<string, Type> $templates |
| 36 |
* @param array<string, Type> $typeAliases |
| 37 |
*/ |
| 38 |
public function __construct( |
| 39 |
public readonly string $calledClassName, |
| 40 |
public readonly string $declaringClassName, |
| 41 |
public readonly ?string $namespace = null, |
| 42 |
public readonly array $uses = [], |
| 43 |
public readonly array $templates = [], |
| 44 |
public readonly array $typeAliases = [], |
| 45 |
) { |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Normalize class name according to current namespace and uses. |
| 50 |
*/ |
| 51 |
public function normalize(string $name): string |
| 52 |
{ |
| 53 |
if (str_starts_with($name, '\\')) { |
| 54 |
return ltrim($name, '\\'); |
| 55 |
} |
| 56 |
|
| 57 |
$nameParts = explode('\\', $name); |
| 58 |
$firstNamePart = $nameParts[0]; |
| 59 |
if (isset($this->uses[$firstNamePart])) { |
| 60 |
if (1 === \count($nameParts)) { |
| 61 |
return $this->uses[$firstNamePart]; |
| 62 |
} |
| 63 |
array_shift($nameParts); |
| 64 |
|
| 65 |
return \sprintf('%s\\%s', $this->uses[$firstNamePart], implode('\\', $nameParts)); |
| 66 |
} |
| 67 |
|
| 68 |
if (null !== $this->namespace) { |
| 69 |
return \sprintf('%s\\%s', $this->namespace, $name); |
| 70 |
} |
| 71 |
|
| 72 |
return $name; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* @return class-string |
| 77 |
*/ |
| 78 |
public function getDeclaringClass(): string |
| 79 |
{ |
| 80 |
return $this->declaringClassName; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @return class-string |
| 85 |
*/ |
| 86 |
public function getCalledClass(): string |
| 87 |
{ |
| 88 |
return $this->calledClassName; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @return class-string |
| 93 |
*/ |
| 94 |
public function getParentClass(): string |
| 95 |
{ |
| 96 |
$declaringClassName = $this->getDeclaringClass(); |
| 97 |
|
| 98 |
if (false === $parentClass = get_parent_class($declaringClassName)) { |
| 99 |
throw new LogicException(\sprintf('"%s" do not extend any class.', $declaringClassName)); |
| 100 |
} |
| 101 |
|
| 102 |
if (!isset(self::$classExistCache[$parentClass])) { |
| 103 |
self::$classExistCache[$parentClass] = false; |
| 104 |
|
| 105 |
if (class_exists($parentClass)) { |
| 106 |
self::$classExistCache[$parentClass] = true; |
| 107 |
} else { |
| 108 |
try { |
| 109 |
new \ReflectionClass($parentClass); |
| 110 |
self::$classExistCache[$parentClass] = true; |
| 111 |
} catch (\Throwable) { |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return self::$classExistCache[$parentClass] ? $parentClass : $this->normalize(str_replace($this->namespace.'\\', '', $parentClass)); |
| 117 |
} |
| 118 |
} |