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

RonasIT / laravel-swagger / 27601920182

16 Jun 2026 07:37AM UTC coverage: 99.448% (-0.2%) from 99.671%
27601920182

Pull #203

github

web-flow
Merge 7f7695402 into ead648e99
Pull Request #203: refactor: align SwaggerService/SwaggerSpecValidator with open api 3.0

21 of 23 new or added lines in 2 files covered. (91.3%)

1 existing line in 1 file now uncovered.

900 of 905 relevant lines covered (99.45%)

22.55 hits per line

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

99.4
/src/Services/SwaggerService.php
1
<?php
2

3
namespace RonasIT\AutoDoc\Services;
4

5
use Illuminate\Container\Container;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Testing\File;
8
use Illuminate\Support\Arr;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\ParallelTesting;
11
use Illuminate\Support\Facades\URL;
12
use Illuminate\Support\Str;
13
use ReflectionClass;
14
use RonasIT\AutoDoc\Contracts\SwaggerDriverContract;
15
use RonasIT\AutoDoc\Exceptions\DocFileNotExistsException;
16
use RonasIT\AutoDoc\Exceptions\EmptyContactEmailException;
17
use RonasIT\AutoDoc\Exceptions\EmptyDocFileException;
18
use RonasIT\AutoDoc\Exceptions\InvalidDriverClassException;
19
use RonasIT\AutoDoc\Exceptions\LegacyConfigException;
20
use RonasIT\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
21
use RonasIT\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
22
use RonasIT\AutoDoc\Exceptions\UnsupportedDocumentationViewerException;
23
use RonasIT\AutoDoc\Exceptions\WrongSecurityConfigException;
24
use RonasIT\AutoDoc\Traits\GetDependenciesTrait;
25
use RonasIT\AutoDoc\Validators\SwaggerSpecValidator;
26
use Symfony\Component\HttpFoundation\Response;
27
use Throwable;
28

29
/**
30
 * @property SwaggerDriverContract $driver
31
 */
32
class SwaggerService
33
{
34
    use GetDependenciesTrait;
35

36
    public const string OPEN_API_VERSION = '3.1.0';
37

38
    protected $driver;
39
    protected $openAPIValidator;
40

41
    protected $data;
42
    protected $config;
43
    protected $container;
44
    private $uri;
45
    private $method;
46
    /**
47
     * @var Request
48
     */
49
    private $request;
50
    private $item;
51
    private $security;
52

53
    protected array $ruleToTypeMap = [
54
        'array' => 'object',
55
        'boolean' => 'boolean',
56
        'date' => 'date',
57
        'digits' => 'integer',
58
        'integer' => 'integer',
59
        'numeric' => 'double',
60
        'string' => 'string',
61
        'int' => 'integer',
62
    ];
63

64
    protected $booleanAnnotations = [
65
        'deprecated',
66
    ];
67

68
    public function __construct(Container $container)
69
    {
70
        $this->openAPIValidator = app(SwaggerSpecValidator::class);
106✔
71

72
        $this->initConfig();
106✔
73

74
        $this->setDriver();
101✔
75

76
        if (config('app.env') === 'testing') {
99✔
77
            // client must enter at least `contact.email` to generate a default `info` block
78
            // otherwise an exception will be called
79
            $this->checkEmail();
99✔
80

81
            $this->container = $container;
98✔
82

83
            $this->security = $this->config['security'];
98✔
84

85
            $this->data = $this->driver->getProcessTmpData();
98✔
86

87
            if (empty($this->data)) {
98✔
88
                $this->data = $this->generateEmptyData();
66✔
89

90
                $this->driver->saveProcessTmpData($this->data);
66✔
91
            }
92
        }
93
    }
94

95
    protected function initConfig()
96
    {
97
        $this->config = config('auto-doc');
106✔
98

99
        $version = Arr::get($this->config, 'config_version');
106✔
100

101
        if (empty($version)) {
106✔
102
            throw new LegacyConfigException();
1✔
103
        }
104

105
        $packageConfigs = require __DIR__ . '/../../config/auto-doc.php';
105✔
106

107
        if (version_compare($packageConfigs['config_version'], $version, '>')) {
105✔
108
            throw new LegacyConfigException();
1✔
109
        }
110

111
        $documentationViewer = (string) Arr::get($this->config, 'documentation_viewer');
104✔
112

113
        if (!view()->exists("auto-doc::documentation-{$documentationViewer}")) {
104✔
114
            throw new UnsupportedDocumentationViewerException($documentationViewer);
2✔
115
        }
116

117
        $securityDriver = Arr::get($this->config, 'security');
102✔
118

119
        if ($securityDriver && !Arr::exists(Arr::get($this->config, 'security_drivers'), $securityDriver)) {
102✔
120
            throw new WrongSecurityConfigException();
1✔
121
        }
122
    }
123

124
    protected function setDriver()
125
    {
126
        $driver = $this->config['driver'];
101✔
127
        $className = Arr::get($this->config, "drivers.{$driver}.class");
101✔
128

129
        if (!class_exists($className)) {
101✔
130
            throw new SwaggerDriverClassNotFoundException($className);
1✔
131
        } else {
132
            $this->driver = app($className);
100✔
133
        }
134

135
        if (!$this->driver instanceof SwaggerDriverContract) {
100✔
136
            throw new InvalidDriverClassException($driver);
1✔
137
        }
138
    }
139

140
    protected function generateEmptyData(?string $view = null, array $viewData = [], array $license = []): array
141
    {
142
        if (empty($view) && !empty($this->config['info'])) {
67✔
143
            $view = $this->config['info']['description'];
65✔
144
        }
145

146
        $data = [
67✔
147
            'openapi' => self::OPEN_API_VERSION,
67✔
148
            'servers' => [
67✔
149
                ['url' => URL::query($this->config['basePath'])],
67✔
150
            ],
67✔
151
            'paths' => [],
67✔
152
            'components' => [
67✔
153
                'schemas' => $this->config['definitions'],
67✔
154
            ],
67✔
155
            'info' => $this->prepareInfo($view, $viewData, $license),
67✔
156
        ];
67✔
157

158
        $securitySchemes = $this->generateSecuritySchemes();
67✔
159

160
        if (!empty($securitySchemes)) {
67✔
161
            $data['components']['securitySchemes'] = $securitySchemes;
3✔
162
        }
163

164
        return $data;
67✔
165
    }
166

167
    protected function checkEmail(): void
168
    {
169
        if (!empty($this->config['info']) && !Arr::get($this->config, 'info.contact.email')) {
99✔
170
            throw new EmptyContactEmailException();
1✔
171
        }
172
    }
173

174
    protected function generateSecuritySchemes(): ?array
175
    {
176
        if (empty($this->security)) {
67✔
177
            return null;
64✔
178
        }
179

180
        return [
3✔
181
            $this->security => $this->config['security_drivers'][$this->security],
3✔
182
        ];
3✔
183
    }
184

185
    public function addData(Request $request, $response)
186
    {
187
        $this->request = $request;
28✔
188

189
        $this->prepareItem();
28✔
190

191
        $this->parseRequest();
28✔
192
        $this->parseResponse($response);
28✔
193

194
        $this->driver->saveProcessTmpData($this->data);
28✔
195
    }
196

197
    protected function prepareItem()
198
    {
199
        $this->uri = "/{$this->getUri()}";
28✔
200
        $this->method = strtolower($this->request->getMethod());
28✔
201

202
        if (empty(Arr::get($this->data, "paths.{$this->uri}.{$this->method}"))) {
28✔
203
            $this->data['paths'][$this->uri][$this->method] = [
27✔
204
                'tags' => [],
27✔
205
                'consumes' => [],
27✔
206
                'produces' => [],
27✔
207
                'parameters' => $this->getPathParams(),
27✔
208
                'responses' => [],
27✔
209
                'security' => [],
27✔
210
                'description' => '',
27✔
211
            ];
27✔
212
        }
213

214
        $this->item = &$this->data['paths'][$this->uri][$this->method];
28✔
215
    }
216

217
    protected function getUri()
218
    {
219
        $uri = $this->request->route()->uri();
28✔
220
        $basePath = preg_replace("/^\//", '', $this->config['basePath']);
28✔
221
        $preparedUri = preg_replace("/^{$basePath}/", '', $uri);
28✔
222

223
        return preg_replace("/^\//", '', $preparedUri);
28✔
224
    }
225

226
    protected function getPathParams(): array
227
    {
228
        $params = [];
27✔
229

230
        preg_match_all('/{.*?}/', $this->uri, $params);
27✔
231

232
        $params = Arr::collapse($params);
27✔
233

234
        $result = [];
27✔
235

236
        foreach ($params as $param) {
27✔
237
            $key = preg_replace('/[{}]/', '', $param);
6✔
238

239
            $result[] = [
6✔
240
                'in' => 'path',
6✔
241
                'name' => $key,
6✔
242
                'description' => $this->generatePathDescription($key),
6✔
243
                'required' => true,
6✔
244
                'schema' => [
6✔
245
                    'type' => 'string',
6✔
246
                ],
6✔
247
            ];
6✔
248
        }
249

250
        return $result;
27✔
251
    }
252

253
    protected function generatePathDescription(string $key): string
254
    {
255
        $expression = Arr::get($this->request->route()->wheres, $key);
6✔
256

257
        if (empty($expression)) {
6✔
258
            return '';
6✔
259
        }
260

261
        $exploded = explode('|', $expression);
1✔
262

263
        foreach ($exploded as $value) {
1✔
264
            if (!preg_match('/^[a-zA-Z0-9\.]+$/', $value)) {
1✔
265
                return "regexp: {$expression}";
1✔
266
            }
267
        }
268

269
        return 'in: ' . implode(',', $exploded);
1✔
270
    }
271

272
    protected function parseRequest()
273
    {
274
        $this->saveConsume();
28✔
275
        $this->saveTags();
28✔
276
        $this->saveSecurity();
28✔
277

278
        $concreteRequest = $this->getConcreteRequest();
28✔
279

280
        if (empty($concreteRequest)) {
28✔
281
            $this->item['description'] = '';
3✔
282

283
            return;
3✔
284
        }
285

286
        $annotations = $this->getClassAnnotations($concreteRequest);
25✔
287

288
        $this->markAsDeprecated($annotations);
25✔
289
        $this->saveParameters($concreteRequest, $annotations);
25✔
290
        $this->saveDescription($concreteRequest, $annotations);
25✔
291
    }
292

293
    protected function markAsDeprecated(array $annotations)
294
    {
295
        $this->item['deprecated'] = Arr::get($annotations, 'deprecated', false);
25✔
296
    }
297

298
    protected function saveResponseSchema(?array $content, string $definition): void
299
    {
300
        $schemaProperties = [];
28✔
301
        $schemaType = 'object';
28✔
302

303
        if (!empty($content) && array_is_list($content)) {
28✔
304
            $this->saveListResponseDefinitions($content, $schemaProperties);
17✔
305

306
            $schemaType = 'array';
17✔
307
        } else {
308
            $this->saveObjectResponseDefinitions($content, $schemaProperties, $definition);
11✔
309
        }
310

311
        $this->data['components']['schemas'][$definition] = [
28✔
312
            'type' => $schemaType,
28✔
313
            'properties' => $schemaProperties,
28✔
314
        ];
28✔
315
    }
316

317
    protected function saveListResponseDefinitions(array $content, array &$schemaProperties): void
318
    {
319
        $types = [];
17✔
320

321
        foreach ($content as $value) {
17✔
322
            $type = gettype($value);
17✔
323

324
            if (!in_array($type, $types)) {
17✔
325
                $types[] = $type;
17✔
326
                $schemaProperties['items']['allOf'][]['type'] = $type;
17✔
327
            }
328
        }
329
    }
330

331
    protected function saveObjectResponseDefinitions(array $content, array &$schemaProperties, string $definition): void
332
    {
333
        $properties = Arr::get($this->data, "components.schemas.{$definition}", []);
11✔
334

335
        foreach ($content as $name => $value) {
11✔
336
            $property = Arr::get($properties, "properties.{$name}", []);
10✔
337

338
            if (is_null($value)) {
10✔
339
                $property['nullable'] = true;
2✔
340
            } else {
341
                $property['type'] = gettype($value);
10✔
342
            }
343

344
            $schemaProperties[$name] = $property;
10✔
345
        }
346
    }
347

348
    protected function parseResponse($response)
349
    {
350
        $produceList = $this->data['paths'][$this->uri][$this->method]['produces'];
28✔
351

352
        $produce = $response->headers->get('Content-type');
28✔
353

354
        if (is_null($produce)) {
28✔
355
            $produce = 'text/plain';
1✔
356
        }
357

358
        if (!in_array($produce, $produceList)) {
28✔
359
            $this->item['produces'][] = $produce;
27✔
360
        }
361

362
        $responses = $this->item['responses'];
28✔
363

364
        $responseExampleLimitCount = config('auto-doc.response_example_limit_count');
28✔
365

366
        $content = json_decode($response->getContent(), true) ?? [];
28✔
367

368
        if (!empty($responseExampleLimitCount)) {
28✔
369
            if (!empty($content['data'])) {
28✔
370
                $limitedResponseData = array_slice($content['data'], 0, $responseExampleLimitCount, true);
2✔
371
                $content['data'] = $limitedResponseData;
2✔
372
                $content['to'] = count($limitedResponseData);
2✔
373
                $content['total'] = count($limitedResponseData);
2✔
374
            }
375
        }
376

377
        if (!empty($content['exception'])) {
28✔
378
            $uselessKeys = array_keys(Arr::except($content, ['message']));
1✔
379

380
            $content = Arr::except($content, $uselessKeys);
1✔
381
        }
382

383
        $code = $response->getStatusCode();
28✔
384

385
        if (!in_array($code, $responses)) {
28✔
386
            $this->saveExample(
28✔
387
                $code,
28✔
388
                json_encode($content, JSON_PRETTY_PRINT),
28✔
389
                $produce,
28✔
390
            );
28✔
391
        }
392

393
        $action = Str::ucfirst($this->getActionName($this->uri));
28✔
394
        $definition = "{$this->method}{$action}{$code}ResponseObject";
28✔
395

396
        $this->saveResponseSchema($content, $definition);
28✔
397

398
        if (is_array($this->item['responses'][$code])) {
28✔
399
            $this->item['responses'][$code]['content'][$produce]['schema']['$ref'] = "#/components/schemas/{$definition}";
27✔
400
        }
401
    }
402

403
    protected function saveExample($code, $content, $produce)
404
    {
405
        $description = $this->getResponseDescription($code);
28✔
406
        $availableContentTypes = [
28✔
407
            'application',
28✔
408
            'text',
28✔
409
            'image',
28✔
410
        ];
28✔
411
        $explodedContentType = explode('/', $produce);
28✔
412

413
        if (in_array($explodedContentType[0], $availableContentTypes)) {
28✔
414
            $this->item['responses'][$code] = $this->makeResponseExample($content, $produce, $description);
27✔
415
        } else {
416
            $this->item['responses'][$code] = '*Unavailable for preview*';
1✔
417
        }
418
    }
419

420
    protected function makeResponseExample($content, $mimeType, $description = ''): array
421
    {
422
        $example = match ($mimeType) {
27✔
423
            'application/json' => json_decode($content, true),
24✔
424
            'application/pdf' => base64_encode($content),
1✔
425
            default => $content,
2✔
426
        };
427

428
        return [
27✔
429
            'description' => $description,
27✔
430
            'content' => [
27✔
431
                $mimeType => [
27✔
432
                    'schema' => [
27✔
433
                        'type' => 'object',
27✔
434
                    ],
27✔
435
                    'example' => $example,
27✔
436
                ],
27✔
437
            ],
27✔
438
        ];
27✔
439
    }
440

441
    protected function saveParameters($request, array $annotations)
442
    {
443
        $formRequest = new $request();
25✔
444
        $formRequest->setUserResolver($this->request->getUserResolver());
25✔
445
        $formRequest->setRouteResolver($this->request->getRouteResolver());
25✔
446
        $rules = method_exists($formRequest, 'rules') ? $this->prepareRules($formRequest->rules()) : [];
25✔
447
        $attributes = method_exists($formRequest, 'attributes') ? $formRequest->attributes() : [];
25✔
448

449
        $actionName = $this->getActionName($this->uri);
25✔
450

451
        if (in_array($this->method, ['get', 'delete'])) {
25✔
452
            $this->saveGetRequestParameters($rules, $attributes, $annotations);
19✔
453
        } else {
454
            $this->savePostRequestParameters($actionName, $rules, $attributes, $annotations);
6✔
455
        }
456
    }
457

458
    protected function prepareRules(array $rules): array
459
    {
460
        $preparedRules = [];
24✔
461

462
        foreach ($rules as $field => $rulesField) {
24✔
463
            if (is_array($rulesField)) {
24✔
464
                $rulesField = array_map(function ($rule) {
21✔
465
                    return $this->getRuleAsString($rule);
21✔
466
                }, $rulesField);
21✔
467

468
                $preparedRules[$field] = implode('|', $rulesField);
21✔
469
            } else {
470
                $preparedRules[$field] = $this->getRuleAsString($rulesField);
24✔
471
            }
472
        }
473

474
        return $preparedRules;
24✔
475
    }
476

477
    protected function getRuleAsString($rule): string
478
    {
479
        if (is_object($rule)) {
24✔
480
            if (method_exists($rule, '__toString')) {
21✔
481
                return $rule->__toString();
21✔
482
            }
483

484
            $shortName = Str::afterLast(get_class($rule), '\\');
21✔
485

486
            $ruleName = preg_replace('/Rule$/', '', $shortName);
21✔
487

488
            return Str::snake($ruleName);
21✔
489
        }
490

491
        return $rule;
24✔
492
    }
493

494
    protected function saveGetRequestParameters($validation, array $attributes, array $annotations)
495
    {
496
        foreach ($validation as $parameter => $rules) {
19✔
497
            if (Arr::exists($validation, "{$parameter}.*")) {
18✔
498
                continue;
1✔
499
            }
500

501
            $rules = collect(explode('|', $rules));
18✔
502

503
            if ($this->isArrayItemParameter($parameter, $rules)) {
18✔
504
                $this->saveListParameters(Str::remove('.*', $parameter), $rules, $attributes, $annotations);
1✔
505
            } else {
506
                $this->saveQueryParameter($parameter, $rules, $attributes, $annotations);
18✔
507
            }
508
        }
509
    }
510

511
    protected function isArrayItemParameter(string $parameter, Collection $rules): bool
512
    {
513
        return Str::endsWith($parameter, '.*')
18✔
514
            && $rules->contains(fn ($rule) => Str::startsWith($rule, 'in:'));
18✔
515
    }
516

517
    protected function saveListParameters(string $parameter, Collection $rules, array $attributes, array $annotations): void
518
    {
519
        $inRule = $rules->first(fn ($rule) => Str::startsWith($rule, 'in:'));
1✔
520
        $availableValues = Str::after($inRule, 'in:');
1✔
521
        $availableValues = explode(',', $availableValues);
1✔
522

523
        $filteredRules = $rules->reject(fn ($rule) => $rule === 'required')->values();
1✔
524

525
        foreach ($availableValues as $value) {
1✔
526
            $this->saveQueryParameter("{$parameter}[]", $filteredRules, $attributes, $annotations, $value);
1✔
527
        }
528
    }
529

530
    protected function saveQueryParameter(string $parameter, Collection $rules, array $attributes, array $annotations, ?string $example = null): void
531
    {
532
        $existedParameter = Arr::first(
18✔
533
            $this->item['parameters'],
18✔
534
            fn ($existedParameter) => $existedParameter['name'] === $parameter && Arr::get($existedParameter, 'example') === $example,
18✔
535
        );
18✔
536

537
        if (!empty($existedParameter)) {
18✔
538
            return;
1✔
539
        }
540

541
        $parameterDefinition = [
17✔
542
            'in' => 'query',
17✔
543
            'name' => $parameter,
17✔
544
            'description' => $this->generateDescription($parameter, $rules->all(), $attributes, $annotations),
17✔
545
            'schema' => [
17✔
546
                'type' => $this->getParameterType($rules->all()),
17✔
547
            ],
17✔
548
        ];
17✔
549

550
        if ($rules->contains('required')) {
17✔
551
            $parameterDefinition['required'] = true;
17✔
552
        }
553

554
        if (!is_null($example)) {
17✔
555
            $parameterDefinition['example'] = $example;
1✔
556
        }
557

558
        $this->item['parameters'][] = $parameterDefinition;
17✔
559
    }
560

561
    protected function savePostRequestParameters($actionName, $rules, array $attributes, array $annotations)
562
    {
563
        if ($this->requestHasMoreProperties($actionName)) {
6✔
564
            if ($this->requestHasBody()) {
6✔
565
                $type = $this->request->header('Content-Type', 'application/json');
6✔
566

567
                $this->item['requestBody'] = [
6✔
568
                    'content' => [
6✔
569
                        $type => [
6✔
570
                            'schema' => [
6✔
571
                                '$ref' => "#/components/schemas/{$actionName}Object",
6✔
572
                            ],
6✔
573
                        ],
6✔
574
                    ],
6✔
575
                    'description' => '',
6✔
576
                    'required' => true,
6✔
577
                ];
6✔
578
            }
579

580
            $this->saveDefinitions($actionName, $rules, $attributes, $annotations);
6✔
581
        }
582
    }
583

584
    protected function saveDefinitions($objectName, $rules, $attributes, array $annotations)
585
    {
586
        $data = [
6✔
587
            'type' => 'object',
6✔
588
            'properties' => [],
6✔
589
        ];
6✔
590

591
        foreach ($rules as $parameter => $rule) {
6✔
592
            $rulesArray = (is_array($rule)) ? $rule : explode('|', $rule);
6✔
593
            $parameterType = $this->getParameterType($rulesArray);
6✔
594
            $this->saveParameterType($data, $parameter, $parameterType);
6✔
595

596
            $uselessRules = $this->ruleToTypeMap;
6✔
597
            $uselessRules['required'] = 'required';
6✔
598

599
            if (in_array('required', $rulesArray)) {
6✔
600
                $data['required'][] = $parameter;
6✔
601
            }
602

603
            $rulesArray = array_flip(array_diff_key(array_flip($rulesArray), $uselessRules));
6✔
604

605
            $data['properties'][$parameter]['description'] = $this->generateDescription($parameter, $rulesArray, $attributes, $annotations);
6✔
606
        }
607

608
        $data['example'] = $this->generateExample($data['properties']);
6✔
609
        $this->data['components']['schemas']["{$objectName}Object"] = $data;
6✔
610
    }
611

612
    protected function getParameterType(array $validation): string
613
    {
614
        $validationRules = $this->ruleToTypeMap;
23✔
615
        $validationRules['email'] = 'string';
23✔
616

617
        $parameterType = 'string';
23✔
618

619
        foreach ($validation as $item) {
23✔
620
            if (in_array($item, array_keys($validationRules))) {
23✔
621
                return $validationRules[$item];
22✔
622
            }
623
        }
624

625
        return $parameterType;
21✔
626
    }
627

628
    protected function saveParameterType(&$data, $parameter, $parameterType)
629
    {
630
        $data['properties'][$parameter] = [
6✔
631
            'type' => $parameterType,
6✔
632
        ];
6✔
633
    }
634

635
    protected function generateDescription(string $parameter, array $rules, array $attributes, array $annotations): string
636
    {
637
        $description = Arr::get($annotations, $parameter);
23✔
638

639
        if (empty($description)) {
23✔
640
            $description = Arr::get($attributes, $parameter, implode(', ', $rules));
23✔
641
        }
642

643
        return $description;
23✔
644
    }
645

646
    protected function requestHasMoreProperties($actionName): bool
647
    {
648
        $requestParametersCount = count($this->request->all());
6✔
649

650
        $properties = Arr::get($this->data, "components.schemas.{$actionName}Object.properties", []);
6✔
651
        $objectParametersCount = count($properties);
6✔
652

653
        return $requestParametersCount > $objectParametersCount;
6✔
654
    }
655

656
    protected function requestHasBody(): bool
657
    {
658
        $parameters = $this->data['paths'][$this->uri][$this->method]['parameters'];
6✔
659

660
        $bodyParamExisted = Arr::where($parameters, function ($value) {
6✔
661
            return $value['name'] === 'body';
1✔
662
        });
6✔
663

664
        return empty($bodyParamExisted);
6✔
665
    }
666

667
    public function getConcreteRequest()
668
    {
669
        $controller = $this->request->route()->getActionName();
28✔
670

671
        if ($controller === 'Closure') {
28✔
672
            return null;
1✔
673
        }
674

675
        $explodedController = explode('@', $controller);
27✔
676

677
        $class = $explodedController[0];
27✔
678
        $method = Arr::get($explodedController, 1, '__invoke');
27✔
679

680
        if (!method_exists($class, $method)) {
27✔
681
            return null;
1✔
682
        }
683

684
        $parameters = $this->resolveClassMethodDependencies(
26✔
685
            app($class),
26✔
686
            $method,
26✔
687
        );
26✔
688

689
        return Arr::first($parameters, function ($key) {
26✔
690
            return preg_match('/Request/', $key);
26✔
691
        });
26✔
692
    }
693

694
    public function saveConsume()
695
    {
696
        $consumeList = $this->data['paths'][$this->uri][$this->method]['consumes'];
28✔
697
        $consume = $this->request->header('Content-Type');
28✔
698

699
        if (!empty($consume) && !in_array($consume, $consumeList)) {
28✔
700
            $this->item['consumes'][] = $consume;
17✔
701
        }
702
    }
703

704
    public function saveTags()
705
    {
706
        $globalPrefix = config('auto-doc.global_prefix');
28✔
707
        $globalPrefix = Str::after($globalPrefix, '/');
28✔
708

709
        $explodedUri = explode('/', $this->uri);
28✔
710
        $explodedUri = array_filter($explodedUri);
28✔
711

712
        $tag = array_shift($explodedUri);
28✔
713

714
        if ($globalPrefix === $tag) {
28✔
715
            $tag = array_shift($explodedUri);
2✔
716
        }
717

718
        $this->item['tags'] = [$tag];
28✔
719
    }
720

721
    public function saveDescription($request, array $annotations)
722
    {
723
        $this->item['summary'] = $this->getSummary($request, $annotations);
25✔
724

725
        $description = Arr::get($annotations, 'description');
25✔
726

727
        if (!empty($description)) {
25✔
728
            $this->item['description'] = $description;
1✔
729
        }
730
    }
731

732
    protected function saveSecurity()
733
    {
734
        if ($this->requestSupportAuth()) {
28✔
735
            $this->addSecurityToOperation();
5✔
736
        }
737
    }
738

739
    protected function addSecurityToOperation()
740
    {
741
        $security = &$this->data['paths'][$this->uri][$this->method]['security'];
5✔
742

743
        if (empty($security)) {
5✔
744
            $security[] = [
5✔
745
                "{$this->security}" => [],
5✔
746
            ];
5✔
747
        }
748
    }
749

750
    protected function getSummary($request, array $annotations)
751
    {
752
        $summary = Arr::get($annotations, 'summary');
25✔
753

754
        if (empty($summary)) {
25✔
755
            $summary = $this->parseRequestName($request);
24✔
756
        }
757

758
        return $summary;
25✔
759
    }
760

761
    protected function requestSupportAuth(): bool
762
    {
763
        $security = Arr::get($this->config, 'security');
28✔
764
        $securityDriver = Arr::get($this->config, "security_drivers.{$security}");
28✔
765

766
        if (Arr::get($securityDriver, 'type') === 'apiKey') {
28✔
767
            $securityToken = match ($securityDriver['in']) {
2✔
NEW
768
                'header' => $this->request->header($securityDriver['name']),
×
769
                'query' => $this->request->query($securityDriver['name']),
1✔
770
                'cookie' => $this->request->cookie($securityDriver['name']),
1✔
NEW
771
                default => null,
×
772
            };
773

774
            return !empty($securityToken);
2✔
775
        }
776

777
        return $this->request->hasHeader('authorization');
26✔
778
    }
779

780
    protected function parseRequestName($request)
781
    {
782
        $explodedRequest = explode('\\', $request);
24✔
783
        $requestName = array_pop($explodedRequest);
24✔
784
        $summaryName = str_replace('Request', '', $requestName);
24✔
785

786
        $underscoreRequestName = $this->camelCaseToUnderScore($summaryName);
24✔
787

788
        return preg_replace('/[_]/', ' ', $underscoreRequestName);
24✔
789
    }
790

791
    protected function getResponseDescription($code)
792
    {
793
        $defaultDescription = Response::$statusTexts[$code];
28✔
794

795
        $request = $this->getConcreteRequest();
28✔
796

797
        if (empty($request)) {
28✔
798
            return $defaultDescription;
3✔
799
        }
800

801
        $annotations = $this->getClassAnnotations($request);
25✔
802

803
        $localDescription = Arr::get($annotations, "_{$code}");
25✔
804

805
        if (!empty($localDescription)) {
25✔
806
            return $localDescription;
1✔
807
        }
808

809
        return Arr::get($this->config, "defaults.code-descriptions.{$code}", $defaultDescription);
24✔
810
    }
811

812
    protected function getActionName($uri): string
813
    {
814
        $action = preg_replace('[\/]', '', $uri);
28✔
815

816
        return Str::camel($action);
28✔
817
    }
818

819
    public function saveProductionData()
820
    {
821
        if (ParallelTesting::token()) {
4✔
822
            $this->driver->appendProcessDataToTmpFile(function (?array $sharedTmpData) {
2✔
823
                $resultDocContent = (empty($sharedTmpData))
2✔
824
                    ? $this->generateEmptyData($this->config['info']['description'])
1✔
825
                    : $sharedTmpData;
1✔
826

827
                $this->mergeOpenAPIDocs($resultDocContent, $this->data);
2✔
828

829
                return $resultDocContent;
2✔
830
            });
2✔
831
        }
832

833
        $this->driver->saveData();
4✔
834
    }
835

836
    public function getDocFileContent()
837
    {
838
        try {
839
            $documentation = $this->driver->getDocumentation();
50✔
840

841
            $this->openAPIValidator->validate($documentation);
48✔
842
        } catch (Throwable $exception) {
42✔
843
            return $this->generateEmptyData($this->config['defaults']['error'], [
42✔
844
                'message' => $exception->getMessage(),
42✔
845
                'type' => $exception::class,
42✔
846
                'error_place' => $this->getErrorPlace($exception),
42✔
847
            ]);
42✔
848
        }
849

850
        $additionalDocs = config('auto-doc.additional_paths', []);
8✔
851

852
        foreach ($additionalDocs as $filePath) {
8✔
853
            try {
854
                $additionalDocContent = $this->getOpenAPIFileContent(base_path($filePath));
4✔
855
            } catch (DocFileNotExistsException|EmptyDocFileException|InvalidSwaggerSpecException $exception) {
3✔
856
                report($exception);
3✔
857

858
                continue;
3✔
859
            }
860

861
            $this->mergeOpenAPIDocs($documentation, $additionalDocContent);
1✔
862
        }
863

864
        return $documentation;
8✔
865
    }
866

867
    public function getPrettyDocFileContent(): array
868
    {
869
        $documentation = $this->getDocFileContent();
7✔
870

871
        foreach ($documentation['paths'] as $path => $pathItem) {
7✔
872
            foreach ($pathItem as $method => $operation) {
7✔
873
                if (Arr::has($operation, 'parameters')) {
7✔
874
                    $documentation['paths'][$path][$method]['parameters'] = collect($operation['parameters'])
7✔
875
                        ->groupBy('name')
7✔
876
                        ->map(function ($params, $name) {
7✔
877
                            if ($params->count() === 1 || !Str::endsWith($name, '[]')) {
7✔
878
                                return $params->first();
1✔
879
                            }
880

881
                            $base = $params->first();
7✔
882
                            $base['schema']['enum'] = $params
7✔
883
                                ->pluck('example')
7✔
884
                                ->filter(fn ($value) => !is_null($value))
7✔
885
                                ->values()
7✔
886
                                ->all();
7✔
887

888
                            return $base;
7✔
889
                        })
7✔
890
                        ->values()
7✔
891
                        ->all();
7✔
892
                }
893
            }
894
        }
895

896
        return $documentation;
7✔
897
    }
898

899
    protected function getErrorPlace(Throwable $exception): string
900
    {
901
        $firstTraceEntry = Arr::first($exception->getTrace());
42✔
902

903
        Arr::forget($firstTraceEntry, 'type');
42✔
904

905
        $formattedTraceEntry = Arr::map(
42✔
906
            array: $firstTraceEntry,
42✔
907
            callback: fn ($value, $key) => $key . '=' . (is_array($value) ? json_encode($value) : $value),
42✔
908
        );
42✔
909

910
        return implode(PHP_EOL, $formattedTraceEntry);
42✔
911
    }
912

913
    protected function camelCaseToUnderScore($input): string
914
    {
915
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
24✔
916
        $ret = $matches[0];
24✔
917

918
        foreach ($ret as &$match) {
24✔
919
            $match = ($match === strtoupper($match)) ? strtolower($match) : lcfirst($match);
24✔
920
        }
921

922
        return implode('_', $ret);
24✔
923
    }
924

925
    protected function generateExample($properties): array
926
    {
927
        $parameters = $this->replaceObjectValues($this->request->all());
6✔
928
        $example = [];
6✔
929

930
        $this->replaceNullValues($parameters, $properties, $example);
6✔
931

932
        return $example;
6✔
933
    }
934

935
    protected function replaceObjectValues($parameters): array
936
    {
937
        $classNamesValues = [
6✔
938
            File::class => '[uploaded_file]',
6✔
939
        ];
6✔
940

941
        $parameters = Arr::dot($parameters);
6✔
942
        $returnParameters = [];
6✔
943

944
        foreach ($parameters as $parameter => $value) {
6✔
945
            if (is_object($value)) {
6✔
946
                $class = get_class($value);
1✔
947

948
                $value = Arr::get($classNamesValues, $class, $class);
1✔
949
            }
950

951
            Arr::set($returnParameters, $parameter, $value);
6✔
952
        }
953

954
        return $returnParameters;
6✔
955
    }
956

957
    protected function getClassAnnotations($class): array
958
    {
959
        $reflection = new ReflectionClass($class);
25✔
960

961
        $annotations = $reflection->getDocComment();
25✔
962

963
        $annotations = Str::of($annotations)->remove("\r");
25✔
964

965
        $blocks = explode("\n", $annotations);
25✔
966

967
        $result = [];
25✔
968

969
        foreach ($blocks as $block) {
25✔
970
            if (Str::contains($block, '@')) {
25✔
971
                $index = strpos($block, '@');
1✔
972
                $block = substr($block, $index);
1✔
973
                $exploded = explode(' ', $block);
1✔
974

975
                $paramName = str_replace('@', '', array_shift($exploded));
1✔
976
                $paramValue = implode(' ', $exploded);
1✔
977

978
                if (in_array($paramName, $this->booleanAnnotations)) {
1✔
979
                    $paramValue = true;
1✔
980
                }
981

982
                $result[$paramName] = $paramValue;
1✔
983
            }
984
        }
985

986
        return $result;
25✔
987
    }
988

989
    /**
990
     * NOTE: All functions below are temporary solution for
991
     * this issue: https://github.com/OAI/OpenAPI-Specification/issues/229
992
     * We hope swagger developers will resolve this problem in next release of Swagger OpenAPI
993
     * */
994
    protected function replaceNullValues($parameters, $types, &$example)
995
    {
996
        foreach ($parameters as $parameter => $value) {
6✔
997
            if (is_null($value) && Arr::exists($types, $parameter)) {
6✔
998
                $example[$parameter] = $this->getDefaultValueByType($types[$parameter]['type']);
5✔
999
            } elseif (is_array($value)) {
6✔
1000
                $this->replaceNullValues($value, $types, $example[$parameter]);
3✔
1001
            } else {
1002
                $example[$parameter] = $value;
6✔
1003
            }
1004
        }
1005
    }
1006

1007
    protected function getDefaultValueByType($type)
1008
    {
1009
        $values = [
5✔
1010
            'object' => 'null',
5✔
1011
            'boolean' => false,
5✔
1012
            'date' => '0000-00-00',
5✔
1013
            'integer' => 0,
5✔
1014
            'string' => '',
5✔
1015
            'double' => 0,
5✔
1016
        ];
5✔
1017

1018
        return $values[$type];
5✔
1019
    }
1020

1021
    protected function prepareInfo(?string $view = null, array $viewData = [], array $license = []): array
1022
    {
1023
        $info = [];
67✔
1024

1025
        $license = array_filter($license);
67✔
1026

1027
        if (!empty($license)) {
67✔
UNCOV
1028
            $info['license'] = $license;
×
1029
        }
1030

1031
        if (!empty($view)) {
67✔
1032
            $info['description'] = view($view, $viewData)->render();
66✔
1033
        }
1034

1035
        return array_merge($this->config['info'], $info);
67✔
1036
    }
1037

1038
    protected function getOpenAPIFileContent(string $filePath): array
1039
    {
1040
        if (!file_exists($filePath)) {
4✔
1041
            throw new DocFileNotExistsException($filePath);
1✔
1042
        }
1043

1044
        $fileContent = json_decode(file_get_contents($filePath), true);
3✔
1045

1046
        if (empty($fileContent)) {
3✔
1047
            throw new EmptyDocFileException($filePath);
1✔
1048
        }
1049

1050
        $this->openAPIValidator->validate($fileContent);
2✔
1051

1052
        return $fileContent;
1✔
1053
    }
1054

1055
    protected function mergeOpenAPIDocs(array &$documentation, array $additionalDocumentation): void
1056
    {
1057
        $paths = array_keys($additionalDocumentation['paths']);
3✔
1058

1059
        foreach ($paths as $path) {
3✔
1060
            $additionalDocPath = $additionalDocumentation['paths'][$path];
3✔
1061

1062
            if (empty($documentation['paths'][$path])) {
3✔
1063
                $documentation['paths'][$path] = $additionalDocPath;
3✔
1064
            } else {
1065
                $methods = array_keys($documentation['paths'][$path]);
1✔
1066
                $additionalDocMethods = array_keys($additionalDocPath);
1✔
1067

1068
                foreach ($additionalDocMethods as $method) {
1✔
1069
                    if (!in_array($method, $methods)) {
1✔
1070
                        $documentation['paths'][$path][$method] = $additionalDocPath[$method];
1✔
1071
                    }
1072
                }
1073
            }
1074
        }
1075

1076
        foreach (Arr::get($additionalDocumentation, 'components.schemas', []) as $definitionName => $definitionData) {
3✔
1077
            if (empty($documentation['components']['schemas'][$definitionName])) {
3✔
1078
                $documentation['components']['schemas'][$definitionName] = $definitionData;
3✔
1079
            }
1080
        }
1081
    }
1082
}
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