Skip to content

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
phpstan-bot:create-pull-request/patch-r9wryev
Open

Fix phpstan/phpstan#13960: Invalid closure.unusedUse error when including external files#5350
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-r9wryev

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When a closure contains require, include, require_once, or include_once, the included file inherits the variable scope of the closure. PHPStan was incorrectly reporting closure.unusedUse for variables passed via use() that could be accessed by the included file.

Changes

  • Modified src/Rules/UnusedFunctionParametersCheck.php to treat Include_ nodes (which represent all four include/require variants) as using all defined variables, similar to how get_defined_vars() and func_get_args() are already handled.
  • Removed two now-unnecessary baseline entries in phpstan-baseline.neon for src/Command/CommandHelper.php and src/Testing/PHPStanTestCase.php that were suppressing exactly this false positive.
  • Added regression test in tests/PHPStan/Rules/Functions/data/bug-13960.php covering require, include, require_once, and include_once.
  • Added test method testBug13960 in tests/PHPStan/Rules/Functions/UnusedClosureUsesRuleTest.php.

Root cause

The getUsedVariables() method in UnusedFunctionParametersCheck walks the AST to find variable references, but include/require statements 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 as get_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 false closure.unusedUse errors. After the fix, no errors are reported.

Fixes phpstan/phpstan#13960

…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
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.

1 participant