1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Node\Property;
4:
5: use PhpParser\Node\Expr\AssignRef;
6: use PhpParser\Node\Expr\PropertyFetch;
7: use PhpParser\Node\Expr\StaticPropertyFetch;
8: use PhpParser\Node\Param;
9: use PHPStan\Analyser\Scope;
10: use PHPStan\Node\ClassPropertyNode;
11: use PHPStan\Node\PropertyAssignNode;
12: use PHPStan\Turbo\ReferencedByTurboExtension;
13:
14: /**
15: * @api
16: */
17: #[ReferencedByTurboExtension(key: 'propertyWrite')]
18: final class PropertyWrite
19: {
20:
21: public function __construct(
22: private PropertyFetch|StaticPropertyFetch $fetch,
23: private Scope $scope,
24: private bool $promotedPropertyWrite,
25: private ClassPropertyNode|PropertyAssignNode|AssignRef|null $originalNode = null,
26: )
27: {
28: }
29:
30: /**
31: * @return PropertyFetch|StaticPropertyFetch
32: */
33: public function getFetch()
34: {
35: return $this->fetch;
36: }
37:
38: public function getScope(): Scope
39: {
40: return $this->scope;
41: }
42:
43: public function isPromotedPropertyWrite(): bool
44: {
45: return $this->promotedPropertyWrite;
46: }
47:
48: /**
49: * Whether the write binds the property to a reference ($this->prop = &$var,
50: * $var = &$this->prop, a by-ref promoted parameter), so what is later
51: * written to the property can be read through the other side.
52: */
53: public function isByReference(): bool
54: {
55: if ($this->originalNode instanceof AssignRef) {
56: return true;
57: }
58:
59: if (!$this->originalNode instanceof ClassPropertyNode) {
60: return false;
61: }
62:
63: $propertyNode = $this->originalNode->getOriginalNode();
64:
65: return $propertyNode instanceof Param && $propertyNode->byRef;
66: }
67:
68: /**
69: * Whether the write happens through offset access ($this->prop[...] = ...)
70: * on an ArrayAccess object, which goes through offsetSet() rather than
71: * reassigning the property itself.
72: */
73: public function isViaOffsetAccess(): bool
74: {
75: if (!$this->originalNode instanceof PropertyAssignNode) {
76: return false;
77: }
78:
79: return $this->originalNode->isArrayAccessOffsetWrite($this->scope);
80: }
81:
82: }
83: