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