Skip to content

Commit 2a6c5c5

Browse files
committed
Fix canChangeTypeAfterAssignment
1 parent 6e0c439 commit 2a6c5c5

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/Reflection/Php/PhpPropertyReflection.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public function getWritableType(): Type
116116

117117
public function canChangeTypeAfterAssignment(): bool
118118
{
119+
if ($this->isStatic()) {
120+
return true;
121+
}
122+
123+
if ($this->isVirtual()->yes()) {
124+
return false;
125+
}
126+
127+
if ($this->hasHook('get')) {
128+
return false;
129+
}
130+
131+
if ($this->hasHook('set')) {
132+
return false;
133+
}
134+
119135
return true;
120136
}
121137

tests/PHPStan/Analyser/nsrt/property-hooks.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,49 @@ public function __construct(
311311
}
312312

313313
}
314+
315+
class CanChangeTypeAfterAssignment
316+
{
317+
318+
public int $i;
319+
320+
public function doFoo(): void
321+
{
322+
assertType('int', $this->i);
323+
$this->i = 1;
324+
assertType('1', $this->i);
325+
}
326+
327+
public int $virtual {
328+
get {
329+
return 1;
330+
}
331+
set {
332+
$this->i = 1;
333+
}
334+
}
335+
336+
public function doFoo2(): void
337+
{
338+
assertType('int', $this->virtual);
339+
$this->virtual = 1;
340+
assertType('int', $this->virtual);
341+
}
342+
343+
public int $backedWithHook {
344+
get {
345+
return $this->backedWithHook + 100;
346+
}
347+
set {
348+
$this->backedWithHook = $this->backedWithHook - 200;
349+
}
350+
}
351+
352+
public function doFoo3(): void
353+
{
354+
assertType('int', $this->backedWithHook);
355+
$this->backedWithHook = 1;
356+
assertType('int', $this->backedWithHook);
357+
}
358+
359+
}

0 commit comments

Comments
 (0)