Test Failed
Pull Request — master (#126)
by Anatoly
05:18
created

ArrayPhpTypeSchemaResolver::resolvePhpTypeSchema()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
nc 8
nop 2
dl 0
loc 29
ccs 17
cts 17
cp 1
crap 6
rs 9.1111
c 1
b 0
f 0
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 ReflectionAttribute;
17
use ReflectionParameter;
18
use ReflectionProperty;
19
use Reflector;
20
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException;
21
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface;
22
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface;
23
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface;
24
use Sunrise\Http\Router\OpenApi\Type;
25
use Sunrise\Hydrator\Annotation\Subtype;
26
27
/**
28
 * @since 3.0.0
29
 */
30
final class ArrayPhpTypeSchemaResolver implements
31
    OpenApiPhpTypeSchemaResolverInterface,
32
    OpenApiPhpTypeSchemaResolverManagerAwareInterface
33
{
34
    private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager;
35
36 5
    public function setOpenApiPhpTypeSchemaResolverManager(
37
        OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager,
38
    ): void {
39 5
        $this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager;
0 ignored issues
show
Bug introduced by
The property openApiPhpTypeSchemaResolverManager is declared read-only in Sunrise\Http\Router\Open...ayPhpTypeSchemaResolver.
Loading history...
40
    }
41
42 3
    public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool
43
    {
44 3
        return $phpType->name == Type::PHP_TYPE_NAME_ARRAY;
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 3
    public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array
51
    {
52 3
        $this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException();
53
54 3
        $phpTypeSchema = [
55 3
            'type' => Type::OAS_TYPE_NAME_ARRAY,
56 3
        ];
57
58
        if (
59 3
            $phpTypeHolder instanceof ReflectionParameter ||
60 3
            $phpTypeHolder instanceof ReflectionProperty
61
        ) {
62
            /** @var list<ReflectionAttribute<Subtype>> $annotations */
63 3
            $annotations = $phpTypeHolder->getAttributes(Subtype::class);
64 3
            if (isset($annotations[0])) {
65 3
                $annotation = $annotations[0]->newInstance();
66
67 3
                $arrayElementPhpType = new Type($annotation->name, $annotation->allowsNull);
68 3
                $arrayElementPhpTypeSchema = $this->openApiPhpTypeSchemaResolverManager
69 3
                    ->resolvePhpTypeSchema($arrayElementPhpType, $phpTypeHolder);
70
71 3
                $phpTypeSchema['items'] = $arrayElementPhpTypeSchema;
72 3
                if ($annotation->limit !== null) {
73 3
                    $phpTypeSchema['maxItems'] = $annotation->limit;
74
                }
75
            }
76
        }
77
78 3
        return $phpTypeSchema;
79
    }
80
81 3
    public function getWeight(): int
82
    {
83 3
        return 0;
84
    }
85
}
86