Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| PropertyAttribute | |
100.00% |
6 / 6 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReflectionProperty | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 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 PropertyAttribute |
| 11 | { |
| 12 | /** |
| 13 | * @param class-string<object>|null $declaredClass |
| 14 | */ |
| 15 | public function __construct( |
| 16 | public readonly string $propertyName, |
| 17 | public readonly ?string $declaredClass = null, |
| 18 | public readonly bool $allowLargeStrings = false |
| 19 | ) { |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @template T of object |
| 24 | * @param ReflectionClass<T> $targetClass |
| 25 | * @param T $instance |
| 26 | */ |
| 27 | public function getReflectionProperty(ReflectionClass $targetClass, object $instance): ?ReflectionProperty |
| 28 | { |
| 29 | $property = ($this->declaredClass ? new ReflectionClass($this->declaredClass) : $targetClass)->getProperty($this->propertyName); |
| 30 | try { |
| 31 | $property->isInitialized($instance); |
| 32 | return $property; |
| 33 | } catch (ReflectionException) { |
| 34 | return null; |
| 35 | } |
| 36 | } |
| 37 | } |