Skip to content

Incorrect behavior of ConstraintOptionsToNamedArgumentsRector #9439

@TheoD02

Description

@TheoD02

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/3dd2a727-c594-4fdf-8cc3-b29050c49cfb

<?php

use Symfony\Component\Validator\Constraints as Assert;

final class DemoFile
{
    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')],
        ]);
    }
}

Responsible rules

  • ConstraintOptionsToNamedArgumentsRector

Here’s the Expected Behavior section updated with example1, example2, etc.:


Expected Behavior

Current (incorrect) output:

    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')]
        );
    }

Expected (correct) output:

    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')],
        ]);
    }

Explanation:
The rule ConstraintOptionsToNamedArgumentsRector should convert the array to use the named argument fields, rather than transforming the array keys directly into named arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions