Skip to content

Commit 3d39f6b

Browse files
authored
[Symfony73] Fix Collection fields name usage on ConstraintOptionsToNamedArgumentsRector (#851)
1 parent 5078be3 commit 3d39f6b

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
7+
final class OnCollection
8+
{
9+
public function run(bool $param)
10+
{
11+
$constraints = new Assert\Collection([
12+
'example1' => [new Assert\NotBlank(), new Assert\Type('string')],
13+
'example2' => [new Assert\Type('string')],
14+
'example3' => [new Assert\NotBlank(), new Assert\Type('string')],
15+
'example4' => [new Assert\NotBlank(), new Assert\Type('string')],
16+
]);
17+
}
18+
}
19+
20+
?>
21+
-----
22+
<?php
23+
24+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;
25+
26+
use Symfony\Component\Validator\Constraints as Assert;
27+
28+
final class OnCollection
29+
{
30+
public function run(bool $param)
31+
{
32+
$constraints = new Assert\Collection(fields: [
33+
'example1' => [new Assert\NotBlank(), new Assert\Type('string')],
34+
'example2' => [new Assert\Type('string')],
35+
'example3' => [new Assert\NotBlank(), new Assert\Type('string')],
36+
'example4' => [new Assert\NotBlank(), new Assert\Type('string')],
37+
]);
38+
}
39+
}
40+
41+
?>

rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public function refactor(Node $node): ?Node
5757
return null;
5858
}
5959

60+
if ($node->isFirstClassCallable()) {
61+
return null;
62+
}
63+
6064
// Match classes starting with Symfony\Component\Validator\Constraints\
6165
if (! $node->class instanceof FullyQualified && ! $node->class instanceof Name) {
6266
return null;
@@ -84,6 +88,19 @@ public function refactor(Node $node): ?Node
8488
return null;
8589
}
8690

91+
$args = $node->getArgs();
92+
if ($className === 'Symfony\Component\Validator\Constraints\Collection'
93+
&& count($args) === 1
94+
&& $args[0]->value instanceof Array_) {
95+
96+
if ($args[0]->name instanceof Identifier) {
97+
return null;
98+
}
99+
100+
$args[0]->name = new Identifier('fields');
101+
return $node;
102+
}
103+
87104
$array = $node->args[0]->value;
88105
$namedArgs = [];
89106

0 commit comments

Comments
 (0)