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