Skip to content

Commit 851ab2c

Browse files
committed
Object with dynamic property assignments cannot report too-wide types on properties
1 parent bde7903 commit 851ab2c

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/Rules/TooWideTypehints/TooWideTypeCheck.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public function checkProperty(
6262
foreach ($propertyAssigns as $assign) {
6363
$assignNode = $assign->getAssign();
6464
$assignPropertyReflections = $this->propertyReflectionFinder->findPropertyReflectionsFromNode($assignNode->getPropertyFetch(), $assign->getScope());
65+
if (count($assignPropertyReflections) === 0) {
66+
return [];
67+
}
6568
foreach ($assignPropertyReflections as $assignPropertyReflection) {
6669
if ($node->getName() !== $assignPropertyReflection->getName()) {
6770
continue;

tests/PHPStan/Rules/TooWideTypehints/TooWidePropertyTypeRuleTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,11 @@ public function testNestedTooWideType(): void
131131
]);
132132
}
133133

134+
public function testBug13624(): void
135+
{
136+
$this->reportTooWideBool = true;
137+
$this->reportNestedTooWideType = true;
138+
$this->analyse([__DIR__ . '/data/bug-13624.php'], []);
139+
}
140+
134141
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Bug13624;
4+
5+
class Example
6+
{
7+
8+
/** @var ?string */
9+
private $exampleProperty;
10+
/** @var string */
11+
private $exampleProperty2;
12+
/** @var ?int */
13+
private $exampleProperty3;
14+
/** @var int */
15+
private $exampleProperty4;
16+
/** This is fine if uninitialized property warnings are off */
17+
private ?int $exampleProperty5;
18+
19+
20+
/** @param array<string, int|float|string|null> $row */
21+
public static function from_db_row(array $row): self
22+
{
23+
$course = new self();
24+
foreach ($row as $key => $value) {
25+
$course->{$key} = $value;
26+
}
27+
return $course;
28+
}
29+
30+
}

0 commit comments

Comments
 (0)