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,41 @@
<?php

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

use Symfony\Component\Validator\Constraints as Assert;

final class OnCollection
{
public function run(bool $param)
{
$constraints = new Assert\Collection([
'example1' => [new Assert\NotBlank(), new Assert\Type('string')],
'example2' => [new Assert\Type('string')],
'example3' => [new Assert\NotBlank(), new Assert\Type('string')],
'example4' => [new Assert\NotBlank(), new Assert\Type('string')],
]);
}
}

?>
-----
<?php

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

use Symfony\Component\Validator\Constraints as Assert;

final class OnCollection
{
public function run(bool $param)
{
$constraints = new Assert\Collection(fields: [
'example1' => [new Assert\NotBlank(), new Assert\Type('string')],
'example2' => [new Assert\Type('string')],
'example3' => [new Assert\NotBlank(), new Assert\Type('string')],
'example4' => [new Assert\NotBlank(), new Assert\Type('string')],
]);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}

// Match classes starting with Symfony\Component\Validator\Constraints\
if (! $node->class instanceof FullyQualified && ! $node->class instanceof Name) {
return null;
Expand Down Expand Up @@ -84,6 +88,19 @@ public function refactor(Node $node): ?Node
return null;
}

$args = $node->getArgs();
if ($className === 'Symfony\Component\Validator\Constraints\Collection'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use SymfonyClass enum by convention to keep classes at one place and to avoid prefixing

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, updated 👍 #852

&& count($args) === 1
&& $args[0]->value instanceof Array_) {

if ($args[0]->name instanceof Identifier) {
return null;
}

$args[0]->name = new Identifier('fields');
return $node;
}

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

Expand Down