Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public function getClass(): string

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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php // lint >= 8.0

declare(strict_types=1);

namespace ReflectionAttributeGetAttributesUnion;

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

#[\Attribute]
class MyAttr {}

// Single type — worked before
function testSingle(ReflectionClass $ref): void
{
/** @var class-string<MyAttr> $class */
$class = MyAttr::class;
$attrs = $ref->getAttributes($class, ReflectionAttribute::IS_INSTANCEOF);
assertType('list<ReflectionAttribute<ReflectionAttributeGetAttributesUnion\MyAttr>>', $attrs);
assertType('ReflectionAttributeGetAttributesUnion\MyAttr', $attrs[0]->newInstance());
}

// Union type — dynamic extension should fire for all members
function testUnion(ReflectionClass|ReflectionProperty $ref): void
{
/** @var class-string<MyAttr> $class */
$class = MyAttr::class;
$attrs = $ref->getAttributes($class, ReflectionAttribute::IS_INSTANCEOF);
assertType('list<ReflectionAttribute<ReflectionAttributeGetAttributesUnion\MyAttr>>', $attrs);
assertType('ReflectionAttributeGetAttributesUnion\MyAttr', $attrs[0]->newInstance());
}

// Template T flows through union
/**
* @template T of object
* @param ReflectionClass<object>|ReflectionProperty $reflection
* @param class-string<T> $attributeClassName
* @return T
*/
function getSingleAttribute(
ReflectionClass|ReflectionProperty $reflection,
string $attributeClassName,
): object
{
$attributes = $reflection->getAttributes($attributeClassName, ReflectionAttribute::IS_INSTANCEOF);
assertType('list<ReflectionAttribute<T of object (function ReflectionAttributeGetAttributesUnion\getSingleAttribute(), argument)>>', $attributes);

$instance = $attributes[0]->newInstance();
assertType('T of object (function ReflectionAttributeGetAttributesUnion\getSingleAttribute(), argument)', $instance);

return $instance;
}
Loading