1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Ast\Type;
4:
5: use PHPStan\PhpDocParser\Ast\NodeAttributes;
6: use function implode;
7:
8: class ArrayShapeNode implements TypeNode
9: {
10:
11: public const KIND_ARRAY = 'array';
12: public const KIND_LIST = 'list';
13:
14: use NodeAttributes;
15:
16: /** @var ArrayShapeItemNode[] */
17: public array $items;
18:
19: public bool $sealed;
20:
21: /** @var self::KIND_* */
22: public $kind;
23:
24: public ?ArrayShapeUnsealedTypeNode $unsealedType = null;
25:
26: /**
27: * @param ArrayShapeItemNode[] $items
28: * @param self::KIND_* $kind
29: */
30: private function __construct(
31: array $items,
32: bool $sealed = true,
33: ?ArrayShapeUnsealedTypeNode $unsealedType = null,
34: string $kind = self::KIND_ARRAY
35: )
36: {
37: $this->items = $items;
38: $this->sealed = $sealed;
39: $this->unsealedType = $unsealedType;
40: $this->kind = $kind;
41: }
42:
43:
44: /**
45: * @param ArrayShapeItemNode[] $items
46: * @param self::KIND_* $kind
47: */
48: public static function createSealed(array $items, string $kind = self::KIND_ARRAY): self
49: {
50: return new self($items, true, null, $kind);
51: }
52:
53: /**
54: * @param ArrayShapeItemNode[] $items
55: * @param self::KIND_* $kind
56: */
57: public static function createUnsealed(array $items, ?ArrayShapeUnsealedTypeNode $unsealedType, string $kind = self::KIND_ARRAY): self
58: {
59: return new self($items, false, $unsealedType, $kind);
60: }
61:
62:
63: public function __toString(): string
64: {
65: $items = $this->items;
66:
67: if (! $this->sealed) {
68: $items[] = '...' . $this->unsealedType;
69: }
70:
71: return $this->kind . '{' . implode(', ', $items) . '}';
72: }
73:
74: }
75: