Skip to content

Commit 35b4cac

Browse files
staabmondrejmirtes
authored andcommitted
max()/min() should expect non-empty-array
1 parent 06b746d commit 35b4cac

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

resources/functionMap_bleedingEdge.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
return [
44
'new' => [
5-
5+
'max' => ['', '...arg1'=>'non-empty-array'],
6+
'min' => ['', '...arg1'=>'non-empty-array'],
67
],
78
'old' => [
89

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,12 @@ public function dataFileAsserts(): iterable
11901190
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
11911191
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8635.php');
11921192
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8625.php');
1193+
if (PHP_VERSION_ID >= 80000) {
1194+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7239-php8.php');
1195+
}
1196+
if (PHP_VERSION_ID < 80000) {
1197+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7239.php');
1198+
}
11931199
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8621.php');
11941200
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8084.php');
11951201
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3019.php');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7239php8;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param string[] $strings
11+
*/
12+
public function sayHello(array $arr, $strings): void
13+
{
14+
assertType('*ERROR*', max([]));
15+
assertType('*ERROR*', min([]));
16+
17+
if (count($arr) > 0) {
18+
assertType('mixed', max($arr));
19+
assertType('mixed', min($arr));
20+
} else {
21+
assertType('*ERROR*', max($arr));
22+
assertType('*ERROR*', min($arr));
23+
}
24+
25+
assertType('array', max([], $arr));
26+
assertType('array', min([], $arr));
27+
28+
if (count($strings) > 0) {
29+
assertType('string', max($strings));
30+
assertType('string', min($strings));
31+
} else {
32+
assertType('*ERROR*', max($strings));
33+
assertType('*ERROR*', min($strings));
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7239;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/**
10+
* @param string[] $strings
11+
*/
12+
public function sayHello(array $arr, $strings): void
13+
{
14+
assertType('false', max([]));
15+
assertType('false', min([]));
16+
17+
if (count($arr) > 0) {
18+
assertType('mixed', max($arr));
19+
assertType('mixed', min($arr));
20+
} else {
21+
assertType('false', max($arr));
22+
assertType('false', min($arr));
23+
}
24+
25+
assertType('array', max([], $arr));
26+
assertType('array', min([], $arr));
27+
28+
if (count($strings) > 0) {
29+
assertType('string', max($strings));
30+
assertType('string', min($strings));
31+
} else {
32+
assertType('false', max($strings));
33+
assertType('false', min($strings));
34+
}
35+
}
36+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,4 +1258,41 @@ public function testBug5986(): void
12581258
]);
12591259
}
12601260

1261+
public function testBug7239(): void
1262+
{
1263+
$tipText = 'array{} is empty.';
1264+
$this->analyse([__DIR__ . '/../../Analyser/data/bug-7239.php'], [
1265+
[
1266+
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
1267+
14,
1268+
$tipText,
1269+
],
1270+
[
1271+
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
1272+
15,
1273+
$tipText,
1274+
],
1275+
[
1276+
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
1277+
21,
1278+
$tipText,
1279+
],
1280+
[
1281+
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
1282+
22,
1283+
$tipText,
1284+
],
1285+
[
1286+
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
1287+
32,
1288+
$tipText,
1289+
],
1290+
[
1291+
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
1292+
33,
1293+
$tipText,
1294+
],
1295+
]);
1296+
}
1297+
12611298
}

0 commit comments

Comments
 (0)