Skip to content

[CodeQuality] Skip constraint without own constructor in LoadValidatorMetadataToAttributeRector - #1001

Merged
TomasVotruba merged 2 commits into
mainfrom
skip-constraint-without-own-constructor
Aug 3, 2026
Merged

[CodeQuality] Skip constraint without own constructor in LoadValidatorMetadataToAttributeRector#1001
TomasVotruba merged 2 commits into
mainfrom
skip-constraint-without-own-constructor

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Follow-up to #1000.

A constraint that adds no constructor of its own inherits Constraint::__construct(mixed $options = null, ?array $groups = null, mixed $payload = null) and configures itself through public properties. Turning its options array into named arguments produces an attribute with parameters that do not exist:

#[\Attribute]
final class UniqueUserAlias extends Constraint
{
    public $message = 'This alias is already in use.';

    public $field = '';

    public function getTargets(): string
    {
        return self::CLASS_CONSTRAINT;
    }

    public function getDefaultOption(): string
    {
        return 'field';
    }
}
+#[\Mautic\LeadBundle\Form\Validator\Constraints\UniqueUserAlias(field: 'alias', message: 'mautic.lead.list.alias.unique')]
 final class SomeClass
 {
     private $alias;
-
-    public static function loadValidatorMetadata(ClassMetadata $metadata): void
-    {
-        $metadata->addConstraint(new UniqueUserAlias([
-            'field'   => 'alias',
-            'message' => 'mautic.lead.list.alias.unique',
-        ]));
-    }
 }

Such a constraint is now left in loadValidatorMetadata(). The new ConstraintConstructorAnalyzer checks the constraint declares a constructor below Symfony\Component\Validator\Constraint, so the Symfony constraints and any custom one with its own constructor keep converting.

The last commit fixes a method chaining indentation ECS error that came in with #1000.

@TomasVotruba
TomasVotruba merged commit df48f95 into main Aug 3, 2026
7 checks passed
@TomasVotruba
TomasVotruba deleted the skip-constraint-without-own-constructor branch August 3, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant