From ad422bdf100f77cd02c309e0ad82ac4bbda29618 Mon Sep 17 00:00:00 2001 From: staabm <120441+staabm@users.noreply.github.com> Date: Sat, 9 May 2026 14:47:52 +0000 Subject: [PATCH] Account for CLI SAPI when determining `cliArgumentsVariablesRegistered` default - In PHP 8.5, `register_argc_argv` defaults to OFF, but `$argv`/`$argc` are always available in CLI SAPI regardless of this setting - `ContainerFactory` used `ini_get('register_argc_argv')` alone, which returns '0' on PHP 8.5, causing false "Variable $argv might not be defined" errors at the top level - Add `PHP_SAPI === 'cli'` check so the parameter is correctly set to true when PHPStan runs as CLI (which is always the case) - Add regression test with the reproducer from the issue --- src/DependencyInjection/ContainerFactory.php | 3 ++- .../Variables/DefinedVariableRuleTest.php | 23 +++++++++++++++++++ .../Rules/Variables/data/bug-14585.php | 12 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/PHPStan/Rules/Variables/data/bug-14585.php diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index cac88d0e39b..fca6eefbeb4 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -53,6 +53,7 @@ use function sprintf; use function str_ends_with; use function substr; +use const PHP_SAPI; /** * @api @@ -138,7 +139,7 @@ public function create( $configurator->addParameters(array_merge([ 'rootDir' => $this->rootDirectory, 'currentWorkingDirectory' => $this->currentWorkingDirectory, - 'cliArgumentsVariablesRegistered' => ini_get('register_argc_argv') === '1', + 'cliArgumentsVariablesRegistered' => PHP_SAPI === 'cli' || ini_get('register_argc_argv') === '1', 'tmpDir' => $tempDirectory, 'additionalConfigFiles' => $additionalConfigFiles, 'allConfigFiles' => $allConfigFiles, diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index b6cc5f03bc3..dbbc4394670 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1540,6 +1540,29 @@ public function testBug6688(): void ]); } + public function testBug14585(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = false; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + $this->analyse([__DIR__ . '/data/bug-14585.php'], []); + } + + public function testBug14585NotRegistered(): void + { + $this->cliArgumentsVariablesRegistered = false; + $this->polluteScopeWithLoopInitialAssignments = false; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + $this->analyse([__DIR__ . '/data/bug-14585.php'], [ + [ + 'Variable $argv might not be defined.', + 7, + ], + ]); + } + #[RequiresPhp('>= 8.0.0')] public function testBug10729(): void { diff --git a/tests/PHPStan/Rules/Variables/data/bug-14585.php b/tests/PHPStan/Rules/Variables/data/bug-14585.php new file mode 100644 index 00000000000..008fb9f464e --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-14585.php @@ -0,0 +1,12 @@ +