-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathTemplateTypeScope.php
More file actions
95 lines (75 loc) · 2 KB
/
TemplateTypeScope.php
File metadata and controls
95 lines (75 loc) · 2 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
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare(strict_types = 1);
namespace PHPStan\Type\Generic;
use function sprintf;
use function str_starts_with;
use function strlen;
use function substr;
final class TemplateTypeScope
{
public static function createWithAnonymousFunction(): self
{
return new self(null, null);
}
public static function createWithTypeAlias(string $className, string $aliasName): self
{
return new self($className, '__typeAlias_' . $aliasName);
}
public static function createWithFunction(string $functionName): self
{
return new self(null, $functionName);
}
public static function createWithMethod(string $className, string $functionName): self
{
return new self($className, $functionName);
}
public static function createWithClass(string $className): self
{
return new self($className, null);
}
private function __construct(private ?string $className, private ?string $functionName)
{
}
/** @api */
public function getClassName(): ?string
{
return $this->className;
}
/** @api */
public function getFunctionName(): ?string
{
return $this->functionName;
}
/** @api */
public function isTypeAlias(): bool
{
return $this->functionName !== null && str_starts_with($this->functionName, '__typeAlias_');
}
/** @api */
public function getTypeAliasName(): ?string
{
if (!$this->isTypeAlias() || $this->functionName === null) {
return null;
}
return substr($this->functionName, strlen('__typeAlias_'));
}
/** @api */
public function equals(self $other): bool
{
return $this->className === $other->className
&& $this->functionName === $other->functionName;
}
/** @api */
public function describe(): string
{
if ($this->className === null && $this->functionName === null) {
return 'anonymous function';
}
if ($this->className === null) {
return sprintf('function %s()', $this->functionName);
}
if ($this->functionName === null) {
return sprintf('class %s', $this->className);
}
return sprintf('method %s::%s()', $this->className, $this->functionName);
}
}