Skip to content

Commit 9216c82

Browse files
authored
Regression tests
1 parent 0502e9e commit 9216c82

File tree

9 files changed

+221
-0
lines changed

9 files changed

+221
-0
lines changed

tests/PHPStan/Rules/Comparison/IfConstantConditionRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,10 @@ public function testBug5370(): void
123123
$this->analyse([__DIR__ . '/data/bug-5370.php'], []);
124124
}
125125

126+
public function testBug6902(): void
127+
{
128+
$this->treatPhpDocTypesAsCertain = true;
129+
$this->analyse([__DIR__ . '/data/bug-6902.php'], []);
130+
}
131+
126132
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug6902;
4+
5+
function doFoo()
6+
{
7+
/** @var array * */
8+
$array1 = ['a' => 1, 'b' => 2];
9+
/** @var array * */
10+
$array2 = ['a' => 1];
11+
12+
$check = function (string $key) use (&$array1, &$array2): bool {
13+
if (!isset($array1[$key], $array2[$key])) {
14+
return false;
15+
}
16+
// ... more conditions here ...
17+
return true;
18+
};
19+
20+
if ($check('a')) {
21+
// ...
22+
}
23+
}

tests/PHPStan/Rules/Functions/ClosureReturnTypeRuleTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,34 @@ public function testBug6806(): void
9898
$this->analyse([__DIR__ . '/data/bug-6806.php'], []);
9999
}
100100

101+
public function testBug4739(): void
102+
{
103+
$this->analyse([__DIR__ . '/data/bug-4739.php'], []);
104+
}
105+
106+
public function testBug4739b(): void
107+
{
108+
$this->analyse([__DIR__ . '/data/bug-4739b.php'], []);
109+
}
110+
111+
public function testBug5753(): void
112+
{
113+
$this->analyse([__DIR__ . '/data/bug-5753.php'], []);
114+
}
115+
116+
public function testBug6559(): void
117+
{
118+
$this->analyse([__DIR__ . '/data/bug-6559.php'], []);
119+
}
120+
121+
public function testBug6902(): void
122+
{
123+
$this->analyse([__DIR__ . '/data/bug-6902.php'], []);
124+
}
125+
126+
public function testBug7220(): void
127+
{
128+
$this->analyse([__DIR__ . '/data/bug-7220.php'], []);
129+
}
130+
101131
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug4739;
4+
5+
class HelloWorld
6+
{
7+
public function check(): bool
8+
{
9+
$counter = [];
10+
11+
return (static function () use (&$counter): bool {
12+
if (! isset($counter['key'])) {
13+
$counter['key'] = 0;
14+
}
15+
16+
return ++$counter['key'] === 2;
17+
})();
18+
}
19+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug4739b;
4+
5+
function filter(callable $predicate, iterable $iterable): \Iterator {
6+
foreach ($iterable as $key => $value) {
7+
if ($predicate($value)) {
8+
yield $key => $value;
9+
}
10+
}
11+
}
12+
13+
14+
class Record {
15+
/**
16+
* @var boolean
17+
*/
18+
public $isInactive;
19+
/**
20+
* @var string
21+
*/
22+
public $name;
23+
}
24+
25+
function doFoo() {
26+
$emails = [];
27+
$records = [];
28+
filter(
29+
function (Record $domain) use (&$emails): bool {
30+
if (!isset($emails[$domain->name])) {
31+
$emails[$domain->name] = TRUE;
32+
return TRUE;
33+
}
34+
return !$domain->isInactive;
35+
},
36+
$records
37+
);
38+
$test = (bool) mt_rand(0, 1);
39+
filter(
40+
function (bool $arg) use (&$emails): bool {
41+
if (empty($emails)) {
42+
return TRUE;
43+
}
44+
return $arg;
45+
},
46+
$records
47+
);
48+
filter(
49+
function (bool $arg) use ($emails): bool {
50+
if (empty($emails)) {
51+
return TRUE;
52+
}
53+
return $arg;
54+
},
55+
$records
56+
);
57+
$test = (bool) mt_rand(0, 1);
58+
filter(
59+
function (bool $arg) use (&$test): bool {
60+
if ($test) {
61+
return TRUE;
62+
}
63+
return $arg;
64+
},
65+
$records
66+
);
67+
filter(
68+
function (bool $arg) use ($test): bool {
69+
if ($test) {
70+
return TRUE;
71+
}
72+
return $arg;
73+
},
74+
$records
75+
);
76+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Bug5753;
4+
5+
function doFoo() {
6+
$arr = [];
7+
$f = function (string $str) use (&$arr) {
8+
$arr[] = $str;
9+
return $arr;
10+
};
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Bug6559;
4+
5+
function doFoo() {
6+
$array = ['a' => true];
7+
8+
$find = function(string $key) use (&$array) {
9+
return $array[$key] ?? null;
10+
};
11+
12+
$find('a') ?? false;
13+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug6902;
4+
5+
function doFoo() {
6+
7+
/** @var array **/
8+
$array1 = ['a' => 1, 'b' => 2];
9+
/** @var array **/
10+
$array2 = ['a' => 1];
11+
12+
$check = function(string $key) use (&$array1, &$array2): bool {
13+
if (!isset($array1[$key], $array2[$key])) {
14+
return false;
15+
}
16+
// ... more conditions here ...
17+
return true;
18+
};
19+
20+
if ($check('a')) {
21+
// ...
22+
}
23+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Bug7220;
4+
5+
function filter(callable $predicate, iterable $iterable): \Iterator {
6+
foreach ($iterable as $key => $value) {
7+
if ($predicate($value)) {
8+
yield $key => $value;
9+
}
10+
}
11+
}
12+
13+
function getFiltered(): \Iterator {
14+
$already_seen = [];
15+
return filter(function (string $value) use (&$already_seen): bool {
16+
$result = !isset($already_seen[$value]);
17+
$already_seen[$value] = TRUE;
18+
return $result;
19+
}, ['a', 'b', 'a']);
20+
}

0 commit comments

Comments
 (0)