Debian Codegraph @

vendor/zircote/swagger-php/src/Analysers/AttributeAnnotationFactory.php

raw
1 <?php declare(strict_types=1);
2
3 /**
4 * @license Apache 2.0
5 */
6
7 namespace OpenApi\Analysers;
8
9 use OpenApi\Annotations as OA;
10 use OpenApi\Attributes as OAT;
11 use OpenApi\Context;
12 use OpenApi\Generator;
13 use OpenApi\GeneratorAwareTrait;
14
15 class AttributeAnnotationFactory implements AnnotationFactoryInterface
16 {
17 use GeneratorAwareTrait;
18
19 protected bool $ignoreOtherAttributes = false;
20
21 public function __construct(bool $ignoreOtherAttributes = false)
22 {
23 $this->ignoreOtherAttributes = $ignoreOtherAttributes;
24 }
25
26 public function isSupported(): bool
27 {
28 return true;
29 }
30
31 public function build(\Reflector $reflector, Context $context): array
32 {
33 if (!$this->isSupported()) {
34 return [];
35 }
36
37 if ($reflector instanceof \ReflectionProperty && $reflector->isPromoted()) {
38 // handled via __construct() parameter
39 return [];
40 }
41
42 // no proper way to inject
43 Generator::$context = $context;
44
45 /** @var list<OA\AbstractAnnotation> $annotations */
46 $annotations = [];
47 try {
48 $attributeName = $this->ignoreOtherAttributes
49 ? [OA\AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF]
50 : [];
51
52 foreach ($reflector->getAttributes(...$attributeName) as $attribute) {
53 if (class_exists($attribute->getName())) {
54 $instance = $attribute->newInstance();
55 if ($instance instanceof OA\AbstractAnnotation) {
56 $annotations[] = $instance;
57 } else {
58 if (false === $context->is('other')) {
59 $context->other = [];
60 }
61 $context->other[] = $instance;
62 }
63 } else {
64 $context->logger->debug(sprintf('Could not instantiate attribute "%s"; class not found.', $attribute->getName()));
65 }
66 }
67
68 if ($reflector instanceof \ReflectionMethod) {
69 // also look at parameter attributes
70 foreach ($reflector->getParameters() as $rp) {
71 foreach ([OA\Property::class, OAT\Parameter::class, OA\RequestBody::class] as $attributeName) {
72 foreach ($rp->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
73 /** @var OA\Property|OAT\Parameter|OA\RequestBody $instance */
74 $instance = $attribute->newInstance();
75 $instance->_context = new Context([
76 'nested' => null,
77 'property' => $rp->getName(),
78 'reflector' => $rp,
79 ], $context);
80
81 if ($instance instanceof OA\Property) {
82 if ($rp->isPromoted()) {
83 // ensure each property has its own context
84 $instance->_context = new Context([
85 'generated' => true,
86 'annotations' => [$instance],
87 'property' => $rp->getName(),
88 'reflector' => $rp,
89 ], $context);
90
91 // promoted parameter - docblock is available via class/property
92 if ($comment = $rp->getDeclaringClass()->getProperty($rp->getName())->getDocComment()) {
93 $instance->_context->comment = $comment;
94 }
95 } else {
96 $instance->_context->property = $rp->getName();
97 }
98 } elseif ($instance instanceof OAT\Parameter) {
99 if (method_exists($rp, 'getDocComment')) {
100 if ($comment = $rp->getDocComment()) {
101 $instance->_context->comment = $comment;
102 }
103 }
104 }
105
106 $annotations[] = $instance;
107 }
108 }
109 }
110 }
111 } finally {
112 Generator::$context = null;
113 }
114
115 // merge backwards into parents...
116 $isParent = static function (OA\AbstractAnnotation $annotation, OA\AbstractAnnotation $possibleParent): bool {
117 // regular annotation hierarchy
118 $explicitParent = null !== $possibleParent->matchNested($annotation) && !$annotation instanceof OA\Attachable;
119 $isParentAllowed = false;
120 // support Attachable subclasses
121 if ($isAttachable = $annotation instanceof OA\Attachable) {
122 if (!$isParentAllowed = (null === $annotation->allowedParents())) {
123 // check for allowed parents
124 foreach ($annotation->allowedParents() as $allowedParent) {
125 if ($possibleParent instanceof $allowedParent) {
126 $isParentAllowed = true;
127 break;
128 }
129 }
130 }
131 }
132
133 // Attachables can always be nested (unless explicitly restricted)
134 return ($isAttachable && $isParentAllowed)
135 || ($annotation->getRoot() !== $possibleParent->getRoot() && $explicitParent);
136 };
137
138 $annotationsWithoutParent = [];
139 foreach ($annotations as $index => $annotation) {
140 $mergedIntoParent = false;
141
142 for ($ii = 0; $ii < count($annotations); ++$ii) {
143 if ($ii === $index) {
144 continue;
145 }
146 $possibleParent = $annotations[$ii];
147 if ($isParent($annotation, $possibleParent)) {
148 $mergedIntoParent = true; //
149 $possibleParent->merge([$annotation]);
150 }
151 }
152
153 if (!$mergedIntoParent) {
154 $annotationsWithoutParent[] = $annotation;
155 }
156 }
157
158 return $annotationsWithoutParent;
159 }
160 }