mirror of
https://github.com/danog/psalm.git
synced 2024-11-26 20:34:47 +01:00
fix tests
This commit is contained in:
parent
7bc29a91eb
commit
5c39e66b15
@ -6,5 +6,5 @@ Emitted when Psalm cannot determine the type of an argument
|
||||
<?php
|
||||
|
||||
function takesInt(int $i) : void {}
|
||||
takesInt($_GET['foo']);
|
||||
takesInt($GLOBALS['foo']);
|
||||
```
|
||||
|
@ -5,5 +5,5 @@ Emitted when trying to access an array offset on a value whose type Psalm cannot
|
||||
```php
|
||||
<?php
|
||||
|
||||
echo $_GET['foo'][0];
|
||||
echo $GLOBALS['foo'][0];
|
||||
```
|
||||
|
@ -5,5 +5,5 @@ Emitted when trying to assign a value to an array offset on a value whose type P
|
||||
```php
|
||||
<?php
|
||||
|
||||
$_GET['foo'][0] = "5";
|
||||
$GLOBALS['foo'][0] = "5";
|
||||
```
|
||||
|
@ -5,5 +5,5 @@ Emitted when attempting to access an array offset where Psalm cannot determine t
|
||||
```php
|
||||
<?php
|
||||
|
||||
echo [1, 2, 3][$_GET['foo']];
|
||||
echo [1, 2, 3][$GLOBALS['foo']];
|
||||
```
|
||||
|
@ -6,7 +6,7 @@ cannot infer a type more specific than `mixed`.
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = $_GET['foo'];
|
||||
$a = $GLOBALS['foo'];
|
||||
```
|
||||
|
||||
## How to fix
|
||||
@ -16,7 +16,7 @@ The above example can be fixed in a few ways – by adding an `assert` call:
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = $_GET['foo'];
|
||||
$a = $GLOBALS['foo'];
|
||||
assert(is_string($a));
|
||||
```
|
||||
|
||||
@ -25,7 +25,7 @@ or by adding an explicit cast:
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = (string) $_GET['foo'];
|
||||
$a = (string) $GLOBALS['foo'];
|
||||
```
|
||||
|
||||
or by adding a docblock
|
||||
@ -34,5 +34,5 @@ or by adding a docblock
|
||||
<?php
|
||||
|
||||
/** @var string */
|
||||
$a = $_GET['foo'];
|
||||
$a = $GLOBALS['foo'];
|
||||
```
|
||||
|
@ -5,5 +5,5 @@ Emitted when trying to clone a value whose type is not known
|
||||
```php
|
||||
<?php
|
||||
|
||||
$a = clone $_GET["a"];
|
||||
$a = clone $GLOBALS["a"];
|
||||
```
|
||||
|
@ -6,6 +6,6 @@ Emitted when calling a function on a value whose type Psalm cannot infer.
|
||||
<?php
|
||||
|
||||
/** @var mixed */
|
||||
$a = $_GET['foo'];
|
||||
$a = $GLOBALS['foo'];
|
||||
$a();
|
||||
```
|
||||
|
@ -6,6 +6,6 @@ Emitted when Psalm cannot determine a function's return type
|
||||
<?php
|
||||
|
||||
function foo() : int {
|
||||
return $_GET['foo'];
|
||||
return $GLOBALS['foo'];
|
||||
}
|
||||
```
|
||||
|
@ -5,7 +5,7 @@ Emitted when Psalm cannot infer a type for an operand in any calculated expressi
|
||||
```php
|
||||
<?php
|
||||
|
||||
echo $_GET['foo'] + "hello";
|
||||
echo $GLOBALS['foo'] + "hello";
|
||||
```
|
||||
|
||||
## Why it’s bad
|
||||
|
@ -6,6 +6,6 @@ Emitted when Psalm cannot determine the type of a given return statement
|
||||
<?php
|
||||
|
||||
function foo() : int {
|
||||
return $_GET['foo']; // emitted here
|
||||
return $GLOBALS['foo']; // emitted here
|
||||
}
|
||||
```
|
||||
|
@ -5,5 +5,5 @@ Emitted when assigning a value on a string using a value for which Psalm cannot
|
||||
```php
|
||||
<?php
|
||||
|
||||
"hello"[0] = $_GET['foo'];
|
||||
"hello"[0] = $GLOBALS['foo'];
|
||||
```
|
||||
|
@ -724,7 +724,7 @@ class ArrayAssignmentTest extends TestCase
|
||||
'mixedSwallowsArrayAssignment' => [
|
||||
'<?php
|
||||
/** @psalm-suppress MixedAssignment */
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
|
||||
/** @psalm-suppress MixedArrayAssignment */
|
||||
$a["bar"] = "cool";
|
||||
|
@ -1512,7 +1512,7 @@ class ArrayFunctionCallTest extends TestCase
|
||||
$direct_closure_result = array_reduce(
|
||||
$arr,
|
||||
function (int $carry, int $item) {
|
||||
return $_GET["boo"];
|
||||
return $GLOBALS["boo"];
|
||||
},
|
||||
1
|
||||
);',
|
||||
@ -2212,7 +2212,7 @@ class ArrayFunctionCallTest extends TestCase
|
||||
$e = array_filter(
|
||||
["a" => 5, "b" => 12, "c" => null],
|
||||
function(?int $i) {
|
||||
return $_GET["a"];
|
||||
return $GLOBALS["a"];
|
||||
}
|
||||
);',
|
||||
'error_message' => 'MixedArgumentTypeCoercion',
|
||||
|
@ -513,7 +513,7 @@ class AssertAnnotationTest extends TestCase
|
||||
}
|
||||
|
||||
/** @psalm-suppress MixedAssignment */
|
||||
$a = $_GET["a"];
|
||||
$a = $GLOBALS["a"];
|
||||
|
||||
assertIntOrFoo($a);
|
||||
|
||||
|
@ -217,7 +217,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
@ -232,7 +232,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
@ -247,7 +247,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() : int {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
@ -268,7 +268,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() : int {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
@ -285,7 +285,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() : int {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
@ -303,7 +303,7 @@ class TemporaryUpdateTest extends TestCase
|
||||
}
|
||||
|
||||
public function bar() : int {
|
||||
$a = $_GET["foo"];
|
||||
$a = $GLOBALS["foo"];
|
||||
return $this->foo();
|
||||
}
|
||||
}',
|
||||
|
@ -128,7 +128,7 @@ class FunctionCallTest extends TestCase
|
||||
'noRedundantConditionAfterMixedOrEmptyArrayCountCheck' => [
|
||||
'<?php
|
||||
function foo(string $s) : void {
|
||||
$a = $_GET["s"] ?: [];
|
||||
$a = $GLOBALS["s"] ?: [];
|
||||
if (count($a)) {}
|
||||
if (!count($a)) {}
|
||||
}',
|
||||
@ -1037,7 +1037,7 @@ class FunctionCallTest extends TestCase
|
||||
/** @psalm-suppress InvalidScalarArgument */
|
||||
$a = mktime("foo");
|
||||
/** @psalm-suppress MixedArgument */
|
||||
$b = mktime($_GET["foo"]);
|
||||
$b = mktime($GLOBALS["foo"]);
|
||||
$c = mktime(1, 2, 3);',
|
||||
'assertions' => [
|
||||
'$a' => 'false|int',
|
||||
@ -1481,7 +1481,7 @@ class FunctionCallTest extends TestCase
|
||||
$y2 = date("Y", 10000);
|
||||
$F2 = date("F", 10000);
|
||||
/** @psalm-suppress MixedArgument */
|
||||
$F3 = date("F", $_GET["F3"]);',
|
||||
$F3 = date("F", $GLOBALS["F3"]);',
|
||||
[
|
||||
'$y' => 'numeric-string',
|
||||
'$m' => 'numeric-string',
|
||||
|
@ -19,7 +19,7 @@ class CliUtilsTest extends TestCase
|
||||
protected function setUp(): void
|
||||
{
|
||||
global $argv;
|
||||
$this->argv = $argv;
|
||||
$this->argv = $argv ?? [];
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
@ -123,11 +123,11 @@ class JsonOutputTest extends TestCase
|
||||
'assertCancelsMixedAssignment' => [
|
||||
'<?php
|
||||
$a = $_GET["hello"];
|
||||
assert(is_int($a));
|
||||
if (is_int($a)) {}',
|
||||
'message' => 'Docblock-defined type int for $a is always int',
|
||||
assert(is_string($a));
|
||||
if (is_string($a)) {}',
|
||||
'message' => 'Docblock-defined type string for $a is always string',
|
||||
'line' => 4,
|
||||
'error' => 'is_int($a)',
|
||||
'error' => 'is_string($a)',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ class SymbolLookupTest extends TestCase
|
||||
return $a + $b;
|
||||
}
|
||||
|
||||
$_SERVER;'
|
||||
$_SESSION;'
|
||||
);
|
||||
|
||||
new FileAnalyzer($this->project_analyzer, 'somefile.php', 'somefile.php');
|
||||
@ -111,9 +111,9 @@ class SymbolLookupTest extends TestCase
|
||||
$this->assertNotNull($information);
|
||||
$this->assertSame("<?php function B\qux(\n int \$a,\n int \$b\n) : int", $information['type']);
|
||||
|
||||
$information = $codebase->getSymbolInformation('somefile.php', '$_SERVER');
|
||||
$information = $codebase->getSymbolInformation('somefile.php', '$_SESSION');
|
||||
$this->assertNotNull($information);
|
||||
$this->assertSame("<?php array<array-key, mixed>", $information['type']);
|
||||
$this->assertSame("<?php array<string, mixed>", $information['type']);
|
||||
|
||||
$information = $codebase->getSymbolInformation('somefile.php', '$my_global');
|
||||
$this->assertNotNull($information);
|
||||
|
@ -1213,7 +1213,7 @@ class ReturnTypeTest extends TestCase
|
||||
* @psalm-suppress UndefinedClass
|
||||
*/
|
||||
function fooFoo(): A {
|
||||
return $_GET["a"];
|
||||
return $GLOBALS["a"];
|
||||
}
|
||||
|
||||
fooFoo()->bar();',
|
||||
|
@ -458,13 +458,6 @@ class TaintTest extends TestCase
|
||||
|
||||
echo $a[0]["b"];',
|
||||
],
|
||||
'intUntainted' => [
|
||||
'<?php
|
||||
$input = $_GET[\'input\'];
|
||||
if (is_int($input)) {
|
||||
echo "$input";
|
||||
}',
|
||||
],
|
||||
'dontTaintSpecializedInstanceProperty' => [
|
||||
'<?php
|
||||
/** @psalm-taint-specialize */
|
||||
@ -672,7 +665,7 @@ class TaintTest extends TestCase
|
||||
],
|
||||
'resultOfPlusIsNotTainted' => [
|
||||
'<?php
|
||||
$input = $_GET["foo"];
|
||||
$input = is_numeric( $_GET["foo"] ) ? $_GET["foo"] : "";
|
||||
$var = $input + 1;
|
||||
var_dump($var);'
|
||||
],
|
||||
@ -1611,7 +1604,15 @@ class TaintTest extends TestCase
|
||||
function test(...$args) {
|
||||
echo $args[0];
|
||||
}
|
||||
test(...$_GET["other"]);',
|
||||
|
||||
/**
|
||||
* @psalm-taint-source input
|
||||
*/
|
||||
function getQueryParam() {}
|
||||
|
||||
// cannot use $_GET, see #8477
|
||||
$foo = getQueryParam();
|
||||
test(...$foo);',
|
||||
'error_message' => 'TaintedHtml',
|
||||
],
|
||||
'foreachArg' => [
|
||||
|
@ -1425,7 +1425,7 @@ class ClassTemplateTest extends TestCase
|
||||
}
|
||||
|
||||
/** @psalm-suppress MixedArgument */
|
||||
$c = new ArrayCollection($_GET["a"]);',
|
||||
$c = new ArrayCollection($GLOBALS["a"]);',
|
||||
[
|
||||
'$c' => 'ArrayCollection<array-key, mixed>',
|
||||
],
|
||||
|
@ -40,7 +40,7 @@ class ConditionalReturnTypeTest extends TestCase
|
||||
$a = (new A)->getAttribute("colour", "red"); // typed as string
|
||||
$b = (new A)->getAttribute(null); // typed as array<string, string>
|
||||
/** @psalm-suppress MixedArgument */
|
||||
$c = (new A)->getAttribute($_GET["foo"]); // typed as string|array<string, string>',
|
||||
$c = (new A)->getAttribute($GLOBALS["foo"]); // typed as string|array<string, string>',
|
||||
[
|
||||
'$a' => 'string',
|
||||
'$b' => 'array<string, string>',
|
||||
|
@ -200,7 +200,7 @@ class EmptyTest extends TestCase
|
||||
'<?php
|
||||
function foo($t) : void {
|
||||
if (empty($t)) {
|
||||
foreach ($_GET["u"] as $a) {
|
||||
foreach ($GLOBALS["u"] as $a) {
|
||||
if (empty($t)) {
|
||||
$t = $a;
|
||||
}
|
||||
|
@ -24,5 +24,10 @@ function bang(string $s) : string {
|
||||
|
||||
function boom(): void
|
||||
{
|
||||
echo (string) ($_GET['abc'] ?? 'z');
|
||||
echo (string) ($GLOBALS['abc'] ?? 'z');
|
||||
}
|
||||
|
||||
function booom(): void
|
||||
{
|
||||
echo isset($_GET['abc']) && is_string($_GET['abc']) ? $_GET['abc'] : 'z';
|
||||
}
|
||||
|
13
tests/fixtures/expected_taint_graph.dot
vendored
13
tests/fixtures/expected_taint_graph.dot
vendored
@ -1,6 +1,9 @@
|
||||
digraph Taints {
|
||||
"$_GET:src/FileWithErrors.php:345" -> "$_GET['abc']-src/FileWithErrors.php:345-349"
|
||||
"$_GET['abc']-src/FileWithErrors.php:345-349" -> "coalesce-src/FileWithErrors.php:345-363"
|
||||
"$_GET:src/FileWithErrors.php:413" -> "$_GET['abc']-src/FileWithErrors.php:413-417"
|
||||
"$_GET:src/FileWithErrors.php:440" -> "$_GET['abc']-src/FileWithErrors.php:440-444"
|
||||
"$_GET:src/FileWithErrors.php:456" -> "$_GET['abc']-src/FileWithErrors.php:456-460"
|
||||
"$_GET['abc']-src/FileWithErrors.php:440-444" -> "call to is_string-src/FileWithErrors.php:440-451"
|
||||
"$_GET['abc']-src/FileWithErrors.php:456-460" -> "call to echo-src/FileWithErrors.php:407-473"
|
||||
"$s-src/FileWithErrors.php:109-110" -> "variable-use" -> "acme\sampleproject\bar"
|
||||
"$s-src/FileWithErrors.php:162-163" -> "variable-use" -> "acme\sampleproject\baz"
|
||||
"$s-src/FileWithErrors.php:215-216" -> "variable-use" -> "acme\sampleproject\bat"
|
||||
@ -10,6 +13,8 @@ digraph Taints {
|
||||
"acme\sampleproject\bat#1" -> "$s-src/FileWithErrors.php:215-216"
|
||||
"acme\sampleproject\baz#1" -> "$s-src/FileWithErrors.php:162-163"
|
||||
"acme\sampleproject\foo#1" -> "$s-src/FileWithErrors.php:57-58"
|
||||
"call to echo-src/FileWithErrors.php:335-364" -> "echo#1-src/filewitherrors.php:330"
|
||||
"coalesce-src/FileWithErrors.php:345-363" -> "call to echo-src/FileWithErrors.php:335-364"
|
||||
"call to echo-src/FileWithErrors.php:335-367" -> "echo#1-src/filewitherrors.php:330"
|
||||
"call to echo-src/FileWithErrors.php:407-473" -> "echo#1-src/filewitherrors.php:402"
|
||||
"call to is_string-src/FileWithErrors.php:440-451" -> "is_string#1-src/filewitherrors.php:430"
|
||||
"coalesce-src/FileWithErrors.php:345-366" -> "call to echo-src/FileWithErrors.php:335-367"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user