Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort(), rsort() and usort() converts an array to list #2891

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ClosureType;
Expand Down Expand Up @@ -2128,6 +2129,19 @@ static function (): void {
);
}

if (
$functionReflection !== null
&& in_array($functionReflection->getName(), ['sort', 'rsort', 'usort'], true)
&& count($expr->getArgs()) >= 1
) {
$arrayArg = $expr->getArgs()[0]->value;
$scope = $scope->assignExpression(
$arrayArg,
$this->getArraySortFunctionType($scope->getType($arrayArg)),
$this->getArraySortFunctionType($scope->getNativeType($arrayArg)),
);
}

if (
$functionReflection !== null
&& $functionReflection->getName() === 'extract'
Expand Down Expand Up @@ -3079,6 +3093,24 @@ static function (?Type $offsetType, Type $valueType, bool $optional) use (&$arra
return $arrayType;
}

private function getArraySortFunctionType(Type $type): Type
{
if (!$type->isArray()->yes()) {
return $type;
}

$isIterableAtLeastOnce = $type->isIterableAtLeastOnce();
if ($isIterableAtLeastOnce->no()) {
return $type;
}

$newArrayType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), $type->getIterableValueType()));
if ($isIterableAtLeastOnce->yes()) {
$newArrayType = TypeCombinator::intersect($newArrayType, new NonEmptyArrayType());
}
return $newArrayType;
}

private function getFunctionThrowPoint(
FunctionReflection $functionReflection,
?ParametersAcceptor $parametersAcceptor,
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7291.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10264.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/conditional-vars.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/sort.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3312.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5961.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10189.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10317.php');
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/bug-3312.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Bug3312;

use function PHPStan\Testing\assertType;

function sayHello(): void
{
$arr = ['one' => 'een', 'two' => 'twee', 'three' => 'drie'];
usort($arr, 'strcmp');
assertType("non-empty-list<'drie'|'een'|'twee'>", $arr);
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/param-out.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ function fooShuffle() {
function fooSort() {
$array = ["foo" => 123, "bar" => 456];
sort($array);
assertType('array{foo: 123, bar: 456}', $array);
assertType('non-empty-list<123|456>', $array);

$emptyArray = [];
sort($emptyArray);
Expand Down
130 changes: 130 additions & 0 deletions tests/PHPStan/Analyser/data/sort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php declare(strict_types=1);

namespace Sort;

use function PHPStan\Testing\assertNativeType;
use function PHPStan\Testing\assertType;
use function sort;

class Foo
{
public function constantArray(): void
{
$arr = [
4,
'one' => 1,
'five' => 5,
'three' => 3,
];

$arr1 = $arr;
sort($arr1);
assertType('non-empty-list<1|3|4|5>', $arr1);
assertNativeType('non-empty-list<1|3|4|5>', $arr1);

$arr2 = $arr;
rsort($arr2);
assertType('non-empty-list<1|3|4|5>', $arr2);
assertNativeType('non-empty-list<1|3|4|5>', $arr2);

$arr3 = $arr;
usort($arr3, fn(int $a, int $b) => $a <=> $b);
assertType('non-empty-list<1|3|4|5>', $arr3);
assertNativeType('non-empty-list<1|3|4|5>', $arr3);
}

public function constantArrayOptionalKey(): void
{
$arr = [
'one' => 1,
'five' => 5,
];
if (rand(0, 1)) {
$arr['two'] = 2;
}

$arr1 = $arr;
sort($arr1);
assertType('non-empty-list<1|2|5>', $arr1);
assertNativeType('non-empty-list<1|2|5>', $arr1);

$arr2 = $arr;
rsort($arr2);
assertType('non-empty-list<1|2|5>', $arr2);
assertNativeType('non-empty-list<1|2|5>', $arr2);

$arr3 = $arr;
usort($arr3, fn(int $a, int $b) => $a <=> $b);
assertType('non-empty-list<1|2|5>', $arr3);
assertNativeType('non-empty-list<1|2|5>', $arr3);
}

public function constantArrayUnion(): void
{
$arr = rand(0, 1) ? [
'one' => 1,
'five' => 5,
] : [
'two' => 2,
];

$arr1 = $arr;
sort($arr1);
assertType('non-empty-list<1|2|5>', $arr1);
assertNativeType('non-empty-list<1|2|5>', $arr1);

$arr2 = $arr;
rsort($arr2);
assertType('non-empty-list<1|2|5>', $arr2);
assertNativeType('non-empty-list<1|2|5>', $arr2);

$arr3 = $arr;
usort($arr3, fn(int $a, int $b) => $a <=> $b);
assertType('non-empty-list<1|2|5>', $arr3);
assertNativeType('non-empty-list<1|2|5>', $arr3);
}

/** @param array<mixed, string> $arr */
public function normalArray(array $arr): void
{
$arr1 = $arr;
sort($arr1);
assertType('list<string>', $arr1);
assertNativeType('list<mixed>', $arr1);

$arr2 = $arr;
rsort($arr2);
assertType('list<string>', $arr2);
assertNativeType('list<mixed>', $arr2);

$arr3 = $arr;
usort($arr3, fn(int $a, int $b) => $a <=> $b);
assertType('list<string>', $arr3);
assertNativeType('list<mixed>', $arr3);
}

public function mixed($arr): void
{
$arr1 = $arr;
sort($arr1);
assertType('mixed', $arr1);
assertNativeType('mixed', $arr1);

$arr2 = $arr;
rsort($arr2);
assertType('mixed', $arr2);
assertNativeType('mixed', $arr2);

$arr3 = $arr;
usort($arr3, fn(int $a, int $b) => $a <=> $b);
assertType('mixed', $arr3);
assertNativeType('mixed', $arr3);
}

public function notArray(): void
{
$arr = 'foo';
sort($arr);
assertType("'foo'", $arr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,10 @@ public function testBug5005(): void
$this->analyse([__DIR__ . '/data/bug-5005.php'], []);
}

public function testBug6467(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-6467.php'], []);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-6467.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types = 1);

namespace Bug6467;

class Comparison
{
public function compare(): void
{
$values = $sortedValues = [0.5, 1, 2, 0.8, 0.4];
sort($sortedValues);
$expected = $sortedValues[2] + 0.05;
foreach (array_fill(0, 5, null) as $index => $null) {
$success = $values[$index] < $expected;
}
}
}