-
Notifications
You must be signed in to change notification settings - Fork 462
/
ConstructorsHelper.php
77 lines (63 loc) · 2.08 KB
/
ConstructorsHelper.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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection;
use PHPStan\DependencyInjection\Container;
use ReflectionException;
use function array_key_exists;
use function explode;
final class ConstructorsHelper
{
/** @var array<string, list<string>> */
private array $additionalConstructorsCache = [];
/**
* @param list<string> $additionalConstructors
*/
public function __construct(
private Container $container,
private array $additionalConstructors,
)
{
}
/**
* @return list<string>
*/
public function getConstructors(ClassReflection $classReflection): array
{
if (array_key_exists($classReflection->getName(), $this->additionalConstructorsCache)) {
return $this->additionalConstructorsCache[$classReflection->getName()];
}
$constructors = [];
if ($classReflection->hasConstructor()) {
$constructors[] = $classReflection->getConstructor()->getName();
}
/** @var AdditionalConstructorsExtension[] $extensions */
$extensions = $this->container->getServicesByTag(AdditionalConstructorsExtension::EXTENSION_TAG);
foreach ($extensions as $extension) {
$extensionConstructors = $extension->getAdditionalConstructors($classReflection);
foreach ($extensionConstructors as $extensionConstructor) {
$constructors[] = $extensionConstructor;
}
}
$nativeReflection = $classReflection->getNativeReflection();
foreach ($this->additionalConstructors as $additionalConstructor) {
[$className, $methodName] = explode('::', $additionalConstructor);
if (!$nativeReflection->hasMethod($methodName)) {
continue;
}
$nativeMethod = $nativeReflection->getMethod($methodName);
if ($nativeMethod->getDeclaringClass()->getName() !== $nativeReflection->getName()) {
continue;
}
try {
$prototype = $nativeMethod->getPrototype();
} catch (ReflectionException) {
$prototype = $nativeMethod;
}
if ($prototype->getDeclaringClass()->getName() !== $className) {
continue;
}
$constructors[] = $methodName;
}
$this->additionalConstructorsCache[$classReflection->getName()] = $constructors;
return $constructors;
}
}