forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructorsHelper.php
More file actions
83 lines (69 loc) · 2.39 KB
/
Copy pathConstructorsHelper.php
File metadata and controls
83 lines (69 loc) · 2.39 KB
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
<?php declare(strict_types = 1);
namespace PHPStan\Reflection;
use PHPStan\DependencyInjection\AutowiredExtensions;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\DependencyInjection\ExtensionsCollection;
use ReflectionException;
use function array_key_exists;
use function explode;
#[AutowiredService]
final class ConstructorsHelper
{
/** @var array<string, list<string>> */
private array $additionalConstructorsCache = [];
/**
* @param ExtensionsCollection<AdditionalConstructorsExtension> $additionalConstructorsExtensions
* @param list<string> $additionalConstructors
*/
public function __construct(
#[AutowiredExtensions(of: AdditionalConstructorsExtension::class)]
private ExtensionsCollection $additionalConstructorsExtensions,
#[AutowiredParameter]
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();
}
$extensions = $this->additionalConstructorsExtensions->getAll();
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;
}
}