• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DoclerLabs / api-client-generator / 13286193319

12 Feb 2025 01:12PM UTC coverage: 86.862% (-0.03%) from 86.888%
13286193319

push

github

web-flow
Merge pull request #121 from gusarov112/feature/add-getVersion-to-specification

Added `Specification::getVersion()` method

0 of 1 new or added line in 1 file covered. (0.0%)

2929 of 3372 relevant lines covered (86.86%)

5.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.25
/src/Input/Specification.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace DoclerLabs\ApiClientGenerator\Input;
6

7
use cebe\openapi\spec\OpenApi;
8
use cebe\openapi\spec\Reference;
9
use cebe\openapi\spec\SecurityScheme;
10
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
11
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MaxLengthConstraint;
12
use DoclerLabs\ApiClientGenerator\Entity\Constraint\MinLengthConstraint;
13
use DoclerLabs\ApiClientGenerator\Entity\Field;
14
use DoclerLabs\ApiClientGenerator\Entity\FieldCollection;
15
use DoclerLabs\ApiClientGenerator\Entity\Operation;
16
use DoclerLabs\ApiClientGenerator\Entity\OperationCollection;
17
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\FormUrlencodedContentTypeSerializer;
18
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\JsonContentTypeSerializer;
19
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\VdnApiJsonContentTypeSerializer;
20
use DoclerLabs\ApiClientGenerator\Output\Copy\Serializer\ContentType\XmlContentTypeSerializer;
21

22
class Specification
23
{
24
    public function __construct(
25
        private OpenApi $openApi,
26
        private OperationCollection $operations,
27
        private FieldCollection $compositeRequestFields,
28
        private FieldCollection $compositeResponseFields
29
    ) {
30
    }
2✔
31

32
    public function getTitle(): string
33
    {
34
        return $this->openApi->info->title;
1✔
35
    }
36

37
    public function getVersion(): string
38
    {
NEW
39
        return $this->openApi->info->version;
×
40
    }
41

42
    public function hasLicense(): bool
43
    {
44
        $license = $this->openApi->info->license;
1✔
45

46
        return $license && $license->name;
1✔
47
    }
48

49
    public function getLicenseName(): string
50
    {
51
        return (string)$this->openApi->info->license?->name;
1✔
52
    }
53

54
    public function getServerUrls(): array
55
    {
56
        $serverUrls = [];
×
57
        foreach ($this->openApi->servers as $server) {
×
58
            $serverUrls[] = $server->url;
×
59
        }
60

61
        return $serverUrls;
×
62
    }
63

64
    public function getOperations(): OperationCollection
65
    {
66
        return $this->operations;
1✔
67
    }
68

69
    public function getCompositeFields(): FieldCollection
70
    {
71
        return $this->getCompositeRequestFields()->merge($this->getCompositeResponseFields());
2✔
72
    }
73

74
    public function getCompositeRequestFields(): FieldCollection
75
    {
76
        return $this->compositeRequestFields;
2✔
77
    }
78

79
    public function getCompositeResponseFields(): FieldCollection
80
    {
81
        return $this->compositeResponseFields;
2✔
82
    }
83

84
    /**
85
     * @return SecurityScheme[]
86
     */
87
    public function getSecuritySchemes(): array
88
    {
89
        return array_map(static function ($securityScheme): SecurityScheme {
1✔
90
            if ($securityScheme instanceof Reference) {
1✔
91
                $securityScheme = $securityScheme->resolve();
×
92
            }
93

94
            /** @var SecurityScheme $securityScheme */
95

96
            return $securityScheme;
1✔
97
        }, $this->openApi->components->securitySchemes ?? []);
1✔
98
    }
99

100
    public function getAllContentTypes(): array
101
    {
102
        $allContentTypes = [
1✔
103
            XmlContentTypeSerializer::MIME_TYPE            => false,
1✔
104
            FormUrlencodedContentTypeSerializer::MIME_TYPE => false,
1✔
105
            JsonContentTypeSerializer::MIME_TYPE           => false,
1✔
106
            VdnApiJsonContentTypeSerializer::MIME_TYPE     => false,
1✔
107
        ];
108

109
        /** @var Operation $operation */
110
        foreach ($this->getOperations() as $operation) {
1✔
111
            foreach ($operation->request->bodyContentTypes as $contentType) {
1✔
112
                $allContentTypes[$contentType] = true;
1✔
113
            }
114
            foreach ($operation->successfulResponses as $response) {
1✔
115
                foreach ($response->bodyContentTypes as $contentType) {
1✔
116
                    $allContentTypes[$contentType] = true;
1✔
117
                }
118
            }
119
        }
120

121
        return array_keys(array_filter($allContentTypes));
1✔
122
    }
123

124
    public function requiresIntlExtension(): bool
125
    {
126
        foreach ($this->getOperations() as $operation) {
1✔
127
            foreach ($operation->request->fields as $field) {
1✔
128
                if ($this->fieldRequiresIntlExtension($field)) {
1✔
129
                    return true;
×
130
                }
131
            }
132
        }
133

134
        return false;
1✔
135
    }
136

137
    public function isSecuritySchemeEnabled(string $scheme): bool
138
    {
139
        if (empty($this->getSecuritySchemes())) {
1✔
140
            return false;
×
141
        }
142

143
        foreach ($this->getSecuritySchemes() as $securityScheme) {
1✔
144
            if ($securityScheme->scheme === $scheme) {
1✔
145
                return true;
1✔
146
            }
147
        }
148

149
        return false;
×
150
    }
151

152
    private function fieldRequiresIntlExtension(Field $field): bool
153
    {
154
        /** @var ConstraintInterface $constraint */
155
        foreach ($field->getConstraints() as $constraint) {
1✔
156
            if (
157
                (
158
                    $constraint instanceof MinLengthConstraint
1✔
159
                    || $constraint instanceof MaxLengthConstraint
1✔
160
                )
161
                && $constraint->exists()
1✔
162
            ) {
163
                return true;
×
164
            }
165
        }
166

167
        if (
168
            $field->isObject()
1✔
169
            && !empty($field->getObjectProperties())
1✔
170
        ) {
171
            foreach ($field->getObjectProperties() as $objectProperty) {
1✔
172
                if ($this->fieldRequiresIntlExtension($objectProperty)) {
1✔
173
                    return true;
×
174
                }
175
            }
176
        }
177

178
        return false;
1✔
179
    }
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc