Skip to content

Commit 99395bb

Browse files
authored
Merge pull request #8561 from kkmuffme/more-specific-superglobals-feedback-update
More specific superglobals feedback update
2 parents b424de9 + 6012981 commit 99395bb

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Psalm/Internal/Analyzer/Statements/Expression/Fetch/VariableFetchAnalyzer.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psalm\Internal\Codebase\TaintFlowGraph;
1414
use Psalm\Internal\DataFlow\DataFlowNode;
1515
use Psalm\Internal\DataFlow\TaintSource;
16+
use Psalm\Internal\Type\TypeCombiner;
1617
use Psalm\Issue\ImpureVariable;
1718
use Psalm\Issue\InvalidScope;
1819
use Psalm\Issue\PossiblyUndefinedGlobalVariable;
@@ -22,6 +23,7 @@
2223
use Psalm\IssueBuffer;
2324
use Psalm\Type;
2425
use Psalm\Type\Atomic\TArray;
26+
use Psalm\Type\Atomic\TBool;
2527
use Psalm\Type\Atomic\TInt;
2628
use Psalm\Type\Atomic\TIntRange;
2729
use Psalm\Type\Atomic\TKeyedArray;
@@ -645,6 +647,9 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
645647
$request_time_float_helper = Type::getFloat();
646648
$request_time_float_helper->possibly_undefined = true;
647649

650+
$bool_string_helper = new Union([new TBool(), new TString()]);
651+
$bool_string_helper->possibly_undefined = true;
652+
648653
$detailed_type = new TKeyedArray([
649654
// https://www.php.net/manual/en/reserved.variables.server.php
650655
'PHP_SELF' => $non_empty_string_helper,
@@ -719,6 +724,9 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
719724
'HTTP_SEC_CH_UA_PLATFORM' => $non_empty_string_helper,
720725
'HTTP_SEC_CH_UA_MOBILE' => $non_empty_string_helper,
721726
'HTTP_SEC_CH_UA' => $non_empty_string_helper,
727+
// phpunit
728+
'APP_DEBUG' => $bool_string_helper,
729+
'APP_ENV' => $string_helper,
722730
]);
723731

724732
// generic case for all other elements
@@ -739,15 +747,15 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
739747
new TNonEmptyList(Type::getString()),
740748
]),
741749
'size' => new Union([
742-
new TInt(),
750+
new TIntRange(0, null),
743751
new TNonEmptyList(Type::getInt()),
744752
]),
745753
'tmp_name' => new Union([
746754
new TString(),
747755
new TNonEmptyList(Type::getString()),
748756
]),
749757
'error' => new Union([
750-
new TInt(),
758+
new TIntRange(0, 8),
751759
new TNonEmptyList(Type::getInt()),
752760
]),
753761
];
@@ -761,7 +769,14 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
761769

762770
$type = new TKeyedArray($values);
763771

764-
return new Union([$type]);
772+
// $_FILES['userfile']['...'] case
773+
$named_type = new TArray([Type::getNonEmptyString(), new Union([$type])]);
774+
775+
// by default $_FILES is an empty array
776+
$default_type = new TArray([Type::getNever(), Type::getNever()]);
777+
778+
// ideally we would have 4 separate arrays with distinct types, but that isn't possible with psalm atm
779+
return TypeCombiner::combine([$default_type, $type, $named_type]);
765780
}
766781

767782
if ($var_id === '$_SESSION') {

0 commit comments

Comments
 (0)