Skip to content

Commit

Permalink
Look for overriden property prototype in implemented interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 24, 2025
1 parent 1cc5347 commit b3ca610
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Rules/Properties/OverridingPropertyRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,32 @@ private function findPrototype(ClassReflection $classReflection, string $propert
{
$parentClass = $classReflection->getParentClass();
if ($parentClass === null) {
return null;
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
}

if (!$parentClass->hasNativeProperty($propertyName)) {
return null;
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
}

$property = $parentClass->getNativeProperty($propertyName);
if ($property->isPrivate()) {
return null;
return $this->findPrototypeInInterfaces($classReflection, $propertyName);
}

return $property;
}

private function findPrototypeInInterfaces(ClassReflection $classReflection, string $propertyName): ?PhpPropertyReflection
{
foreach ($classReflection->getInterfaces() as $interface) {
if (!$interface->hasNativeProperty($propertyName)) {
continue;
}

return $interface->getNativeProperty($propertyName);
}

return null;
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Properties/OverridingPropertyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,19 @@ public function testFinal(): void
]);
}

public function testPropertyPrototypeFromInterface(): void
{
if (PHP_VERSION_ID < 80400) {
$this->markTestSkipped('Test requires PHP 8.4.');
}

$this->reportMaybes = true;
$this->analyse([__DIR__ . '/data/property-prototype-from-interface.php'], [
[
'Type string of property Bug12466\Bar::$a is not the same as type int of overridden property Bug12466\Foo::$a.',
15,
],
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php // lint >= 8.4

namespace Bug12466;

interface Foo
{

public int $a { get; set;}

}

class Bar implements Foo
{

public string $a;

}

0 comments on commit b3ca610

Please sign in to comment.