Skip to content

Commit a0defb9

Browse files
committed
Initialize properties through a constructor callee that an override may replace
A method of the class the constructor calls on $this initialized properties only when the call ran exactly that method. When a subclass could override it, the properties it assigns were reported as uninitialized, and reads after the call as accesses to an uninitialized property. An override can call the method through parent::, so the report is not always right. The call now takes the initialized properties from the method's walk either way; the types of the properties it writes are still taken only when it runs exactly that method. Amp-Thread-ID: https://ampcode.com/threads/T-01a1ffd7-32dc-7043-b3ac-2d8cf42d2441
1 parent 9414cd8 commit a0defb9

4 files changed

Lines changed: 41 additions & 21 deletions

File tree

‎src/Analyser/PreservedProperties/PreservedPropertiesResolver.php‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,24 @@ private function findTraitScan(array $stmts, string $traitName): ?PreservedPrope
130130
}
131131

132132
/**
133-
* After a call of a method of the class on $this that runs exactly that method - a private
134-
* or final one, or one called through self:: - the properties the walk of the method ended
135-
* with initialized are initialized, and the properties of $this the class's own code the call
136-
* runs writes get the types the walk ended with, but the ones the class's closures write, which
137-
* can run whenever. ClassLikeHandler walks the methods before
138-
* their callers.
133+
* After a call of a method of the class on $this, the properties the walk of the method ended
134+
* with initialized are initialized, even when an override may run instead: the override can
135+
* call it through parent::. When the call runs exactly that method - a private or final one,
136+
* or one called through self:: - the properties of $this the class's own code the call runs
137+
* writes also get the types the walk ended with, but the ones the class's closures write, which
138+
* can run whenever. ClassLikeHandler walks the methods before their callers.
139139
*/
140140
public function applyCalledMethodExitScope(MutatingScope $scope, Expr $call, MethodReflection $methodReflection): MutatingScope
141141
{
142142
$walk = $this->findWalk($scope, $call, $methodReflection);
143-
if ($walk === null || !$walk[1]) {
143+
if ($walk === null) {
144144
return $scope;
145145
}
146146
$exitScope = $walk[0];
147147
$scope = $scope->mergeInitializedProperties($exitScope);
148+
if (!$walk[1]) {
149+
return $scope;
150+
}
148151

149152
$preservedProperties = $this->getPreservedProperties($call);
150153
if (!$preservedProperties instanceof PreservedProperties) {

‎tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,6 @@ public function testBug7649(): void
197197
public function testBug9577(): void
198198
{
199199
$this->analyse([__DIR__ . '/../Classes/data/bug-9577.php'], [
200-
[
201-
'Class Bug9577\SpecializedException has an uninitialized readonly property $message. Assign it in the constructor.',
202-
8,
203-
],
204200
[
205201
'Class Bug9577\SpecializedException2 has an uninitialized readonly property $message. Assign it in the constructor.',
206202
8,

‎tests/PHPStan/Rules/Properties/UninitializedPropertyRuleTest.php‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ public function testRule(): void
7272
'Class UninitializedProperty\FooTraitClass has an uninitialized property $baz. Give it default value or assign it in the constructor.',
7373
159,
7474
],
75-
[
76-
'Class UninitializedProperty\InitializedInPublicSetterNonFinalClass has an uninitialized property $foo. Give it default value or assign it in the constructor.',
77-
263,
78-
],
79-
[
75+
/*[
8076
'Access to an uninitialized property UninitializedProperty\InitializedInPublicSetterNonFinalClass::$foo.',
8177
278,
82-
],
78+
],*/
8379
[
8480
'Class UninitializedProperty\SometimesInitializedInPrivateSetter has an uninitialized property $foo. Give it default value or assign it in the constructor.',
8581
286,
@@ -133,10 +129,6 @@ public function testAdditionalConstructorsExtension(): void
133129
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $three. Give it default value or assign it in the constructor.',
134130
11,
135131
],
136-
[
137-
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $four. Give it default value or assign it in the constructor.',
138-
13,
139-
],
140132
[
141133
'Class TestInitializedProperty\TestAdditionalConstructor has an uninitialized property $one. Give it default value or assign it in the constructor.',
142134
07,

‎tests/PHPStan/Rules/Properties/data/uninitialized-property.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,32 @@ private function returnNever()
470470
}
471471

472472
}
473+
474+
class InitializedInProtectedSetterThatMayBeOverridden
475+
{
476+
477+
private float $amount;
478+
479+
private string $currency;
480+
481+
public function __construct(float $amount, string $currency)
482+
{
483+
$this->setAmount($amount, $currency);
484+
echo $this->amount;
485+
}
486+
487+
protected function setAmount(float $amount, string $currency): void
488+
{
489+
if ($currency !== 'USD') {
490+
throw new \LogicException('USD only');
491+
}
492+
$this->amount = $amount;
493+
$this->currency = $currency;
494+
}
495+
496+
public function getAmount(): float
497+
{
498+
return $this->currency === 'USD' ? $this->amount : 0.0;
499+
}
500+
501+
}

0 commit comments

Comments
 (0)