Skip to content

Commit

Permalink
make superglobals more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Sep 11, 2022
1 parent 5108834 commit f83a8d0
Showing 1 changed file with 117 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\TArray;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TList;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TNonEmptyArray;
use Psalm\Type\Atomic\TNonEmptyList;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TIntRange;
use Psalm\Type\Atomic\TString;
use Psalm\Type\TaintKindGroup;
use Psalm\Type\Union;

Expand Down Expand Up @@ -531,26 +540,127 @@ public static function getGlobalType(string $var_id): Union
}

if ($var_id === '$argv') {
// only in CLI, null otherwise
return new Union([
new TArray([Type::getInt(), Type::getString()]),
new TNonEmptyList(Type::getString()),
new TNull()
]);
}

if ($var_id === '$argc') {
return Type::getInt();
// only in CLI, null otherwise
return new Union([
new TIntRange(1, null),
new TNull()
]);
}

if (!self::isSuperGlobal($var_id)) {
return Type::getMixed();
}

if ($var_id === '$http_response_header') {
return new Union([
new TList(Type::getString())
new TList(Type::getNonEmptyString())
]);
}

if (self::isSuperGlobal($var_id)) {
$type = Type::getArray();
if ($var_id === '$_SESSION') {
$type->possibly_undefined = true;
if ($var_id === '$GLOBALS') {
return new Union([
new TNonEmptyArray([
Type::getNonEmptyString(),
Type::getMixed()
])
]);
}

if ($var_id === '$_COOKIE') {
$type = new TArray(
[
Type::getNonEmptyString(),
Type::getString(),
]
);

return new Union([$type]);
}

if (in_array($var_id, array('$_GET', '$_POST', '$_REQUEST'), true)) {
$array_key = new Union([new TNonEmptyString(), new TInt()]);
$array = new TNonEmptyArray(
[
$array_key,
new Union([
new TString(),
new TArray([
$array_key,
Type::getMixed()
])
])
]
);

$type = new TArray(
[
$array_key,
new Union([new TString(), $array]),
]
);

return new Union([$type]);
}

if ($var_id === '$_SERVER' || $var_id === '$_ENV') {
$type = new TArray(
[
Type::getNonEmptyString(),
new Union([new TFloat(), new TIntRange(1, null), new TString(), new TNonEmptyList(Type::getNonEmptyString())]),
]
);

return new Union([$type]);
}

if ($var_id === '$_FILES') {
$values = [
'name' => new Union([
new TString(),
new TNonEmptyList(Type::getString()),
]),
'type' => new Union([
new TString(),
new TNonEmptyList(Type::getString()),
]),
'size' => new Union([
new TInt(),
new TNonEmptyList(Type::getInt()),
]),
'tmp_name' => new Union([
new TString(),
new TNonEmptyList(Type::getString()),
]),
'error' => new Union([
new TInt(),
new TNonEmptyList(Type::getInt()),
]),
];

$php_version = $config->getPhpVersionFromConfig();
if (!is_null($php_version) && version_compare($php_version, '8.1', '>=')) {
$values['full_path'] = new Union([
new TString(),
new TNonEmptyList(Type::getString()),
]);
}

$type = new TKeyedArray($values);

return new Union([$type]);
}

if ($var_id === '$_SESSION') {
$type = Type::getArray();
$type->possibly_undefined = true;
return $type;
}

Expand Down

0 comments on commit f83a8d0

Please sign in to comment.