Skip to content

Commit 691c46b

Browse files
authored
[Symfony73] Skip with @translate docblock on ConstraintOptionsToNamedArgumentsRector (#920)
* [Symfony73] Skip with @translate docblock on ConstraintOptionsToNamedArgumentsRector * fix * fix * final touch: also skip comment
1 parent eadb590 commit 691c46b

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\Validator\Constraints\NotBlank;
9+
10+
class SkipWithTranslateComment extends AbstractType
11+
{
12+
public function buildForm(FormBuilderInterface $builder, array $options): void
13+
{
14+
$builder
15+
->add('name', TextType::class, [
16+
'constraints' => [
17+
new NotBlank(['message' =>
18+
// @Translate
19+
'Name is required.'
20+
]),
21+
],
22+
]);
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\Validator\Constraints\NotBlank;
9+
10+
class SkipWithTranslateDocblock extends AbstractType
11+
{
12+
public function buildForm(FormBuilderInterface $builder, array $options): void
13+
{
14+
$builder
15+
->add('name', TextType::class, [
16+
'constraints' => [
17+
new NotBlank(['message' => /** @Translate */ 'Name is required.']),
18+
],
19+
]);
20+
}
21+
}

rules/Symfony73/Rector/Class_/ConstraintOptionsToNamedArgumentsRector.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public function refactor(Node $node): ?Node
110110

111111
$array = $node->args[0]->value;
112112
$namedArgs = [];
113+
$oldTokens = $this->file->getOldTokens();
113114

114115
foreach ($array->items as $item) {
115116
if (! $item instanceof ArrayItem) {
@@ -130,6 +131,22 @@ public function refactor(Node $node): ?Node
130131
continue;
131132
}
132133

134+
$lastTokenKey = $item->key->getEndTokenPos();
135+
$startTokenValue = $item->value->getStartTokenPos();
136+
137+
while ($lastTokenKey < $startTokenValue) {
138+
++$lastTokenKey;
139+
140+
if (! isset($oldTokens[$lastTokenKey])) {
141+
break;
142+
}
143+
144+
$token = $oldTokens[$lastTokenKey];
145+
if ($token->is([T_DOC_COMMENT, T_COMMENT])) {
146+
return null;
147+
}
148+
}
149+
133150
$arg = new Arg($item->value);
134151
$arg->name = new Identifier($keyValue);
135152

0 commit comments

Comments
 (0)