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