Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More specific superglobals feedback update #8561

Merged
merged 7 commits into from
Oct 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record

  • Phpunit cast 'false' and 'true' to a boolean.
  • Symfony support using APP_DEBUG=0 or APP_DEBUG=1 in the .env file.
    So the best way to represent this is bool|string and force people to cast to bool.

'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