Skip to content

Commit

Permalink
max()/min() should expect non-empty-array
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jan 4, 2023
1 parent 28c2c79 commit 3b1c15f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
4 changes: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6143,7 +6143,7 @@
'mapObj::zoomPoint' => ['int', 'nZoomFactor'=>'int', 'oPixelPos'=>'pointObj', 'nImageWidth'=>'int', 'nImageHeight'=>'int', 'oGeorefExt'=>'rectObj'],
'mapObj::zoomRectangle' => ['int', 'oPixelExt'=>'rectObj', 'nImageWidth'=>'int', 'nImageHeight'=>'int', 'oGeorefExt'=>'rectObj'],
'mapObj::zoomScale' => ['int', 'nScaleDenom'=>'float', 'oPixelPos'=>'pointObj', 'nImageWidth'=>'int', 'nImageHeight'=>'int', 'oGeorefExt'=>'rectObj', 'oMaxGeorefExt'=>'rectObj'],
'max' => ['', '...arg1'=>'array'],
'max' => ['', '...arg1'=>'non-empty-array'],
'max\'1' => ['', 'arg1'=>'', 'arg2'=>'', '...args='=>''],
'maxdb::__construct' => ['void', 'host='=>'string', 'username='=>'string', 'passwd='=>'string', 'dbname='=>'string', 'port='=>'int', 'socket='=>'string'],
'maxdb::affected_rows' => ['int', 'link'=>''],
Expand Down Expand Up @@ -6513,7 +6513,7 @@
'mhash_keygen_s2k' => ['string|false', 'hash'=>'int', 'input_password'=>'string', 'salt'=>'string', 'bytes'=>'int'],
'microtime' => ['mixed', 'get_as_float='=>'bool'],
'mime_content_type' => ['string|false', 'filename_or_stream'=>'string|resource'],
'min' => ['', '...arg1'=>'array'],
'min' => ['', '...arg1'=>'non-empty-array'],
'min\'1' => ['', 'arg1'=>'', 'arg2'=>'', '...args='=>''],
'ming_keypress' => ['int', 'char'=>'string'],
'ming_setcubicthreshold' => ['void', 'threshold'=>'int'],
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,12 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/DeadCode/data/bug-8620.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8635.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8625.php');
if (PHP_VERSION_ID >= 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7239-php8.php');
}
if (PHP_VERSION_ID < 80000) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7239.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8621.php');
}

Expand Down
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7239-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Bug7239php8;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param string[] $strings
*/
public function sayHello(array $arr, $strings): void
{
assertType('*ERROR*', max([]));
assertType('*ERROR*', min([]));

if (count($arr) > 0) {
assertType('mixed', max($arr));
assertType('mixed', min($arr));
} else {
assertType('*ERROR*', max($arr));
assertType('*ERROR*', min($arr));
}

assertType('array', max([], $arr));
assertType('array', min([], $arr));

if (count($strings) > 0) {
assertType('string', max($strings));
assertType('string', min($strings));
} else {
assertType('*ERROR*', max($strings));
assertType('*ERROR*', min($strings));
}
}
}
36 changes: 36 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7239.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types = 1);

namespace Bug7239;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/**
* @param string[] $strings
*/
public function sayHello(array $arr, $strings): void
{
assertType('false', max([]));
assertType('false', min([]));

if (count($arr) > 0) {
assertType('mixed', max($arr));
assertType('mixed', min($arr));
} else {
assertType('false', max($arr));
assertType('false', min($arr));
}

assertType('array', max([], $arr));
assertType('array', min([], $arr));

if (count($strings) > 0) {
assertType('string', max($strings));
assertType('string', min($strings));
} else {
assertType('false', max($strings));
assertType('false', min($strings));
}
}
}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1230,4 +1230,34 @@ public function testBug8389(): void
$this->analyse([__DIR__ . '/data/bug-8389.php'], []);
}

public function testBug7239(): void
{
$this->analyse([__DIR__ . '/../../Analyser/data/bug-7239.php'], [
[
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
14,
],
[
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
15,
],
[
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
21,
],
[
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
22,
],
[
'Parameter #1 ...$arg1 of function max expects non-empty-array, array{} given.',
32,
],
[
'Parameter #1 ...$arg1 of function min expects non-empty-array, array{} given.',
33,
],
]);
}

}

0 comments on commit 3b1c15f

Please sign in to comment.