Fix phpstan/phpstan#13960: Invalid closure.unusedUse error when including external files#5350
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
…quire - Treat include/require/include_once/require_once as using all variables in scope - Included files inherit the variable scope of the including line - Same approach as existing get_defined_vars() and func_get_args() handling - Removed two now-unnecessary baseline entries for CommandHelper.php and PHPStanTestCase.php - New regression test in tests/PHPStan/Rules/Functions/data/bug-13960.php Fixes phpstan/phpstan#13960
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
When a closure contains
require,include,require_once, orinclude_once, the included file inherits the variable scope of the closure. PHPStan was incorrectly reportingclosure.unusedUsefor variables passed viause()that could be accessed by the included file.Changes
src/Rules/UnusedFunctionParametersCheck.phpto treatInclude_nodes (which represent all four include/require variants) as using all defined variables, similar to howget_defined_vars()andfunc_get_args()are already handled.phpstan-baseline.neonforsrc/Command/CommandHelper.phpandsrc/Testing/PHPStanTestCase.phpthat were suppressing exactly this false positive.tests/PHPStan/Rules/Functions/data/bug-13960.phpcoveringrequire,include,require_once, andinclude_once.testBug13960intests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php.Root cause
The
getUsedVariables()method inUnusedFunctionParametersCheckwalks the AST to find variable references, butinclude/requirestatements don't directly reference variables — they just load a file. However, per PHP documentation, the included file inherits the variable scope of the line where the include occurs. The fix treats include/require the same asget_defined_vars(): all variables in scope are considered "used" since the included file could reference any of them.Test
The regression test creates closures with
use ($aap)containing each of the four include/require variants. Before the fix, all four produced falseclosure.unusedUseerrors. After the fix, no errors are reported.Fixes phpstan/phpstan#13960