1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #957 - allow assertions on $_GET, $_POST, $_SERVER etc

This commit is contained in:
Matt Brown 2018-08-24 16:48:14 -04:00
parent 4ef2aafdd8
commit caf65ca8a9
7 changed files with 33 additions and 4 deletions

View File

@ -183,7 +183,7 @@ class IfChecker
$if_context->inside_conditional = false;
$mixed_var_ids = ['$_GET', '$_POST', '$_SERVER'];
$mixed_var_ids = [];
foreach ($if_context->vars_in_scope as $var_id => $type) {
if ($type->isMixed()) {
@ -890,7 +890,7 @@ class IfChecker
$elseif_context->inside_conditional = false;
$mixed_var_ids = ['$_GET', '$_POST', '$_SERVER'];
$mixed_var_ids = [];
foreach ($elseif_context->vars_in_scope as $var_id => $type) {
if ($type->isMixed()) {

View File

@ -288,7 +288,7 @@ class BinaryOpChecker
$statements_checker
);
$mixed_var_ids = ['$_GET', '$_POST', '$_SERVER'];
$mixed_var_ids = [];
foreach ($context->vars_in_scope as $var_id => $type) {
if ($type->isMixed()) {

View File

@ -107,6 +107,8 @@ class VariableFetchChecker
)
) {
$stmt->inferredType = Type::getArray();
$context->vars_in_scope['$' . $stmt->name] = Type::getArray();
$context->vars_possibly_in_scope['$' . $stmt->name] = true;
return null;
}

View File

@ -45,7 +45,7 @@ class TernaryChecker
$statements_checker
);
$mixed_var_ids = ['$_GET', '$_POST', '$_SERVER'];
$mixed_var_ids = [];
foreach ($context->vars_in_scope as $var_id => $type) {
if ($type->isMixed()) {

View File

@ -189,6 +189,21 @@ class AssertTest extends TestCase
echo "Ma chaine " . $myString;
}'
],
'assertServerVar' => [
'<?php
/**
* @psalm-assert-if-true string $a
* @param mixed $a
*/
function my_is_string($a) : bool
{
return is_string($a);
}
if (my_is_string($_SERVER["abc"])) {
$i = substr($_SERVER["abc"], 1, 2);
}',
],
];
}

View File

@ -341,6 +341,12 @@ class IssetTest extends TestCase
'assertions' => [],
'error_levels' => ['MixedAssignment', 'MixedArrayAccess'],
],
'mixedArrayIssetGetStringVar' => [
'<?php
if (isset($_GET["b"]) && is_string($_GET["b"])) {
echo $_GET["b"];
}',
],
'nestedArrayAccessInLoopAfterIsset' => [
'<?php
$arr = [];

View File

@ -1013,6 +1013,12 @@ class TypeReconciliationTest extends TestCase
}
}',
],
'isStringServerVar' => [
'<?php
if (is_string($_SERVER["abc"])) {
echo substr($_SERVER["abc"], 1, 2);
}',
],
];
}