Repository navigation
AnnotationToAttributeRector: fold unknown named args into the attribute's $options - #8914
Merged
TomasVotruba merged 1 commit intoNov 3, 2026
Conversation
…te's $options
Annotation arguments become named attribute arguments. When the attribute
constructor does not declare a parameter of that name, PHP throws "Unknown
named parameter" as soon as the attribute is instantiated, while the annotation
form was accepted.
This hits Symfony validator constraints on versions where a setting is only
reachable through the $options array, e.g. on Symfony 5.4:
@Assert\Length(min=1, allowEmptyString=true)
-> #[Assert\Length(min: 1, allowEmptyString: true)] // Error on load
Now, when the attribute class is resolvable, its constructor has no parameter of
that name, and it has an array-accepting $options parameter, the unknown
arguments are folded into it:
#[Assert\Length(min: 1, options: ['allowEmptyString' => true])]
Anything else is untouched: unresolvable classes, constructors without
$options, variadic constructors, positional args, and an explicit non-array
options argument.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
samsonasik
approved these changes
Nov 3, 2026
Member
|
Thank you |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
AnnotationToAttributeRectorturns every annotation argument into a named attribute argument. If the attribute constructor has no parameter of that name, PHP throwsUnknown named parameterthe moment the attribute is instantiated - while the annotation form was accepted.I hit this migrating a Symfony 5.4 app. On 5.4
Length::__construct()has noallowEmptyStringparameter, the setting is only reachable through$options:The error only shows up when the constraint metadata for that class is loaded, so it passes lint and static analysis.
Change
New
UnknownNamedArgsToOptionsFolder, called fromPhpAttributeGroupFactory::create(). When the attribute class is resolvable, its constructor has no parameter with a given name, and it has an array-accepting$optionsparameter, the unknown arguments are folded into it:It is deliberately conservative and leaves the arguments untouched when:
$optionsparameter, or is variadicoptions:argument exists and is not an array literalThe check uses the project's installed class, so the result follows the installed Symfony version: on versions where the constructor does declare the parameter, it stays a named argument.
Tests
Three fixtures with stub attribute classes (Symfony's validator is not installed in this repo): unknown arg folded, known args stay named, no
$optionsparameter left alone. The first fails without the change.vendor/bin/phpunitpasses (4874 tests) and ECS is clean. PHPStan reports the same 17 errors onmainwith and without this change, none in touched files. I did not run it against the real Symfony 5.4Lengthclass, only against the stubs.Happy to adjust the approach if you would rather solve this differently (e.g. in rector-symfony).
🤖 Generated with Claude Code