Skip to content

Account for CLI SAPI when determining cliArgumentsVariablesRegistered default#5621

Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-i5wf3qw
Closed

Account for CLI SAPI when determining cliArgumentsVariablesRegistered default#5621
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-i5wf3qw

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

On PHP 8.5, the register_argc_argv INI directive defaults to OFF (and is deprecated). However, $argv and $argc are always available in CLI SAPI regardless of this setting. PHPStan's ContainerFactory used ini_get('register_argc_argv') to determine the cliArgumentsVariablesRegistered parameter, which returns '0' on PHP 8.5, causing false positive "Variable $argv might not be defined" errors for top-level scripts.

Changes

  • Modified src/DependencyInjection/ContainerFactory.php: changed the cliArgumentsVariablesRegistered default from ini_get('register_argc_argv') === '1' to PHP_SAPI === 'cli' || ini_get('register_argc_argv') === '1', correctly accounting for CLI SAPI where $argv/$argc are always available
  • Added test data file tests/PHPStan/Rules/Variables/data/bug-14585.php with the reproducer from the issue
  • Added testBug14585() verifying no errors when cliArgumentsVariablesRegistered is true (the corrected behavior)
  • Added testBug14585NotRegistered() documenting the error when the parameter is false

Root cause

ContainerFactory::create() determined the cliArgumentsVariablesRegistered parameter solely from ini_get('register_argc_argv'). Before PHP 8.5, this defaulted to '1', so the parameter was effectively always true. In PHP 8.5, the default changed to '0', breaking the assumption. Since PHPStan always runs as CLI, and CLI SAPI always provides $argv/$argc regardless of register_argc_argv, the fix adds a PHP_SAPI === 'cli' check.

Analogous cases probed

  • $argc alongside $argv: both are handled together in DefinedVariableRule and MutatingScope — fixed by the same parameter change
  • Other ini_get calls in src/: checked all 6 occurrences — the others (memory_limit, precision) are unaffected by PHP 8.5 default changes
  • Other consumers of cliArgumentsVariablesRegistered: only DefinedVariableRule uses this parameter — no other rules or components are affected

Test

  • testBug14585: uses the reproducer from the issue (top-level script with PHP_SAPI !== 'cli' guard and $argv usage), verifies no errors with cliArgumentsVariablesRegistered = true
  • testBug14585NotRegistered: same code with cliArgumentsVariablesRegistered = false, documents the error that was occurring on PHP 8.5

Fixes phpstan/phpstan#14585

…d` 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
@staabm
Copy link
Copy Markdown
Contributor

staabm commented May 9, 2026

does not make sense, as phpstan is usually executed from the CLI

@staabm staabm closed this May 9, 2026
@staabm staabm deleted the create-pull-request/patch-i5wf3qw branch May 9, 2026 15:03
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.

2 participants