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: public const KIND_NON_EMPTY_ARRAY = 'non-empty-array';
14: public const KIND_NON_EMPTY_LIST = 'non-empty-list';
15:
16: use NodeAttributes;
17:
18: /** @var ArrayShapeItemNode[] */
19: public array $items;
20:
21: public bool $sealed;
22:
23: /** @var self::KIND_* */
24: public $kind;
25:
26: public ?ArrayShapeUnsealedTypeNode $unsealedType = null;
27:
28: /**
29: * @param ArrayShapeItemNode[] $items
30: * @param self::KIND_* $kind
31: */
32: private function __construct(
33: array $items,
34: bool $sealed = true,
35: ?ArrayShapeUnsealedTypeNode $unsealedType = null,
36: string $kind = self::KIND_ARRAY
37: )
38: {
39: $this->items = $items;
40: $this->sealed = $sealed;
41: $this->unsealedType = $unsealedType;
42: $this->kind = $kind;
43: }
44:
45:
46: /**
47: * @param ArrayShapeItemNode[] $items
48: * @param self::KIND_* $kind
49: */
50: public static function createSealed(array $items, string $kind = self::KIND_ARRAY): self
51: {
52: return new self($items, true, null, $kind);
53: }
54:
55: /**
56: * @param ArrayShapeItemNode[] $items
57: * @param self::KIND_* $kind
58: */
59: public static function createUnsealed(array $items, ?ArrayShapeUnsealedTypeNode $unsealedType, string $kind = self::KIND_ARRAY): self
60: {
61: return new self($items, false, $unsealedType, $kind);
62: }
63:
64:
65: public function __toString(): string
66: {
67: $items = $this->items;
68:
69: if (! $this->sealed) {
70: $items[] = '...' . $this->unsealedType;
71: }
72:
73: return $this->kind . '{' . implode(', ', $items) . '}';
74: }
75:
76: }
77: