Skip to content

Commit b3ca610

Browse files
committed
Look for overriden property prototype in implemented interfaces
1 parent 1cc5347 commit b3ca610

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

src/Rules/Properties/OverridingPropertyRule.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,32 @@ private function findPrototype(ClassReflection $classReflection, string $propert
213213
{
214214
$parentClass = $classReflection->getParentClass();
215215
if ($parentClass === null) {
216-
return null;
216+
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
217217
}
218218

219219
if (!$parentClass->hasNativeProperty($propertyName)) {
220-
return null;
220+
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
221221
}
222222

223223
$property = $parentClass->getNativeProperty($propertyName);
224224
if ($property->isPrivate()) {
225-
return null;
225+
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
226226
}
227227

228228
return $property;
229229
}
230230

231+
private function findPrototypeInInterfaces(ClassReflection $classReflection, string $propertyName): ?PhpPropertyReflection
232+
{
233+
foreach ($classReflection->getInterfaces() as $interface) {
234+
if (!$interface->hasNativeProperty($propertyName)) {
235+
continue;
236+
}
237+
238+
return $interface->getNativeProperty($propertyName);
239+
}
240+
241+
return null;
242+
}
243+
231244
}

tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,19 @@ public function testFinal(): void
199199
]);
200200
}
201201

202+
public function testPropertyPrototypeFromInterface(): void
203+
{
204+
if (PHP_VERSION_ID < 80400) {
205+
$this->markTestSkipped('Test requires PHP 8.4.');
206+
}
207+
208+
$this->reportMaybes = true;
209+
$this->analyse([__DIR__ . '/data/property-prototype-from-interface.php'], [
210+
[
211+
'Type string of property Bug12466\Bar::$a is not the same as type int of overridden property Bug12466\Foo::$a.',
212+
15,
213+
],
214+
]);
215+
}
216+
202217
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php // lint >= 8.4
2+
3+
namespace Bug12466;
4+
5+
interface Foo
6+
{
7+
8+
public int $a { get; set;}
9+
10+
}
11+
12+
class Bar implements Foo
13+
{
14+
15+
public string $a;
16+
17+
}

0 commit comments

Comments
 (0)