Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use ReflectionAttribute;
use function count;

Expand All @@ -32,8 +34,7 @@

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getDeclaringClass()->getName() === $this->className
&& $methodReflection->getName() === 'getAttributes';
return $methodReflection->getName() === 'getAttributes';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
Expand All @@ -44,7 +45,34 @@
$argType = $scope->getType($methodCall->getArgs()[0]->value);
$classType = $argType->getClassStringObjectType();

return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new GenericObjectType(ReflectionAttribute::class, [$classType])), new AccessoryArrayListType()]);
$valueType = $this->resolveReflectionAttributeType($methodReflection, $classType);

return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), $valueType), new AccessoryArrayListType()]);
}

private function resolveReflectionAttributeType(MethodReflection $methodReflection, Type $classType): Type
{
$returnType = $methodReflection->getVariants()[0]->getReturnType();
$nativeReflectionAttributeType = new ObjectType(ReflectionAttribute::class);

$valueTypes = [];
foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) {
if (!$nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) {

Check warning on line 60 in src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $valueTypes = []; foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) { - if (!$nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) { + if ($nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->no()) { continue; }

Check warning on line 60 in src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $valueTypes = []; foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) { - if (!$nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) { + if (!(new ObjectType($className))->isSuperTypeOf($nativeReflectionAttributeType)->yes()) { continue; }

Check warning on line 60 in src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $valueTypes = []; foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) { - if (!$nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) { + if ($nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->no()) { continue; }

Check warning on line 60 in src/Type/Php/ReflectionGetAttributesMethodReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $valueTypes = []; foreach ($returnType->getIterableValueType()->getObjectClassNames() as $className) { - if (!$nativeReflectionAttributeType->isSuperTypeOf(new ObjectType($className))->yes()) { + if (!(new ObjectType($className))->isSuperTypeOf($nativeReflectionAttributeType)->yes()) { continue; }
continue;
}

$valueTypes[] = new GenericObjectType($className, [$classType]);
}

if (count($valueTypes) === 0) {
return new GenericObjectType(ReflectionAttribute::class, [$classType]);
}

if (count($valueTypes) === 1) {
return $valueTypes[0];
}
Comment on lines +59 to +65
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would creates an issue with a method which returns something like array<Attribute1|Attribute2> since you're matching the first one only.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both make tests and make phpstan pass cleanly.

Summary of the change:

In resolveReflectionAttributeClassName, instead of returning the first class name from the method's return type that is a subtype of ReflectionAttribute, the code now collects all matching class names and only uses the result when there is exactly one unambiguous match. When there are zero or multiple matches (e.g., a return type like array<Attribute1|Attribute2> where both extend ReflectionAttribute), it falls back to the native ReflectionAttribute::class.

This handles the existing cases correctly:

  • Native reflection: return type value is ReflectionAttribute → one match → uses it
  • BetterReflection adapters: return type value is BRAdapter\ReflectionAttribute|FakeReflectionAttribute → one match (only the adapter extends native ReflectionAttribute) → uses it
  • Hypothetical Attribute1|Attribute2: two matches → falls back to ReflectionAttribute::class (the common base)


return TypeCombinator::union(...$valueTypes);
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14466.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14466;

use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
use function PHPStan\Testing\assertType;

interface I
{

}

class Bug
{
/**
* @param ReflectionClass<*> $object
*/
protected function c(ReflectionClass $object): void
{
$requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF);

assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements);
}

/**
* @param ReflectionMethod $object
*/
protected function m(ReflectionMethod $object): void
{
$requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF);

assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements);
}

/**
* @param ReflectionClass<*>|ReflectionMethod $object
*/
protected function classOrMethod(ReflectionClass|ReflectionMethod $object): void
{
$requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF);

assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements);
}

/**
* @param ReflectionClass<*>|\ReflectionProperty $object
*/
protected function classOrProperty(ReflectionClass|\ReflectionProperty $object): void
{
$requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF);

assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements);
}

/**
* @param ReflectionMethod|\ReflectionProperty $object
*/
protected function methodOrProperty(ReflectionMethod|\ReflectionProperty $object): void
{
$requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF);

assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements);
}
}
Loading