1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Generic;
4:
5: use function sprintf;
6:
7: class TemplateTypeScope
8: {
9:
10: public static function createWithAnonymousFunction(): self
11: {
12: return new self(null, null);
13: }
14:
15: public static function createWithFunction(string $functionName): self
16: {
17: return new self(null, $functionName);
18: }
19:
20: public static function createWithMethod(string $className, string $functionName): self
21: {
22: return new self($className, $functionName);
23: }
24:
25: public static function createWithClass(string $className): self
26: {
27: return new self($className, null);
28: }
29:
30: private function __construct(private ?string $className, private ?string $functionName)
31: {
32: }
33:
34: /** @api */
35: public function getClassName(): ?string
36: {
37: return $this->className;
38: }
39:
40: /** @api */
41: public function getFunctionName(): ?string
42: {
43: return $this->functionName;
44: }
45:
46: /** @api */
47: public function equals(self $other): bool
48: {
49: return $this->className === $other->className
50: && $this->functionName === $other->functionName;
51: }
52:
53: /** @api */
54: public function describe(): string
55: {
56: if ($this->className === null && $this->functionName === null) {
57: return 'anonymous function';
58: }
59:
60: if ($this->className === null) {
61: return sprintf('function %s()', $this->functionName);
62: }
63:
64: if ($this->functionName === null) {
65: return sprintf('class %s', $this->className);
66: }
67:
68: return sprintf('method %s::%s()', $this->className, $this->functionName);
69: }
70:
71: /**
72: * @param mixed[] $properties
73: */
74: public static function __set_state(array $properties): self
75: {
76: return new self(
77: $properties['className'],
78: $properties['functionName'],
79: );
80: }
81:
82: }
83: