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 @@ +