Skip to content

Fix phpstan/phpstan#14318: False positive variable might not be defined#5240

Merged
VincentLanglet merged 8 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-yzi8ao1
Mar 18, 2026
Merged

Fix phpstan/phpstan#14318: False positive variable might not be defined#5240
VincentLanglet merged 8 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-yzi8ao1

Conversation

@phpstan-bot
Copy link
Collaborator

Summary

Fixes a false positive "Variable $sql might not be defined" in catch blocks when the variable is assigned as part of a function argument expression (e.g., $this->maybeThrows($sql = "SELECT * FROM foo")). This was a regression from #5166.

Changes

  • src/Analyser/ExprHandler/MethodCallHandler.php: Moved the method throw point creation from before processArgs to after it, so the throw point's scope includes variables assigned in function arguments.
  • src/Analyser/ExprHandler/StaticCallHandler.php: Applied the same fix for static method calls.
  • tests/PHPStan/Rules/Variables/data/bug-14318.php: New test data file reproducing both the direct and nested assignment cases.
  • tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php: Added testBug14318 test method.

Root cause

In MethodCallHandler, the throw point for a method call was created before processArgs() was called. This meant the throw point's scope didn't include variables assigned in argument expressions (like $sql = "..." in $this->maybeThrows($sql = "SELECT * FROM foo")). When catch blocks merged throw point scopes, they used a scope where $sql wasn't yet defined, leading to the false positive "might not be defined" error.

FuncCallHandler already had the correct order (throw point created after processArgs), so only MethodCallHandler and StaticCallHandler needed fixing.

Test

Added a regression test with two cases:

  1. Direct assignment in argument: $this->maybeThrows5($sql = "SELECT * FROM foo")
  2. Nested assignment via another function: $this->maybeThrows6(strlen($sql = "SELECT * FROM foo"))

Both should report no errors when accessing $sql in the catch block.

Fixes phpstan/phpstan#14318

…ed in catch block

- Moved method call throw point creation to after processArgs in MethodCallHandler
- Applied the same fix to StaticCallHandler for consistency
- The throw point scope now includes variables assigned in function arguments
- New regression test in tests/PHPStan/Rules/Variables/data/bug-14318.php
@VincentLanglet VincentLanglet requested a review from staabm March 17, 2026 22:29
@phpstan-bot
Copy link
Collaborator Author

All tests pass (11598 tests, 0 failures) and static analysis reports no errors. The PR is approved and ready to merge — no additional changes needed.

Copy link
Contributor

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fix also seem to address more issues.

please add regression tests for

Both issues involve variables in try/catch blocks not being properly
tracked - #11284 for by-reference parameters and #7806 for preg_match
matches. These are also fixed by the throw point scope fix in phpstan#5240.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Collaborator Author

Already handled — all 11600 tests passed and the commit has been pushed.

Cover the StaticCallHandler code path with regression tests
using static method calls (self::maybeThrows()) alongside
the existing instance method call tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
phpstan-bot and others added 3 commits March 18, 2026 07:02
Adds regression tests for regular function calls (FuncCallHandler) with
variable assignment in arguments, proving this handler already had the
correct throw point order.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Collaborator Author

Already retrieved — all 11,600 tests passed. Both tests and static analysis are green. The PR is approved with no outstanding changes needed.

@VincentLanglet VincentLanglet merged commit f077b36 into phpstan:2.1.x Mar 18, 2026
649 of 651 checks passed
@VincentLanglet VincentLanglet deleted the create-pull-request/patch-yzi8ao1 branch March 18, 2026 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants