Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| GetMethodOrPropertyAttribute | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReflectionMethod | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getReflectionProperty | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Attributes; |
| 3 | |
| 4 | use Attribute; |
| 5 | use ReflectionClass; |
| 6 | use ReflectionMethod; |
| 7 | use ReflectionProperty; |
| 8 | |
| 9 | #[Attribute(Attribute::TARGET_PROPERTY)] |
| 10 | class GetMethodOrPropertyAttribute extends PropertyAttribute |
| 11 | { |
| 12 | /** |
| 13 | * @param class-string<object>|null $declaredClass |
| 14 | */ |
| 15 | public function __construct( |
| 16 | public readonly string $methodName, |
| 17 | string $propertyName, |
| 18 | ?string $declaredClass = null, |
| 19 | bool $allowLargeStrings = false |
| 20 | ) { |
| 21 | parent::__construct($propertyName, $declaredClass, $allowLargeStrings); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @template T of object |
| 26 | * @param ReflectionClass<T> $targetClass |
| 27 | * @param T $instance |
| 28 | */ |
| 29 | public function getReflectionMethod(ReflectionClass $targetClass, object $instance): ?ReflectionMethod |
| 30 | { |
| 31 | return (new GetMethodAttribute($this->methodName, $this->declaredClass, $this->allowLargeStrings)) |
| 32 | ->getReflectionMethod($targetClass, $instance); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @template T of object |
| 37 | * @param ReflectionClass<T> $targetClass |
| 38 | * @param T $instance |
| 39 | */ |
| 40 | public function getReflectionProperty( |
| 41 | ReflectionClass $targetClass, |
| 42 | object $instance |
| 43 | ): ?ReflectionProperty { |
| 44 | $method = $this->getReflectionMethod($targetClass, $instance); |
| 45 | if ($method) { |
| 46 | $method->invoke($instance); |
| 47 | } |
| 48 | return parent::getReflectionProperty($targetClass, $instance); |
| 49 | } |
| 50 | } |