Skip to content

Commit bc20ce4

Browse files
committed
Overriding @property tags - take native type into account if the scope can read the native property
1 parent 3f7e878 commit bc20ce4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ private function createProperty(
430430
&& $this->annotationsPropertiesClassReflectionExtension->hasProperty($classReflection, $propertyName)
431431
&& (
432432
$nativeProperty->isPublic()
433-
|| ($scope->isInClass() && $scope->getClassReflection()->getName() !== $declaringClassReflection->getName())
433+
|| !$scope->isInClass()
434+
|| $scope->getClassReflection()->getName() !== $declaringClassReflection->getName()
434435
)
435436
) {
436437
$hierarchyDistances = $classReflection->getClassHierarchyDistances();
@@ -451,7 +452,7 @@ private function createProperty(
451452
if (
452453
$hierarchyDistances[$annotationProperty->getDeclaringClass()->getName()] <= $hierarchyDistances[$distanceDeclaringClass]
453454
) {
454-
if ($nativeType->isSuperTypeOf($annotationProperty->getReadableType())->yes()) {
455+
if ($nativeType->isSuperTypeOf($annotationProperty->getReadableType())->yes() || !$scope->canReadProperty($nativeProperty)) {
455456
$nativeType = new MixedType();
456457
}
457458

tests/PHPStan/Rules/Properties/data/bug-13537.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ function (): void {
6262

6363
assertType(stdClass::class, $bar->attributes);
6464
};
65+
66+
class Test
67+
{
68+
public function doFoo(): void
69+
{
70+
$bar = new Bar2();
71+
echo $bar->attributes->foo;
72+
73+
assertType(stdClass::class, $bar->attributes);
74+
}
75+
}

0 commit comments

Comments
 (0)