Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.67% |
11 / 12 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
| BuiltInPhpClassStrategy | |
91.67% |
11 / 12 |
|
85.71% |
6 / 7 |
9.05 | |
0.00% |
0 / 1 |
| supports | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| getClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getMetadata | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getCreationMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModificationMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getResultMetadata | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\Core\Metadata\Strategy; |
| 3 | |
| 4 | use Apie\Core\Context\ApieContext; |
| 5 | use Apie\Core\Context\MetadataFieldHashmap; |
| 6 | use Apie\Core\Enums\ScalarType; |
| 7 | use Apie\Core\Metadata\CompositeMetadata; |
| 8 | use Apie\Core\Metadata\ScalarMetadata; |
| 9 | use Apie\Core\Metadata\StrategyInterface; |
| 10 | use DateTime; |
| 11 | use DateTimeImmutable; |
| 12 | use DateTimeInterface; |
| 13 | use ReflectionClass; |
| 14 | use ReflectionEnum; |
| 15 | use ReflectionFiber; |
| 16 | use ReflectionIntersectionType; |
| 17 | use ReflectionMethod; |
| 18 | use ReflectionNamedType; |
| 19 | use ReflectionParameter; |
| 20 | use ReflectionProperty; |
| 21 | use ReflectionType; |
| 22 | use ReflectionUnionType; |
| 23 | use Stringable; |
| 24 | |
| 25 | class BuiltInPhpClassStrategy implements StrategyInterface |
| 26 | { |
| 27 | private const COMPOSITE_NAME_CLASSES = [ |
| 28 | ReflectionType::class, |
| 29 | ReflectionNamedType::class, |
| 30 | ReflectionUnionType::class, |
| 31 | ReflectionIntersectionType::class, |
| 32 | ReflectionClass::class, |
| 33 | ReflectionMethod::class, |
| 34 | ReflectionProperty::class, |
| 35 | ReflectionParameter::class, |
| 36 | ReflectionEnum::class, |
| 37 | ReflectionFiber::class, |
| 38 | ]; |
| 39 | |
| 40 | private const STRING_NAME_CLASSES = [ |
| 41 | Stringable::class, |
| 42 | DateTimeInterface::class, |
| 43 | DateTime::class, |
| 44 | DateTimeImmutable::class, |
| 45 | ]; |
| 46 | |
| 47 | public static function supports(ReflectionClass $class): bool |
| 48 | { |
| 49 | return in_array($class->name, self::COMPOSITE_NAME_CLASSES) || in_array($class->name, self::STRING_NAME_CLASSES); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return ReflectionClass<object> |
| 54 | */ |
| 55 | public function getClass(): ReflectionClass |
| 56 | { |
| 57 | return $this->class; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param ReflectionClass<object> $class |
| 62 | */ |
| 63 | public function __construct(private ReflectionClass $class) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | private function getMetadata(): CompositeMetadata|ScalarMetadata |
| 68 | { |
| 69 | if (in_array($this->class->name, self::STRING_NAME_CLASSES)) { |
| 70 | return new ScalarMetadata(ScalarType::STRING); |
| 71 | } |
| 72 | return new CompositeMetadata( |
| 73 | new MetadataFieldHashmap([]), |
| 74 | $this->class |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public function getCreationMetadata(ApieContext $context): CompositeMetadata|ScalarMetadata |
| 79 | { |
| 80 | return $this->getMetadata(); |
| 81 | } |
| 82 | public function getModificationMetadata(ApieContext $context): CompositeMetadata|ScalarMetadata |
| 83 | { |
| 84 | return $this->getMetadata(); |
| 85 | } |
| 86 | public function getResultMetadata(ApieContext $context): CompositeMetadata|ScalarMetadata |
| 87 | { |
| 88 | return $this->getMetadata(); |
| 89 | } |
| 90 | } |