Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Sep 11, 2022
1 parent f83a8d0 commit c2b2144
Show file tree
Hide file tree
Showing 26 changed files with 62 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArgument.md
Expand Up @@ -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']);
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayAccess.md
Expand Up @@ -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];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayAssignment.md
Expand Up @@ -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";
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedArrayOffset.md
Expand Up @@ -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']];
```
8 changes: 4 additions & 4 deletions docs/running_psalm/issues/MixedAssignment.md
Expand Up @@ -6,7 +6,7 @@ cannot infer a type more specific than `mixed`.
```php
<?php

$a = $_GET['foo'];
$a = $GLOBALS['foo'];
```

## How to fix
Expand All @@ -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));
```

Expand All @@ -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
Expand All @@ -34,5 +34,5 @@ or by adding a docblock
<?php

/** @var string */
$a = $_GET['foo'];
$a = $GLOBALS['foo'];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedClone.md
Expand Up @@ -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"];
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedFunctionCall.md
Expand Up @@ -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();
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedInferredReturnType.md
Expand Up @@ -6,6 +6,6 @@ Emitted when Psalm cannot determine a function's return type
<?php

function foo() : int {
return $_GET['foo'];
return $GLOBALS['foo'];
}
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedOperand.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedReturnStatement.md
Expand Up @@ -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
}
```
2 changes: 1 addition & 1 deletion docs/running_psalm/issues/MixedStringOffsetAssignment.md
Expand Up @@ -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'];
```
2 changes: 1 addition & 1 deletion tests/ArrayAssignmentTest.php
Expand Up @@ -724,7 +724,7 @@ public function offsetGet($offset) {
'mixedSwallowsArrayAssignment' => [
'<?php
/** @psalm-suppress MixedAssignment */
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
/** @psalm-suppress MixedArrayAssignment */
$a["bar"] = "cool";
Expand Down
4 changes: 2 additions & 2 deletions tests/ArrayFunctionCallTest.php
Expand Up @@ -1512,7 +1512,7 @@ public static function multiplyStatic(array $arr): int {
$direct_closure_result = array_reduce(
$arr,
function (int $carry, int $item) {
return $_GET["boo"];
return $GLOBALS["boo"];
},
1
);',
Expand Down Expand Up @@ -2212,7 +2212,7 @@ public function providerInvalidCodeParse(): iterable
$e = array_filter(
["a" => 5, "b" => 12, "c" => null],
function(?int $i) {
return $_GET["a"];
return $GLOBALS["a"];
}
);',
'error_message' => 'MixedArgumentTypeCoercion',
Expand Down
2 changes: 1 addition & 1 deletion tests/AssertAnnotationTest.php
Expand Up @@ -513,7 +513,7 @@ function assertIntOrFoo($b) : void {
}
/** @psalm-suppress MixedAssignment */
$a = $_GET["a"];
$a = $GLOBALS["a"];
assertIntOrFoo($a);
Expand Down
12 changes: 6 additions & 6 deletions tests/FileUpdates/TemporaryUpdateTest.php
Expand Up @@ -217,7 +217,7 @@ public function foo() {
}
public function bar() {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand All @@ -232,7 +232,7 @@ public function foo() : int {
}
public function bar() {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand All @@ -247,7 +247,7 @@ public function foo() : int {
}
public function bar() : int {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand All @@ -268,7 +268,7 @@ public function foo() : int {
}
public function bar() : int {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand All @@ -285,7 +285,7 @@ public function foo() : int {
}
public function bar() : int {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand All @@ -303,7 +303,7 @@ public function foo() : int {
}
public function bar() : int {
$a = $_GET["foo"];
$a = $GLOBALS["foo"];
return $this->foo();
}
}',
Expand Down
6 changes: 3 additions & 3 deletions tests/FunctionCallTest.php
Expand Up @@ -128,7 +128,7 @@ function foo() { }
'noRedundantConditionAfterMixedOrEmptyArrayCountCheck' => [
'<?php
function foo(string $s) : void {
$a = $_GET["s"] ?: [];
$a = $GLOBALS["s"] ?: [];
if (count($a)) {}
if (!count($a)) {}
}',
Expand Down Expand Up @@ -1037,7 +1037,7 @@ class Props {
/** @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',
Expand Down Expand Up @@ -1481,7 +1481,7 @@ function test() : void {
$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',
Expand Down
2 changes: 1 addition & 1 deletion tests/Internal/CliUtilsTest.php
Expand Up @@ -19,7 +19,7 @@ class CliUtilsTest extends TestCase
protected function setUp(): void
{
global $argv;
$this->argv = $argv;
$this->argv = $argv ?? [];
}

protected function tearDown(): void
Expand Down
8 changes: 4 additions & 4 deletions tests/JsonOutputTest.php
Expand Up @@ -123,11 +123,11 @@ function fooFoo() {
'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)',
],
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/LanguageServer/SymbolLookupTest.php
Expand Up @@ -113,7 +113,7 @@ function qux(int $a, int $b) : int {

$information = $codebase->getSymbolInformation('somefile.php', '$_SERVER');
$this->assertNotNull($information);
$this->assertSame("<?php array<array-key, mixed>", $information['type']);
$this->assertSame("<?php array<string, float|int<1, max>|non-empty-list<string>|string>", $information['type']);

$information = $codebase->getSymbolInformation('somefile.php', '$my_global');
$this->assertNotNull($information);
Expand Down
2 changes: 1 addition & 1 deletion tests/ReturnTypeTest.php
Expand Up @@ -1213,7 +1213,7 @@ function fooFoo(): A {
* @psalm-suppress UndefinedClass
*/
function fooFoo(): A {
return $_GET["a"];
return $GLOBALS["a"];
}
fooFoo()->bar();',
Expand Down
19 changes: 10 additions & 9 deletions tests/TaintTest.php
Expand Up @@ -458,13 +458,6 @@ public static function slugify(string $url) : string {
echo $a[0]["b"];',
],
'intUntainted' => [
'<?php
$input = $_GET[\'input\'];
if (is_int($input)) {
echo "$input";
}',
],
'dontTaintSpecializedInstanceProperty' => [
'<?php
/** @psalm-taint-specialize */
Expand Down Expand Up @@ -672,7 +665,7 @@ function takesArray(array $arr): void {
],
'resultOfPlusIsNotTainted' => [
'<?php
$input = $_GET["foo"];
$input = is_numeric( $_GET["foo"] ) ? $_GET["foo"] : "";
$var = $input + 1;
var_dump($var);'
],
Expand Down Expand Up @@ -1611,7 +1604,15 @@ function identity(string $s) : string {
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' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/ClassTemplateTest.php
Expand Up @@ -1425,7 +1425,7 @@ public function __construct(array $elements = [])
}
/** @psalm-suppress MixedArgument */
$c = new ArrayCollection($_GET["a"]);',
$c = new ArrayCollection($GLOBALS["a"]);',
[
'$c' => 'ArrayCollection<array-key, mixed>',
],
Expand Down
2 changes: 1 addition & 1 deletion tests/Template/ConditionalReturnTypeTest.php
Expand Up @@ -40,7 +40,7 @@ public function getAttribute(?string $name, string $default = "")
$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>',
Expand Down
2 changes: 1 addition & 1 deletion tests/TypeReconciliation/EmptyTest.php
Expand Up @@ -200,7 +200,7 @@ function foo(int $t) : void {
'<?php
function foo($t) : void {
if (empty($t)) {
foreach ($_GET["u"] as $a) {
foreach ($GLOBALS["u"] as $a) {
if (empty($t)) {
$t = $a;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/fixtures/DummyProjectWithErrors/src/FileWithErrors.php
Expand Up @@ -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 changes: 9 additions & 4 deletions tests/fixtures/expected_taint_graph.dot
@@ -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"
Expand All @@ -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"
}

0 comments on commit c2b2144

Please sign in to comment.