[Agent] Fix variable.undefined PHPStan error on Toolbox::execute()#2103
Closed
peter-si wants to merge 1 commit into
Closed
[Agent] Fix variable.undefined PHPStan error on Toolbox::execute()#2103peter-si wants to merge 1 commit into
peter-si wants to merge 1 commit into
Conversation
PHPStan's flow analysis can't infer that `$sourceCollection` is defined on line 110 because it's only assigned inside the `if ($tool instanceof HasSourcesInterface)` branch on line 104. The ternary on line 110 uses the same instanceof check, so the code is safe at runtime, but the analysis triggers `variable.undefined`. Initialise `$sourceCollection` to null up front and drop the second instanceof check from the ToolResult constructor call. Behavior is identical, PHPStan happy. Existing coverage via `ToolboxTest::testSourcesGetFromToolIntoResult` exercises the HasSourcesInterface branch and remains green.
72dfe68 to
1d01c65
Compare
5 tasks
wachterjohannes
added a commit
to wachterjohannes/symfony-ai
that referenced
this pull request
May 19, 2026
Pre-existing issue on main, also tracked in symfony#2100 and symfony#2103. Inlined here so this PR's CI goes green without waiting for either to merge first; drop this commit if one of them lands first.
Contributor
|
#2100 :) |
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
Toolbox::execute()trips PHPStan's flow analysis with:The variable is assigned inside an
if ($tool instanceof HasSourcesInterface)branch on line 104, then read back via a ternary using the same instanceof check on line 110. The code is safe at runtime — PHP only evaluates the variable when the ternary condition is true — but PHPStan can't infer this control-flow correlation, so the analysis fails.Fix
Initialise
$sourceCollection = nullup front and drop the redundant$tool instanceof HasSourcesInterface ? ... : nullternary; just pass$sourceCollectiontoToolResultdirectly. Behavior is identical.Verification
phpstan analyse src/agent→ greenphpuniton Agent component → 247 tests, 596 assertions, all greenCurrently the failure is blocking PHPStan on any Agent-touching PR (and also surfaces on PRs that don't touch Agent, e.g. #2102, because the matrix runs Agent's PHPStan unconditionally).
🤖 Generated with Claude Code