You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 428ff02
Browse filesBrowse the repository at this point in the historyBrowse files
Join the type before an overridable call with the walked type of a private property it writes
A call on $this that may run an override forgot the private properties the class's own
method writes, so a property narrowed before the call went back to its declared type -
a lazily created `?Client $client = null` became nullable again after any public method
that runs `$this->client ??= new Client()`. Only the class's own code writes a private
property, so it now has the type it had before the call or the type the walk of the
class's method ends with.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
Copy file name to clipboardExpand all lines: tests/PHPStan/Analyser/nsrt/method-call-preserved-properties.php
+50Lines changed: 50 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -52,6 +52,13 @@ public function test(): void
52
52
$callback();
53
53
}
54
54
55
+
publicfunctionoverridable(): void
56
+
{
57
+
$this->history = [];
58
+
$this->prepareOverridable();
59
+
assertType('list<int>', $this->history);
60
+
}
61
+
55
62
privatefunctionprepare(): \Closure
56
63
{
57
64
$this->history = [];
@@ -62,6 +69,13 @@ private function prepare(): \Closure
62
69
};
63
70
}
64
71
72
+
publicfunctionprepareOverridable(): bool
73
+
{
74
+
$this->history = [];
75
+
76
+
returntrue;
77
+
}
78
+
65
79
}
66
80
67
81
/** Called private method calling an overridable one: an override can write public and protected properties, not private ones. */
@@ -1384,6 +1398,7 @@ public function update(): void
1384
1398
}
1385
1399
1386
1400
}
1401
+
1387
1402
abstractclass ArgumentOverridesParent
1388
1403
{
1389
1404
@@ -1436,6 +1451,41 @@ private function load(): void
1436
1451
1437
1452
}
1438
1453
1454
+
/** An overridable callee: a private property only the class's own code writes has its old type or the type the walk of the class's method ends with. */
1455
+
class OverridableCalleeWrites
1456
+
{
1457
+
1458
+
private ?\stdClass$client = null;
1459
+
1460
+
public ?\stdClass$publicClient = null;
1461
+
1462
+
private ?int$sometimes = null;
1463
+
1464
+
publicfunctionrun(): void
1465
+
{
1466
+
if ($this->client === null || $this->publicClient === null) {
1467
+
return;
1468
+
}
1469
+
$this->sometimes = 1;
1470
+
$this->connect();
1471
+
assertType('stdClass', $this->client);
1472
+
assertType('stdClass|null', $this->publicClient);
1473
+
assertType('int|null', $this->sometimes); // the walk starts from the declared type
0 commit comments