Skip to content

Commit 07654a9

Browse files
authored
[Php80] Handle crash on empty name attribute field value on AnnotationToAttributeRector (#7864)
* [Php80] Handle crash on empty name attribute field value on AnnotationToAttributeRector * fix
1 parent 92ddbf0 commit 07654a9

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
4+
5+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\EmptyAttribute;
6+
7+
/**
8+
* @EmptyAttribute(foo="")
9+
*/
10+
class UseEmptyAttribute
11+
{
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;
19+
20+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\EmptyAttribute;
21+
22+
#[EmptyAttribute(foo: '')]
23+
class UseEmptyAttribute
24+
{
25+
}
26+
27+
?>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute;
6+
7+
use Attribute;
8+
9+
#[Attribute]
10+
final class EmptyAttribute
11+
{
12+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Rector\Php80\ValueObject\AnnotationToAttribute;
1010
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\Annotation\NestedPastAnnotation;
1111
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Annotation\OpenApi\PastAnnotation;
12+
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\EmptyAttribute;
1213
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName1;
1314
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\NewName2;
1415
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\OpenApi\Attribute\NestedFutureAttribute;
@@ -74,5 +75,7 @@
7475

7576
// for testing allow numeric string keep as string
7677
new AnnotationToAttribute('OpenApi\\Annotations\\Property', 'OpenApi\\Attributes\\Property'),
78+
79+
new AnnotationToAttribute(EmptyAttribute::class, null, ['foo'])
7780
]);
7881
};

src/PhpAttribute/NodeFactory/PhpAttributeGroupFactory.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ private function mapClassReferences(Expr|string $expr, array $classReferencedFie
154154
continue;
155155
}
156156

157+
if ($arrayItem->value->value === '') {
158+
continue;
159+
}
160+
157161
$arrayItem->value = new ClassConstFetch(new FullyQualified($arrayItem->value->value), 'class');
158162
}
159163
}

0 commit comments

Comments
 (0)