1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Ast\Type;
4:
5: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
6: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
7: use PHPStan\PhpDocParser\Ast\NodeAttributes;
8: use function sprintf;
9:
10: class ArrayShapeItemNode implements TypeNode
11: {
12:
13: use NodeAttributes;
14:
15: /** @var ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null */
16: public $keyName;
17:
18: /** @var bool */
19: public $optional;
20:
21: /** @var TypeNode */
22: public $valueType;
23:
24: /**
25: * @param ConstExprIntegerNode|ConstExprStringNode|IdentifierTypeNode|null $keyName
26: */
27: public function __construct($keyName, bool $optional, TypeNode $valueType)
28: {
29: $this->keyName = $keyName;
30: $this->optional = $optional;
31: $this->valueType = $valueType;
32: }
33:
34:
35: public function __toString(): string
36: {
37: if ($this->keyName !== null) {
38: return sprintf(
39: '%s%s: %s',
40: (string) $this->keyName,
41: $this->optional ? '?' : '',
42: (string) $this->valueType
43: );
44: }
45:
46: return (string) $this->valueType;
47: }
48:
49: }
50: