Account for CLI SAPI when determining cliArgumentsVariablesRegistered default#5621
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Account for CLI SAPI when determining cliArgumentsVariablesRegistered default#5621phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
cliArgumentsVariablesRegistered default#5621phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…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
Contributor
|
does not make sense, as phpstan is usually executed from the CLI |
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
On PHP 8.5, the
register_argc_argvINI directive defaults to OFF (and is deprecated). However,$argvand$argcare always available in CLI SAPI regardless of this setting. PHPStan'sContainerFactoryusedini_get('register_argc_argv')to determine thecliArgumentsVariablesRegisteredparameter, which returns'0'on PHP 8.5, causing false positive "Variable $argv might not be defined" errors for top-level scripts.Changes
src/DependencyInjection/ContainerFactory.php: changed thecliArgumentsVariablesRegistereddefault fromini_get('register_argc_argv') === '1'toPHP_SAPI === 'cli' || ini_get('register_argc_argv') === '1', correctly accounting for CLI SAPI where$argv/$argcare always availabletests/PHPStan/Rules/Variables/data/bug-14585.phpwith the reproducer from the issuetestBug14585()verifying no errors whencliArgumentsVariablesRegisteredis true (the corrected behavior)testBug14585NotRegistered()documenting the error when the parameter is falseRoot cause
ContainerFactory::create()determined thecliArgumentsVariablesRegisteredparameter solely fromini_get('register_argc_argv'). Before PHP 8.5, this defaulted to'1', so the parameter was effectively alwaystrue. In PHP 8.5, the default changed to'0', breaking the assumption. Since PHPStan always runs as CLI, and CLI SAPI always provides$argv/$argcregardless ofregister_argc_argv, the fix adds aPHP_SAPI === 'cli'check.Analogous cases probed
$argcalongside$argv: both are handled together inDefinedVariableRuleandMutatingScope— fixed by the same parameter changeini_getcalls insrc/: checked all 6 occurrences — the others (memory_limit,precision) are unaffected by PHP 8.5 default changescliArgumentsVariablesRegistered: onlyDefinedVariableRuleuses this parameter — no other rules or components are affectedTest
testBug14585: uses the reproducer from the issue (top-level script withPHP_SAPI !== 'cli'guard and$argvusage), verifies no errors withcliArgumentsVariablesRegistered = truetestBug14585NotRegistered: same code withcliArgumentsVariablesRegistered = false, documents the error that was occurring on PHP 8.5Fixes phpstan/phpstan#14585