Skip to content

Commit 7f5d49d

Browse files
authored
[Php80] Handle OpenApi\Attributes\Property example to keep numeric string on AnnotationToAttributeRector (#7677)
* [Php80] Handle OpenApi\Attributes\Property example to keep numeric string on AnnotationToAttributeRector * Fix
1 parent 6535bab commit 7f5d49d

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
4+
5+
use OpenApi\Annotations as OA;
6+
7+
final class OpenAPIPropertyExampleKeepNumericString
8+
{
9+
/**
10+
* @OA\Property(
11+
* example="01112"
12+
* )
13+
*/
14+
public ?string $uid = null;
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
22+
23+
use OpenApi\Attributes as OA;
24+
25+
final class OpenAPIPropertyExampleKeepNumericString
26+
{
27+
#[OA\Property(example: '01112')]
28+
public ?string $uid = null;
29+
}
30+
31+
?>

rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/config/configured_rule.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,8 @@
7171
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Annotation\SomeProperty',
7272
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\SomeProperty'
7373
),
74+
75+
// for testing allow numeric string keep as string
76+
new AnnotationToAttribute('OpenApi\\Annotations\\Property', 'OpenApi\\Attributes\\Property'),
7477
]);
7578
};

src/PhpAttribute/AttributeArrayNameInliner.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515

1616
final class AttributeArrayNameInliner
1717
{
18+
/**
19+
* @var class-string
20+
*/
21+
private const OPEN_API_PROPERTY_ATTRIBUTE = 'OpenApi\Attributes\Property';
22+
1823
/**
1924
* @param Array_|list<Arg> $array
2025
* @return list<Arg>
2126
*/
22-
public function inlineArrayToArgs(Array_|array $array): array
27+
public function inlineArrayToArgs(Array_|array $array, ?string $attributeClass = null): array
2328
{
2429
if (is_array($array)) {
25-
return $this->inlineArray($array);
30+
return $this->inlineArray($array, $attributeClass);
2631
}
2732

2833
return $this->inlineArrayNode($array);
@@ -56,11 +61,16 @@ private function inlineArrayNode(Array_ $array): array
5661
* @param list<Arg> $args
5762
* @return list<Arg>
5863
*/
59-
private function inlineArray(array $args): array
64+
private function inlineArray(array $args, ?string $attributeClass = null): array
6065
{
6166
Assert::allIsAOf($args, Arg::class);
62-
6367
foreach ($args as $arg) {
68+
if ($attributeClass === self::OPEN_API_PROPERTY_ATTRIBUTE
69+
&& $arg->name instanceof Identifier
70+
&& $arg->name->toString() === 'example') {
71+
continue;
72+
}
73+
6474
if ($arg->value instanceof String_ && is_numeric($arg->value->value)) {
6575
// use equal over identical on purpose to verify if it is an integer
6676
if ((float) $arg->value->value == (int) $arg->value->value) {

src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function create(
8585

8686
$this->annotationToAttributeIntegerValueCaster->castAttributeTypes($annotationToAttribute, $args);
8787

88-
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args);
88+
$args = $this->attributeArrayNameInliner->inlineArrayToArgs($args, $annotationToAttribute->getAttributeClass());
8989

9090
$attributeName = $this->attributeNameFactory->create(
9191
$annotationToAttribute,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenApi\Attributes;
6+
7+
if (class_exists('OpenApi\Attributes\Property')) {
8+
return;
9+
}
10+
11+
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::IS_REPEATABLE)]
12+
class Property
13+
{
14+
public function __construct(mixed $example)
15+
{
16+
}
17+
}

0 commit comments

Comments
 (0)