BackedEnumPhpTypeSchemaResolver   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 60
ccs 21
cts 23
cp 0.913
rs 10
c 1
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOpenApiPhpTypeSchemaResolverManager() 0 4 1
A resolvePhpTypeSchema() 0 16 3
A supportsPhpType() 0 3 1
A getWeight() 0 3 1
A resolvePhpTypeSchemaName() 0 14 2
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router\OpenApi\PhpTypeSchemaResolver;
15
16
use BackedEnum;
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use ReflectionAttribute;
18
use ReflectionClass;
19
use ReflectionEnum;
0 ignored issues
show
Bug introduced by
The type ReflectionEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use ReflectionException;
21
use Reflector;
22
use Sunrise\Http\Router\OpenApi\Annotation\SchemaName;
23
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException;
24
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaNameResolverInterface;
25
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface;
26
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface;
27
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface;
28
use Sunrise\Http\Router\OpenApi\Type;
29
use Sunrise\Http\Router\OpenApi\TypeFactory;
30
31
use function is_subclass_of;
32
33
/**
34
 * @since 3.0.0
35
 */
36
final class BackedEnumPhpTypeSchemaResolver implements
37
    OpenApiPhpTypeSchemaResolverInterface,
38
    OpenApiPhpTypeSchemaNameResolverInterface,
39
    OpenApiPhpTypeSchemaResolverManagerAwareInterface
40
{
41
    private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager;
42
43 5
    public function setOpenApiPhpTypeSchemaResolverManager(
44
        OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager,
45
    ): void {
46 5
        $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager;
0 ignored issues
show
Bug introduced by
The property openApiPhpTypeSchemaResolverManager is declared read-only in Sunrise\Http\Router\Open...umPhpTypeSchemaResolver.
Loading history...
47
    }
48
49 3
    public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool
50
    {
51 3
        return is_subclass_of($phpType->name, BackedEnum::class);
52
    }
53
54
    /**
55
     * @inheritDoc
56
     *
57
     * @throws ReflectionException
58
     */
59 3
    public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array
60
    {
61 3
        $this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException();
62
63
        /** @var class-string<BackedEnum> $phpTypeName */
64 3
        $phpTypeName = $phpType->name;
65
66 3
        $enumPhpType = TypeFactory::fromPhpTypeReflection((new ReflectionEnum($phpTypeName))->getBackingType());
67 3
        $phpTypeSchema = $this->openApiPhpTypeSchemaResolverManager->resolvePhpTypeSchema($enumPhpType, $phpTypeHolder);
68
69 3
        $phpTypeSchema['enum'] = [];
70 3
        foreach ($phpTypeName::cases() as $case) {
71 3
            $phpTypeSchema['enum'][] = $case->value;
72
        }
73
74 3
        return $phpTypeSchema;
75
    }
76
77 3
    public function getWeight(): int
78
    {
79 3
        return 0;
80
    }
81
82 3
    public function resolvePhpTypeSchemaName(Type $phpType, Reflector $phpTypeHolder): string
83
    {
84
        /** @var class-string $className */
85 3
        $className = $phpType->name;
86 3
        $classReflection = new ReflectionClass($className);
87
88
        /** @var list<ReflectionAttribute<SchemaName>> $annotations */
89 3
        $annotations = $classReflection->getAttributes(SchemaName::class);
90 3
        if (isset($annotations[0])) {
91
            $annotation = $annotations[0]->newInstance();
92
            return $annotation->value;
93
        }
94
95 3
        return $classReflection->getShortName();
96
    }
97
}
98