Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
66.67% |
4 / 6 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| OneToOneAttribute | |
66.67% |
4 / 6 |
|
50.00% |
1 / 2 |
4.59 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReflectionProperty | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| 1 | <?php |
| 2 | namespace Apie\StorageMetadata\Attributes; |
| 3 | |
| 4 | use Attribute; |
| 5 | use ReflectionClass; |
| 6 | use ReflectionException; |
| 7 | use ReflectionProperty; |
| 8 | |
| 9 | #[Attribute(Attribute::TARGET_PROPERTY)] |
| 10 | class OneToOneAttribute |
| 11 | { |
| 12 | /** |
| 13 | * @param class-string<object> $declaredClass |
| 14 | */ |
| 15 | public function __construct( |
| 16 | public readonly string $propertyName, |
| 17 | public readonly ?string $declaredClass = null |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @template T of object |
| 23 | * @param ReflectionClass<T> $targetClass |
| 24 | * @param T $instance |
| 25 | */ |
| 26 | public function getReflectionProperty(ReflectionClass $targetClass, object $instance): ?ReflectionProperty |
| 27 | { |
| 28 | $property = ($this->declaredClass ? new ReflectionClass($this->declaredClass) : $targetClass)->getProperty($this->propertyName); |
| 29 | try { |
| 30 | $property->isInitialized($instance); |
| 31 | return $property; |
| 32 | } catch (ReflectionException) { |
| 33 | return null; |
| 34 | } |
| 35 | } |
| 36 | } |