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

onmoon / openapi-server-bundle / 15881876638

25 Jun 2025 04:28PM UTC coverage: 80.585% (-0.5%) from 81.095%
15881876638

Pull #196

github

web-flow
Merge 90ce8070d into c35fba5f2
Pull Request #196: Fix minimum versions install

9 of 29 new or added lines in 5 files covered. (31.03%)

44 existing lines in 12 files now uncovered.

1378 of 1710 relevant lines covered (80.58%)

3.8 hits per line

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

95.0
/src/CodeGenerator/NameGenerator.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace OnMoon\OpenApiServerBundle\CodeGenerator;
6

7
use Lukasoppermann\Httpstatus\Httpstatus;
8
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\DtoDefinition;
9
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\GraphDefinition;
10
use OnMoon\OpenApiServerBundle\CodeGenerator\Definitions\ResponseDefinition;
11
use OnMoon\OpenApiServerBundle\CodeGenerator\Naming\NamingStrategy;
12
use Throwable;
13

14
use function ucfirst;
15

16
/** @psalm-suppress ClassMustBeFinal */
17
class NameGenerator
18
{
19
    private const DTO_NAMESPACE       = 'Dto';
20
    private const REQUEST_SUFFIX      = 'Request';
21
    private const RESPONSE_SUFFIX     = 'Response';
22
    public const DTO_SUFFIX           = 'Dto';
23
    public const APIS_NAMESPACE       = 'Apis';
24
    public const COMPONENTS_NAMESPACE = 'Components';
25

26
    private NamingStrategy $naming;
27
    private Httpstatus $httpstatus;
28
    private string $rootNamespace;
29
    private string $rootPath;
30

31
    public function __construct(NamingStrategy $naming, Httpstatus $httpstatus, string $rootNamespace, string $rootPath)
32
    {
33
        $this->naming        = $naming;
10✔
34
        $this->httpstatus    = $httpstatus;
10✔
35
        $this->rootNamespace = $rootNamespace;
10✔
36
        $this->rootPath      = $rootPath;
10✔
37
    }
38

39
    public function setAllNamesAndPaths(GraphDefinition $graph): void
40
    {
41
        $graph->getServiceSubscriber()
3✔
42
            ->setFileName('ApiServiceLoaderServiceSubscriber.php')
3✔
43
            ->setFilePath($this->naming->buildPath($this->rootPath, 'ServiceSubscriber'))
3✔
44
            ->setClassName('ApiServiceLoaderServiceSubscriber')
3✔
45
            ->setNamespace($this->naming->buildNamespace($this->rootNamespace, 'ServiceSubscriber'));
3✔
46

47
        foreach ($graph->getSpecifications() as $specificationDefinition) {
3✔
48
            $specification       = $specificationDefinition->getSpecification();
2✔
49
            $apiName             = $this->naming->stringToNamespace($specification->getNameSpace());
2✔
50
            $apiNamespace        = $this->naming->buildNamespace($this->rootNamespace, self::APIS_NAMESPACE, $apiName);
2✔
51
            $apiPath             = $this->naming->buildPath($this->rootPath, self::APIS_NAMESPACE, $apiName);
2✔
52
            $componentsNamespace = $this->naming->buildNamespace($this->rootNamespace, self::COMPONENTS_NAMESPACE, $apiName);
2✔
53
            $componentsPath      = $this->naming->buildPath($this->rootPath, self::COMPONENTS_NAMESPACE, $apiName);
2✔
54

55
            foreach ($specificationDefinition->getComponents() as $component) {
2✔
UNCOV
56
                $componentName      = $this->naming->stringToNamespace($component->getName());
×
57
                $componentNamespace = $this->naming->buildNamespace($componentsNamespace, $componentName);
×
58
                $componentPath      = $this->naming->buildPath($componentsPath, $componentName);
×
59

UNCOV
60
                $this->setTreeNames($component->getDto(), $componentNamespace, $componentName, $componentPath);
×
61
            }
62

63
            foreach ($specificationDefinition->getOperations() as $operation) {
2✔
64
                $operationName      = $this->naming->stringToNamespace($operation->getOperationId());
1✔
65
                $operationNamespace = $this->naming->buildNamespace($apiNamespace, $operationName);
1✔
66
                $operationPath      = $this->naming->buildPath($apiPath, $operationName);
1✔
67

68
                $methodName = $this->naming->stringToMethodName($operation->getOperationId());
1✔
69
                $operation->getRequestHandlerInterface()
1✔
70
                    ->setMethodName($methodName)
1✔
71
                    ->setMethodDescription($operation->getSummary())
1✔
72
                    ->setFileName($this->getFileName($operationName))
1✔
73
                    ->setFilePath($operationPath)
1✔
74
                    ->setNamespace($operationNamespace)
1✔
75
                    ->setClassName($operationName);
1✔
76

77
                $request = $operation->getRequest();
1✔
78
                if ($request instanceof DtoDefinition) {
1✔
79
                    $this->setRequestNames($request, $operationNamespace, $operationName, $operationPath);
1✔
80
                }
81

82
                $responseNamespace = $this->naming->buildNamespace(
1✔
83
                    $operationNamespace,
1✔
84
                    self::DTO_NAMESPACE,
1✔
85
                    self::RESPONSE_SUFFIX
1✔
86
                );
1✔
87

88
                $responsePath = $this->naming->buildPath(
1✔
89
                    $operationPath,
1✔
90
                    self::DTO_NAMESPACE,
1✔
91
                    self::RESPONSE_SUFFIX
1✔
92
                );
1✔
93

94
                foreach ($operation->getResponses() as $response) {
1✔
95
                    $this->setResponseNames($response, $responseNamespace, $operationName, $responsePath);
1✔
96
                }
97
            }
98
        }
99
    }
100

101
    public function setRequestNames(DtoDefinition $request, string $operationNamespace, string $operationName, string $operationPath): void
102
    {
103
        $requestDtoNamespace = $this->naming->buildNamespace(
2✔
104
            $operationNamespace,
2✔
105
            self::DTO_NAMESPACE,
2✔
106
            self::REQUEST_SUFFIX
2✔
107
        );
2✔
108
        $requestDtoClassName = $this->naming->stringToNamespace(
2✔
109
            $operationName . self::REQUEST_SUFFIX . self::DTO_SUFFIX
2✔
110
        );
2✔
111
        $requestDtoPath      = $this->naming->buildPath(
2✔
112
            $operationPath,
2✔
113
            self::DTO_NAMESPACE,
2✔
114
            self::REQUEST_SUFFIX
2✔
115
        );
2✔
116

117
        $this->setTreeNames($request, $requestDtoNamespace, $requestDtoClassName, $requestDtoPath);
2✔
118
    }
119

120
    public function setResponseNames(ResponseDefinition $response, string $responseNamespace, string $operationName, string $responsePath): void
121
    {
122
        $responseBody = $response->getResponseBody();
3✔
123
        if (! $responseBody instanceof DtoDefinition) {
3✔
UNCOV
124
            return;
×
125
        }
126

127
        try {
128
            $statusNamespace = $this->httpstatus->getReasonPhrase((int) $response->getStatusCode());
3✔
129
        } catch (Throwable $e) {
1✔
130
            $statusNamespace = $response->getStatusCode();
1✔
131
        }
132

133
        $statusNamespace = $this->naming->stringToNamespace($statusNamespace);
3✔
134

135
        $responseDtoNamespace = $this->naming->buildNamespace($responseNamespace, $statusNamespace);
3✔
136
        $responseDtoClassName = $this->naming->stringToNamespace(
3✔
137
            $operationName . $statusNamespace . self::DTO_SUFFIX
3✔
138
        );
3✔
139
        $responseDtoPath      = $this->naming->buildPath($responsePath, $statusNamespace);
3✔
140

141
        $this->setTreeNames($responseBody, $responseDtoNamespace, $responseDtoClassName, $responseDtoPath);
3✔
142
    }
143

144
    public function setTreeNames(DtoDefinition $root, string $namespace, string $className, string $path): void
145
    {
146
        $root->setClassName($className);
5✔
147
        $root->setFileName($this->getFileName($className));
5✔
148
        $root->setFilePath($path);
5✔
149
        $root->setNamespace($namespace);
5✔
150
        $this->setPropertyClassNames($root);
5✔
151
        $this->setGettersSetters($root);
5✔
152

153
        foreach ($root->getProperties() as $property) {
5✔
154
            $objectDefinition = $property->getObjectTypeDefinition();
2✔
155
            if (! $objectDefinition instanceof DtoDefinition) {
2✔
156
                continue;
2✔
157
            }
158

159
            $part         = $this->naming->stringToNamespace($property->getClassPropertyName());
2✔
160
            $subClassName = $this->naming->stringToNamespace($part . self::DTO_SUFFIX);
2✔
161
            $subNamespace = $this->naming->buildNamespace($namespace, $part);
2✔
162
            $subPath      = $this->naming->buildPath($path, $part);
2✔
163
            $this->setTreeNames($objectDefinition, $subNamespace, $subClassName, $subPath);
2✔
164
        }
165
    }
166

167
    public function getFileName(string $className): string
168
    {
169
        return $className . '.php';
6✔
170
    }
171

172
    public function setGettersSetters(DtoDefinition $root): void
173
    {
174
        foreach ($root->getProperties() as $property) {
6✔
175
            $baseName = ucfirst($this->naming->stringToMethodName($property->getClassPropertyName()));
3✔
176
            $property->setGetterName('get' . $baseName);
3✔
177
            $property->setSetterName('set' . $baseName);
3✔
178
        }
179
    }
180

181
    public function setPropertyClassNames(DtoDefinition $root): void
182
    {
183
        foreach ($root->getProperties() as $property) {
6✔
184
            $propertyName = $property->getSpecPropertyName();
3✔
185

186
            if (! $this->naming->isAllowedPhpPropertyName($propertyName)) {
3✔
187
                $propertyName = $this->naming->stringToMethodName($propertyName);
1✔
188
            }
189

190
            $property->setClassPropertyName($propertyName);
3✔
191
        }
192
    }
193
}
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

© 2026 Coveralls, Inc