Passed
Pull Request — master (#1)
by
unknown
08:35
created

SchemaFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 3
b 0
f 0
nc 1
nop 1
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * Copyright (c) 2020.
4
 * @author Paweł Antosiak <[email protected]>
5
 */
6
7
declare(strict_types=1);
8
9
namespace Gorynych\Util;
10
11
use Cake\Collection\Collection;
12
use Cake\Collection\CollectionInterface;
13
use OpenApi\Annotations\Items;
14
use OpenApi\Annotations\Property;
15
use OpenApi\Annotations\Schema;
16
use const OpenApi\Annotations\UNDEFINED;
17
18
final class SchemaFactory
19
{
20
    private SchemaStorage $schemaStorage;
21
22 1
    public function __construct(SchemaStorage $schemaStorage)
23
    {
24 1
        $this->schemaStorage = $schemaStorage;
25 1
    }
26
27
    /**
28
     * Creates single JSON schema for provided class name
29
     */
30 1
    public function create(string $className): string
31
    {
32 1
        $schemaName = (new \ReflectionClass($className))->getShortName();
33
34 1
        $schema = $this->schemaStorage->getSchemas()->filter(
35 1
            static fn(Schema $schema): bool => $schema->schema === $schemaName
36 1
        )->first();
37
38 1
        $relatedSchemas = $this->getRelatedSchemas($schema)->toArray();
0 ignored issues
show
Bug introduced by
It seems like $schema can also be of type null; however, parameter $schema of Gorynych\Util\SchemaFactory::getRelatedSchemas() does only seem to accept OpenApi\Annotations\Schema, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
        $relatedSchemas = $this->getRelatedSchemas(/** @scrutinizer ignore-type */ $schema)->toArray();
Loading history...
39 1
        $schema = json_decode(json_encode($schema), true);
40 1
        $schema['components']['schemas'] = $relatedSchemas;
41
42 1
        return json_encode($schema);
43
    }
44
45
    /**
46
     * @return CollectionInterface<string, Schema>
47
     */
48 1
    private function getRelatedSchemas(Schema $schema): CollectionInterface
49
    {
50 1
        $schemas = $this->schemaStorage->getSchemas();
51 1
        $relatedSchemas = new Collection([]);
52
53 1
        (new Collection($schema->properties))
54 1
            ->reject(
55 1
                static function(Property $property): bool {
56
                    /** @phpstan-ignore-next-line */
57 1
                    return UNDEFINED === $property->ref && UNDEFINED === $property->items;
58 1
                }
59
            )
60 1
            ->each(
61 1
                static function(Property $property) use ($schemas, &$relatedSchemas): void {
62
                    /** @phpstan-ignore-next-line */
63 1
                    $ref = $property->items instanceof Items ? $property->items->ref : $property->ref;
0 ignored issues
show
introduced by
$property->items is always a sub-type of OpenApi\Annotations\Items.
Loading history...
64
65 1
                    $relatedSchemas = $relatedSchemas->append(
66 1
                        $schemas->filter(
67 1
                            static function(Schema $schema) use ($ref): bool {
68 1
                                $ref = explode('/', $ref);
69 1
                                return $schema->schema === end($ref);
70 1
                            }
71
                        )
72
                    );
73 1
                }
74
            );
75
76 1
        return $relatedSchemas->indexBy(
77 1
            static fn(Schema $schema): string => $schema->schema
78 1
        );
79
    }
80
}
81