Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 31, 2022
1 parent edc1e71 commit c17a331
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -665,6 +665,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/template-null-bound.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4592.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4903.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2420.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2718.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3126.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4586.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4887.php');
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2420.php
@@ -0,0 +1,33 @@
<?php

namespace Bug2420;

use function PHPStan\Testing\assertType;

class HelloWorld
{
const CONFIG = [
0 => false,
1 => false,
];

public function sayHello(int $key): void
{
$config = self::CONFIG[$key] ?? true;
assertType('bool', $config);
}
}

class HelloWorld2
{
const CONFIG = [
0 => ['foo' => false],
1 => ['foo' => false],
];

public function sayHello(int $key): void
{
$config = self::CONFIG[$key]['foo'] ?? true;
assertType('bool', $config);
}
}
41 changes: 41 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2718.php
@@ -0,0 +1,41 @@
<?php

namespace Bug2218;

use function PHPStan\Testing\assertType;

class Foo
{

public function doFoo()
{
$fun = function (): array {
return [
['group_id' => 'a', 'user_id' => 'id1'],
['group_id' => 'a', 'user_id' => 'id2'],
['group_id' => 'a', 'user_id' => 'id3'],
['group_id' => 'b', 'user_id' => 'id4'],
['group_id' => 'b', 'user_id' => 'id5'],
['group_id' => 'b', 'user_id' => 'id6'],
];
};

$orders = $fun();

$result = [];
foreach ($orders as $order) {
assertType('bool', isset($result[$order['group_id']]['users']));
if (isset($result[$order['group_id']]['users'])) {
$result[$order['group_id']]['users'][] = $order['user_id'];
continue;
}

$result[$order['group_id']] = [
'users' => [
$order['user_id'],
],
];
}
}

}
37 changes: 37 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3126.php
@@ -0,0 +1,37 @@
<?php

namespace Bug3126;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param array<int,string> $input
*/
function failure(array $input): void {
$results = [];

foreach ($input as $keyOne => $layerOne) {
assertType('bool', isset($results[$keyOne]['name']));
if(isset($results[$keyOne]['name']) === false) {
$results[$keyOne]['name'] = $layerOne;
}
}
}

/**
* @param array<int,string> $input
*/
function no_failure(array $input): void {
$results = [];

foreach ($input as $keyOne => $layerOne) {
if(isset($results[$keyOne]) === false) {
$results[$keyOne] = $layerOne;
}
}
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4586.php
@@ -0,0 +1,13 @@
<?php

namespace Bug4586;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function sayHello(): void
{
assertType('bool', isset($_SESSION));
}
}
18 changes: 18 additions & 0 deletions tests/PHPStan/Analyser/data/bug-4887.php
@@ -0,0 +1,18 @@
<?php

namespace Bug4887;

use function PHPStan\Testing\assertType;

assertType('bool', isset($_SESSION));

$foo = ['$_REQUEST' => $_REQUEST,
'$_COOKIE' => $_COOKIE,
'$_SERVER' => $_SERVER,
'$GLOBALS' => $GLOBALS,
'$SESSION' => isset($_SESSION) ? $_SESSION : NULL];

foreach ($foo as $data)
{
assertType('bool', is_array($data));
}

0 comments on commit c17a331

Please sign in to comment.