Skip to content

[Agent] Fix variable.undefined PHPStan error on Toolbox::execute()#2103

Closed
peter-si wants to merge 1 commit into
symfony:mainfrom
peter-si:fix/agent-toolbox-source-collection-phpstan
Closed

[Agent] Fix variable.undefined PHPStan error on Toolbox::execute()#2103
peter-si wants to merge 1 commit into
symfony:mainfrom
peter-si:fix/agent-toolbox-source-collection-phpstan

Conversation

@peter-si

@peter-si peter-si commented May 19, 2026

Copy link
Copy Markdown
Q A
Branch? main
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

Summary

Toolbox::execute() trips PHPStan's flow analysis with:

Variable $sourceCollection might not be defined.
src/Toolbox/Toolbox.php:110

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 = null up front and drop the redundant $tool instanceof HasSourcesInterface ? ... : null ternary; just pass $sourceCollection to ToolResult directly. Behavior is identical.

+            $sourceCollection = null;
             if ($tool instanceof HasSourcesInterface) {
                 $tool->setSourceCollection($sourceCollection = new SourceCollection());
             }

             $result = new ToolResult(
                 $toolCall,
                 $tool->{$metadata->getReference()->getMethod()}(...$arguments),
-                $tool instanceof HasSourcesInterface ? $sourceCollection : null,
+                $sourceCollection,
             );

Verification

  • phpstan analyse src/agent → green
  • phpunit on Agent component → 247 tests, 596 assertions, all green

Currently 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

@carsonbot carsonbot added Status: Needs Review Agent Issues & PRs about the AI Agent component labels May 19, 2026
@peter-si peter-si changed the title [Agent] Fix variable.undefined PHPStan error on Toolbox::call() [Agent] Fix variable.undefined PHPStan error on Toolbox::execute() May 19, 2026
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.
@peter-si peter-si force-pushed the fix/agent-toolbox-source-collection-phpstan branch from 72dfe68 to 1d01c65 Compare May 19, 2026 09:42
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.
@4lxndr

4lxndr commented May 19, 2026

Copy link
Copy Markdown
Contributor

#2100 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Agent Issues & PRs about the AI Agent component Status: Needs Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants