Cast variadic tool parameters element-by-element - #465
Open
Faneraiy14 wants to merge 1 commit into
Open
Conversation
SchemaGenerator::buildVariadicParameterSchema() already advertises a variadic parameter (e.g. `int ...$scores`) as a JSON "array" schema, so a spec-conformant client sends an array for it. But ReferenceHandler::prepareArguments() never special-cased ReflectionParameter::isVariadic(): it fell through to the regular single-value branch and passed the whole array to castArgumentType(), which tried to cast it as one scalar and failed - e.g. "Cannot cast value to integer" for `int ...$scores` given [1, 2, 3]. Any tool handler declaring a variadic parameter was therefore unreachable through the advertised schema, confirmed by direct reproduction against ReferenceHandler::handle() before this change. Casts each array element individually via the existing castArgumentType() (unchanged - it already inspects the variadic parameter's element type, not "array", per PHP reflection semantics) and appends to the result in order; variadic is always the last parameter, so this preserves correct positional ordering. A non-array value for a variadic argument now fails clearly with an invalid-params error instead of the confusing cast message.
Faneraiy14
requested review from
CodeWithKyrian,
Nyholm,
chr-hertel and
soyuka
as code owners
August 19, 2026 14:12
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.
Summary
SchemaGenerator::buildVariadicParameterSchema()already advertises a variadic parameter (e.g.int ...$scores) as a JSON"array"schema, so a spec-conformant client sends an array for it. ButReferenceHandler::prepareArguments()never special-casedReflectionParameter::isVariadic()— it fell through to the regular single-value branch and passed the whole array tocastArgumentType(), which tried to cast it as one scalar and failed (e.g."Cannot cast value to integer"forint ...$scoresgiven[1, 2, 3]).Any tool handler declaring a variadic parameter was therefore unreachable through the advertised schema — confirmed by direct reproduction against
ReferenceHandler::handle()before this change:This resolves the
// TODO: Handle variadic parameters.left inprepareArguments().Fix
Casts each array element individually via the existing
castArgumentType()(unchanged — it already inspects the variadic parameter's element type, notarray, per PHP reflection semantics) and appends to the result in order. Variadic is always the last parameter, so this preserves correct positional ordering. A non-array value for a variadic argument now fails clearly with an invalid-params error instead of the confusing cast message.Test plan
vendor/bin/phpunit --testsuite=unit— 1516 tests, 3923 assertions, all passingvendor/bin/phpstan analyse(full repo, level 6 perphpstan.dist.neon) — no errors