Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
6 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
OneToManyAttribute
75.00% covered (warning)
75.00%
6 / 8
50.00% covered (danger)
50.00%
1 / 2
5.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReflectionProperty
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
4.37
1<?php
2namespace Apie\StorageMetadata\Attributes;
3
4use Apie\StorageMetadata\Interfaces\StorageDtoInterface;
5use Attribute;
6use ReflectionClass;
7use ReflectionException;
8use ReflectionProperty;
9
10#[Attribute(Attribute::TARGET_PROPERTY)]
11class 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}