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
1 change: 1 addition & 0 deletions config/sets/symfony/symfony7/symfony73.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-console.php');
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-security-core.php');
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-twig-bundle.php');
$rectorConfig->import(__DIR__ . '/symfony73/symfony73-validator.php');
};
10 changes: 10 additions & 0 deletions config/sets/symfony/symfony7/symfony73/symfony73-validator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([ConstraintOptionsToNamedArgumentsRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ConstraintOptionsToNamedArgumentsRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class SomeForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'constraints' => [
new NotBlank(['message' => 'Name is required.']),
],
]);
}
}
?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector\Fixture;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\NotBlank;

class SomeForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name', TextType::class, [
'constraints' => [
new NotBlank(message: 'Name is required.'),
],
]);
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\Class_\ConstraintOptionsToNamedArgumentsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ConstraintOptionsToNamedArgumentsRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Symfony73\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ConstraintOptionsToNamedArgumentsRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Refactor Symfony constraints using array options to named arguments syntax for better readability and type safety.',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;

$constraint = new NotBlank(['message' => 'This field should not be blank.']);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Validator\Constraints\NotBlank;

$constraint = new NotBlank(message: 'This field should not be blank.');
CODE_SAMPLE
)]
);
}

public function getNodeTypes(): array
{
return [New_::class];
}

public function refactor(Node $node): ?Node
{
if (!$node instanceof New_) {
return null;
}

// Match classes starting with Symfony\Component\Validator\Constraints\
if (!$node->class instanceof FullyQualified && !$node->class instanceof Name) {
return null;
}

$className = $this->getName($node->class);
if (!is_string($className)) {
return null;
}

if (!str_starts_with($className, 'Symfony\Component\Validator\Constraints\\')) {
return null;
}

if (
0 === count($node->args) ||
!$node->args[0] instanceof Arg ||
!$node->args[0]->value instanceof Array_
) {
return null;
}

$array = $node->args[0]->value;
$namedArgs = [];

foreach ($array->items as $item) {
if (!$item instanceof ArrayItem || null === $item->key) {
continue;
}

$keyValue = $this->valueResolver->getValue($item->key);
if (!is_string($keyValue)) {
continue;
}

$arg = new Node\Arg($item->value);
$arg->name = new Node\Identifier($keyValue);

$namedArgs[] = $arg;
}

$node->args = $namedArgs;

return $node;
}
}
6 changes: 6 additions & 0 deletions src/Set/SetProvider/Symfony7SetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public function provide(): array
'7.3',
__DIR__ . '/../../../config/sets/symfony/symfony7/symfony73/symfony73-twig-bundle.php'
),
new ComposerTriggeredSet(
SetGroup::SYMFONY,
'symfony/validator',
'7.3',
__DIR__ . '/../../../config/sets/symfony/symfony7/symfony73/symfony73-validator.php'
),
];
}
}