1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
6: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\Turbo\ShadowedByTurboExtension;
9: use PHPStan\Type\Generic\TemplateTypeVariance;
10: use PHPStan\Type\Traits\LateResolvableTypeTrait;
11: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
12: use function sprintf;
13:
14: /** @api */
15: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/NewObjectType.cpp')]
16: class NewObjectType implements CompoundType, LateResolvableType
17: {
18:
19: use LateResolvableTypeTrait;
20: use NonGeneralizableTypeTrait;
21:
22: public function __construct(private Type $type)
23: {
24: }
25:
26: public function getType(): Type
27: {
28: return $this->type;
29: }
30:
31: public function getReferencedClasses(): array
32: {
33: return $this->type->getReferencedClasses();
34: }
35:
36: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
37: {
38: return $this->type->getReferencedTemplateTypes($positionVariance);
39: }
40:
41: public function equals(Type $type): bool
42: {
43: return $type instanceof self
44: && $this->type->equals($type->type);
45: }
46:
47: public function describe(VerbosityLevel $level): string
48: {
49: return sprintf('new<%s>', $this->type->describe($level));
50: }
51:
52: public function isResolvable(): bool
53: {
54: return !TypeUtils::containsTemplateType($this->type);
55: }
56:
57: protected function getResult(): Type
58: {
59: return $this->type->getObjectTypeOrClassStringObjectType();
60: }
61:
62: /**
63: * @param callable(Type): Type $cb
64: */
65: public function traverse(callable $cb): Type
66: {
67: $type = $cb($this->type);
68:
69: if ($this->type === $type) {
70: return $this;
71: }
72:
73: return new self($type);
74: }
75:
76: public function traverseSimultaneously(Type $right, callable $cb): Type
77: {
78: if (!$right instanceof self) {
79: return $this;
80: }
81:
82: $type = $cb($this->type, $right->type);
83:
84: if ($this->type === $type) {
85: return $this;
86: }
87:
88: return new self($type);
89: }
90:
91: public function toPhpDocNode(): TypeNode
92: {
93: return new GenericTypeNode(new IdentifierTypeNode('new'), [$this->type->toPhpDocNode()]);
94: }
95:
96: }
97: