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