Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use OpenApi\Annotations as OA;

final class OpenAPIPropertyExampleKeepNumericString
{
/**
* @OA\Property(
* example="01112"
* )
*/
public ?string $uid = null;
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use OpenApi\Attributes as OA;

final class OpenAPIPropertyExampleKeepNumericString
{
#[OA\Property(example: '01112')]
public ?string $uid = null;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,8 @@
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Annotation\SomeProperty',
'Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\SomeProperty'
),

// for testing allow numeric string keep as string
new AnnotationToAttribute('OpenApi\\Annotations\\Property', 'OpenApi\\Attributes\\Property'),
]);
};
18 changes: 14 additions & 4 deletions src/PhpAttribute/AttributeArrayNameInliner.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@

final class AttributeArrayNameInliner
{
/**
* @var class-string
*/
private const OPEN_API_PROPERTY_ATTRIBUTE = 'OpenApi\Attributes\Property';

/**
* @param Array_|list<Arg> $array
* @return list<Arg>
*/
public function inlineArrayToArgs(Array_|array $array): array
public function inlineArrayToArgs(Array_|array $array, ?string $attributeClass = null): array
{
if (is_array($array)) {
return $this->inlineArray($array);
return $this->inlineArray($array, $attributeClass);
}

return $this->inlineArrayNode($array);
Expand Down Expand Up @@ -56,11 +61,16 @@ private function inlineArrayNode(Array_ $array): array
* @param list<Arg> $args
* @return list<Arg>
*/
private function inlineArray(array $args): array
private function inlineArray(array $args, ?string $attributeClass = null): array
{
Assert::allIsAOf($args, Arg::class);

foreach ($args as $arg) {
if ($attributeClass === self::OPEN_API_PROPERTY_ATTRIBUTE
&& $arg->name instanceof Identifier
&& $arg->name->toString() === 'example') {
continue;
}

if ($arg->value instanceof String_ && is_numeric($arg->value->value)) {
// use equal over identical on purpose to verify if it is an integer
if ((float) $arg->value->value == (int) $arg->value->value) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function create(

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

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

$attributeName = $this->attributeNameFactory->create(
$annotationToAttribute,
Expand Down
17 changes: 17 additions & 0 deletions stubs/OpenApi/Attributes/Property.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace OpenApi\Attributes;

if (class_exists('OpenApi\Attributes\Property')) {
return;
}

#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::TARGET_PROPERTY | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::IS_REPEATABLE)]
class Property
{
public function __construct(mixed $example)
{
}
}