Skip to content

Commit d99f8c3

Browse files
authored
Refactor AbstractAnnotation::identity() (#1962)
1 parent ec21679 commit d99f8c3

File tree

4 files changed

+43
-34
lines changed

4 files changed

+43
-34
lines changed

src/Annotations/AbstractAnnotation.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
474474
} elseif (Generator::isDefault($item->{$keyField})) {
475475
$this->_context->logger->error($item->identity() . ' is missing key-field: "' . $keyField . '" in ' . $item->_context);
476476
} elseif (isset($keys[$item->{$keyField}])) {
477-
$this->_context->logger->error('Multiple ' . $item->_identity([]) . ' with the same ' . $keyField . '="' . $item->{$keyField} . "\":\n " . $item->_context . "\n " . $keys[$item->{$keyField}]->_context);
477+
$this->_context->logger->error('Multiple ' . $item->identity([]) . ' with the same ' . $keyField . '="' . $item->{$keyField} . "\":\n " . $item->_context . "\n " . $keys[$item->{$keyField}]->_context);
478478
} else {
479479
$keys[$item->{$keyField}] = $item;
480480
}
@@ -583,24 +583,37 @@ private static function _validate($fields, array $stack, array $skip, string $ba
583583
}
584584

585585
/**
586-
* Return a identity for easy debugging.
587-
* Example: "@OA\Get(path="/pets")".
586+
* Return a simple string representation of the annotation.
587+
*
588+
* @param array|null $properties the properties to include in the string representation
589+
* @example "@OA\Response(response=200)"
588590
*/
589-
public function identity(): string
591+
public function identity(?array $properties = null): string
590592
{
591593
$class = static::class;
592-
$properties = [];
593-
/** @var class-string<AbstractAnnotation> $parent */
594-
foreach (static::$_parents as $parent) {
595-
foreach ($parent::$_nested as $annotationClass => $entry) {
596-
if ($annotationClass === $class && is_array($entry) && !Generator::isDefault($this->{$entry[1]})) {
597-
$properties[] = $entry[1];
598-
break 2;
594+
595+
if (null === $properties) {
596+
$properties = [];
597+
/** @var class-string<AbstractAnnotation> $parent */
598+
foreach (static::$_parents as $parent) {
599+
foreach ($parent::$_nested as $annotationClass => $entry) {
600+
if ($annotationClass === $class && is_array($entry) && !Generator::isDefault($this->{$entry[1]})) {
601+
$properties[] = $entry[1];
602+
break 2;
603+
}
599604
}
600605
}
601606
}
602607

603-
return $this->_identity($properties);
608+
$details = [];
609+
foreach ($properties as $property) {
610+
$value = $this->{$property};
611+
if ($value !== null && !Generator::isDefault($value)) {
612+
$details[] = $property . '=' . (is_string($value) ? '"' . $value . '"' : $value);
613+
}
614+
}
615+
616+
return static::shorten(static::class) . '(' . implode(',', $details) . ')';
604617
}
605618

606619
/**
@@ -650,22 +663,6 @@ public function isRoot(string $rootClass): bool
650663
return static::class === $rootClass || $this->getRoot() === $rootClass;
651664
}
652665

653-
/**
654-
* Helper for generating the identity().
655-
*/
656-
protected function _identity(array $properties): string
657-
{
658-
$fields = [];
659-
foreach ($properties as $property) {
660-
$value = $this->{$property};
661-
if ($value !== null && !Generator::isDefault($value)) {
662-
$fields[] = $property . '=' . (is_string($value) ? '"' . $value . '"' : $value);
663-
}
664-
}
665-
666-
return static::shorten(static::class) . '(' . implode(',', $fields) . ')';
667-
}
668-
669666
/**
670667
* Validates the matching of the property value to a annotation type.
671668
*

src/Annotations/Operation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
221221
if (!Generator::isDefault($this->responses)) {
222222
foreach ($this->responses as $response) {
223223
if (!Generator::isDefault($response->response) && $response->response !== 'default' && preg_match('/^([12345]{1}\d{2})|([12345]{1}XX)$/', (string) $response->response) === 0) {
224-
$this->_context->logger->warning('Invalid value "' . $response->response . '" for ' . $response->_identity([]) . '->response, expecting "default", a HTTP Status Code or HTTP Status Code range definition in ' . $response->_context);
224+
$this->_context->logger->warning('Invalid value "' . $response->response . '" for ' . $response->identity([]) . '->response, expecting "default", a HTTP Status Code or HTTP Status Code range definition in ' . $response->_context);
225225
$valid = false;
226226
}
227227
}

src/Annotations/Parameter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,9 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
289289
return $valid;
290290
}
291291

292-
/**
293-
* @inheritdoc
294-
*/
295-
public function identity(): string
292+
#[\Override]
293+
public function identity(?array $properties = []): string
296294
{
297-
return parent::_identity(['name', 'in']);
295+
return parent::identity(['name', 'in']);
298296
}
299297
}

tests/Annotations/AbstractAnnotationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OpenApi\Tests\Annotations;
88

99
use OpenApi\Annotations as OA;
10+
use OpenApi\Annotations\Response;
1011
use OpenApi\Tests\OpenApiTestCase;
1112
use PHPUnit\Framework\Attributes\DataProvider;
1213

@@ -142,6 +143,19 @@ public function testValidateExamples(): void
142143

143144
$analysis->validate();
144145
}
146+
147+
public static function identityCases(): iterable
148+
{
149+
yield 'default' => [new Response(['response' => 200]), null, '@OA\Response(response=200)'];
150+
yield '[]' => [new Response(['response' => 200]), [], '@OA\Response()'];
151+
yield 'custom' => [new Response(['response' => 200]), ['response'], '@OA\Response(response=200)'];
152+
}
153+
154+
#[DataProvider('identityCases')]
155+
public function testIdentity(OA\AbstractAnnotation $annotation, ?array $identityArgs, string $expected): void
156+
{
157+
$this->assertSame($expected, $annotation->identity($identityArgs));
158+
}
145159
}
146160

147161
class SubSchema extends OA\Schema

0 commit comments

Comments
 (0)