1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node;
4:
5: use PhpParser\Modifiers;
6: use PhpParser\Node;
7: use PhpParser\Node\Expr;
8: use PhpParser\NodeAbstract;
9: use PHPStan\Reflection\ClassReflection;
10: use PHPStan\Type\Type;
11:
12: /**
13: * @api
14: */
15: final class ClassPropertyNode extends NodeAbstract implements VirtualNode
16: {
17:
18: public function __construct(
19: private string $name,
20: private int $flags,
21: private ?Type $type,
22: private ?Expr $default,
23: private ?string $phpDoc,
24: private ?Type $phpDocType,
25: private bool $isPromoted,
26: private bool $isPromotedFromTrait,
27: private Node\Stmt\Property|Node\Param $originalNode,
28: private bool $isReadonlyByPhpDoc,
29: private bool $isDeclaredInTrait,
30: private bool $isReadonlyClass,
31: private bool $isAllowedPrivateMutation,
32: private ClassReflection $classReflection,
33: )
34: {
35: parent::__construct($originalNode->getAttributes());
36: }
37:
38: public function getName(): string
39: {
40: return $this->name;
41: }
42:
43: public function getFlags(): int
44: {
45: return $this->flags;
46: }
47:
48: public function getDefault(): ?Expr
49: {
50: return $this->default;
51: }
52:
53: public function isPromoted(): bool
54: {
55: return $this->isPromoted;
56: }
57:
58: public function isPromotedFromTrait(): bool
59: {
60: return $this->isPromotedFromTrait;
61: }
62:
63: public function getPhpDoc(): ?string
64: {
65: return $this->phpDoc;
66: }
67:
68: public function getPhpDocType(): ?Type
69: {
70: return $this->phpDocType;
71: }
72:
73: public function isPublic(): bool
74: {
75: return ($this->flags & Modifiers::PUBLIC) !== 0
76: || ($this->flags & Modifiers::VISIBILITY_MASK) === 0;
77: }
78:
79: public function isProtected(): bool
80: {
81: return (bool) ($this->flags & Modifiers::PROTECTED);
82: }
83:
84: public function isPrivate(): bool
85: {
86: return (bool) ($this->flags & Modifiers::PRIVATE);
87: }
88:
89: public function isFinal(): bool
90: {
91: return (bool) ($this->flags & Modifiers::FINAL);
92: }
93:
94: public function isStatic(): bool
95: {
96: return (bool) ($this->flags & Modifiers::STATIC);
97: }
98:
99: public function isReadOnly(): bool
100: {
101: return (bool) ($this->flags & Modifiers::READONLY) || $this->isReadonlyClass;
102: }
103:
104: public function isReadOnlyByPhpDoc(): bool
105: {
106: return $this->isReadonlyByPhpDoc;
107: }
108:
109: public function isDeclaredInTrait(): bool
110: {
111: return $this->isDeclaredInTrait;
112: }
113:
114: public function isAllowedPrivateMutation(): bool
115: {
116: return $this->isAllowedPrivateMutation;
117: }
118:
119: public function isAbstract(): bool
120: {
121: return (bool) ($this->flags & Modifiers::ABSTRACT);
122: }
123:
124: public function getNativeType(): ?Type
125: {
126: return $this->type;
127: }
128:
129: /**
130: * @return Node\Identifier|Node\Name|Node\ComplexType|null
131: */
132: public function getNativeTypeNode()
133: {
134: return $this->originalNode->type;
135: }
136:
137: public function getClassReflection(): ClassReflection
138: {
139: return $this->classReflection;
140: }
141:
142: public function getType(): string
143: {
144: return 'PHPStan_Node_ClassPropertyNode';
145: }
146:
147: /**
148: * @return string[]
149: */
150: public function getSubNodeNames(): array
151: {
152: return [];
153: }
154:
155: public function hasHooks(): bool
156: {
157: return $this->getHooks() !== [];
158: }
159:
160: /**
161: * @return Node\PropertyHook[]
162: */
163: public function getHooks(): array
164: {
165: return $this->originalNode->hooks;
166: }
167:
168: public function isVirtual(): bool
169: {
170: return $this->classReflection->getNativeProperty($this->name)->isVirtual()->yes();
171: }
172:
173: public function isWritable(): bool
174: {
175: return $this->classReflection->getNativeProperty($this->name)->isWritable();
176: }
177:
178: public function isReadable(): bool
179: {
180: return $this->classReflection->getNativeProperty($this->name)->isReadable();
181: }
182:
183: }
184: