Skip to content

Commit

Permalink
Merge pull request #8561 from kkmuffme/more-specific-superglobals-fee…
Browse files Browse the repository at this point in the history
…dback-update

More specific superglobals feedback update
  • Loading branch information
orklah authored Oct 10, 2022
2 parents b424de9 + 6012981 commit 99395bb
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Psalm\Internal\Codebase\TaintFlowGraph;
use Psalm\Internal\DataFlow\DataFlowNode;
use Psalm\Internal\DataFlow\TaintSource;
use Psalm\Internal\Type\TypeCombiner;
use Psalm\Issue\ImpureVariable;
use Psalm\Issue\InvalidScope;
use Psalm\Issue\PossiblyUndefinedGlobalVariable;
Expand All @@ -22,6 +23,7 @@
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TBool;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TKeyedArray;
Expand Down Expand Up @@ -645,6 +647,9 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
$request_time_float_helper = Type::getFloat();
$request_time_float_helper->possibly_undefined = true;

$bool_string_helper = new Union([new TBool(), new TString()]);
$bool_string_helper->possibly_undefined = true;

$detailed_type = new TKeyedArray([
// https://www.php.net/manual/en/reserved.variables.server.php
'PHP_SELF' => $non_empty_string_helper,
Expand Down Expand Up @@ -719,6 +724,9 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
'HTTP_SEC_CH_UA_PLATFORM' => $non_empty_string_helper,
'HTTP_SEC_CH_UA_MOBILE' => $non_empty_string_helper,
'HTTP_SEC_CH_UA' => $non_empty_string_helper,
// phpunit
'APP_DEBUG' => $bool_string_helper,
'APP_ENV' => $string_helper,
]);

// generic case for all other elements
Expand All @@ -739,15 +747,15 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_
new TNonEmptyList(Type::getString()),
]),
'size' => new Union([
new TInt(),
new TIntRange(0, null),
new TNonEmptyList(Type::getInt()),
]),
'tmp_name' => new Union([
new TString(),
new TNonEmptyList(Type::getString()),
]),
'error' => new Union([
new TInt(),
new TIntRange(0, 8),
new TNonEmptyList(Type::getInt()),
]),
];
Expand All @@ -761,7 +769,14 @@ public static function getGlobalType(string $var_id, int $codebase_analysis_php_

$type = new TKeyedArray($values);

return new Union([$type]);
// $_FILES['userfile']['...'] case
$named_type = new TArray([Type::getNonEmptyString(), new Union([$type])]);

// by default $_FILES is an empty array
$default_type = new TArray([Type::getNever(), Type::getNever()]);

// ideally we would have 4 separate arrays with distinct types, but that isn't possible with psalm atm
return TypeCombiner::combine([$default_type, $type, $named_type]);
}

if ($var_id === '$_SESSION') {
Expand Down

0 comments on commit 99395bb

Please sign in to comment.