-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiSpecResolver.php
More file actions
37 lines (30 loc) · 1.01 KB
/
OpenApiSpecResolver.php
File metadata and controls
37 lines (30 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
declare(strict_types=1);
namespace Studio\OpenApiContractTesting;
use ReflectionClass;
use ReflectionMethod;
trait OpenApiSpecResolver
{
protected function openApiSpecFallback(): string
{
return '';
}
private function resolveOpenApiSpec(): string
{
// 1. Method-level #[OpenApiSpec] attribute
$methodName = $this->name(); // @phpstan-ignore method.notFound
$refMethod = new ReflectionMethod($this, $methodName);
$methodAttrs = $refMethod->getAttributes(OpenApiSpec::class);
if ($methodAttrs !== []) {
return $methodAttrs[0]->newInstance()->name;
}
// 2. Class-level #[OpenApiSpec] attribute
$refClass = new ReflectionClass($this);
$classAttrs = $refClass->getAttributes(OpenApiSpec::class);
if ($classAttrs !== []) {
return $classAttrs[0]->newInstance()->name;
}
// 3. Subclass hook (e.g. openApiSpec() in Laravel trait)
return $this->openApiSpecFallback();
}
}