Skip to content

Order optional command parameters last in InvokableCommandInputAttributeRector#944

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-optional-params-order-9546
Jun 26, 2026
Merged

Order optional command parameters last in InvokableCommandInputAttributeRector#944
TomasVotruba merged 1 commit into
mainfrom
fix-optional-params-order-9546

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes rectorphp/rector#9546

Problem

InvokableCommandInputAttributeRector emitted __invoke() parameters in the same order the addArgument()/addOption() calls appeared in configure(). When an optional parameter (one with a default value) was declared before a required one, the generated method ended up with an optional parameter in front of a required parameter — which is invalid PHP and would have to be fixed up afterwards by OptionalParametersAfterRequiredRector.

Fix

After building the __invoke() parameter list, parameters are partitioned so required parameters (no default) keep their relative order and come first, followed by optional parameters (with a default). This guarantees optional parameters are always listed last.

This is a fresh, passing take on the previously-closed #899 — the trailing-comma formatting problem reported there no longer reproduces with the current printer.

Tests

  • Updated option_with_array_type.php.inc fixture to reflect the corrected order.
  • Added optional_option_before_required.php.inc fixture covering an optional option declared before a required one.

@TomasVotruba TomasVotruba force-pushed the fix-optional-params-order-9546 branch from 7bbf5da to 80a1a33 Compare June 26, 2026 10:48
@TomasVotruba TomasVotruba merged commit 77cc62b into main Jun 26, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the fix-optional-params-order-9546 branch June 26, 2026 13:20
@TomasVotruba

Copy link
Copy Markdown
Member Author

Already fixed, just adding the fixture here

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.

Parameter order for optional parameters in InvokableCommandInputAttributeRector

1 participant