From 81423dc12b5a7af4c058b0ae7d662203647b77a5 Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Sat, 5 Nov 2022 15:19:21 -0400 Subject: [PATCH] Remove `argc` and `argv` elements from `$_ENV` Fixes vimeo/psalm#8662 --- .../Expression/Fetch/VariableFetchAnalyzer.php | 14 ++++++++++---- tests/SuperGlobalsTest.php | 9 +++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php index 2d7f5bca7..26ecddde3 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php @@ -650,11 +650,9 @@ class VariableFetchAnalyzer $bool_string_helper = new Union([new TBool(), new TString()]); $bool_string_helper->possibly_undefined = true; - $detailed_type = new TKeyedArray([ + $detailed_type_members = [ // https://www.php.net/manual/en/reserved.variables.server.php 'PHP_SELF' => $non_empty_string_helper, - 'argv' => $argv_helper, - 'argc' => $argc_helper, 'GATEWAY_INTERFACE' => $non_empty_string_helper, 'SERVER_ADDR' => $non_empty_string_helper, 'SERVER_NAME' => $non_empty_string_helper, @@ -727,7 +725,15 @@ class VariableFetchAnalyzer // phpunit 'APP_DEBUG' => $bool_string_helper, 'APP_ENV' => $string_helper, - ]); + ]; + + if ($var_id === '$_SERVER') { + // those elements are not usually present in $_ENV + $detailed_type_members['argc'] = $argc_helper; + $detailed_type_members['argv'] = $argv_helper; + } + + $detailed_type = new TKeyedArray($detailed_type_members); // generic case for all other elements $detailed_type->previous_key_type = Type::getNonEmptyString(); diff --git a/tests/SuperGlobalsTest.php b/tests/SuperGlobalsTest.php index d13919f23..ad052fd56 100644 --- a/tests/SuperGlobalsTest.php +++ b/tests/SuperGlobalsTest.php @@ -22,5 +22,14 @@ class SuperGlobalsTest extends TestCase ', 'assertions' => [] ]; + + yield 'ENV has scalar entries only' => [ + ' */ + function f(): array { + return $_ENV; + } + ' + ]; } }