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

Fix return type of array_reverse() with optional keys #3406

Open
wants to merge 1 commit into
base: 1.12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
use function array_merge;
use function array_pop;
use function array_push;
use function array_reverse;
use function array_slice;
use function array_unique;
use function array_values;
Expand Down Expand Up @@ -886,14 +885,16 @@ public function popArray(): Type

public function reverseArray(TrinaryLogic $preserveKeys): Type
{
$keyTypesReversed = array_reverse($this->keyTypes, true);
$keyTypes = array_values($keyTypesReversed);
$keyTypesReversedKeys = array_keys($keyTypesReversed);
$optionalKeys = array_map(static fn (int $optionalKey): int => $keyTypesReversedKeys[$optionalKey], $this->optionalKeys);
$builder = ConstantArrayTypeBuilder::createEmpty();

$reversed = new self($keyTypes, array_reverse($this->valueTypes), $this->nextAutoIndexes, $optionalKeys, TrinaryLogic::createNo());
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
$offsetType = $preserveKeys->yes() || $this->keyTypes[$i]->isInteger()->no()
? $this->keyTypes[$i]
: null;
$builder->setOffsetValueType($offsetType, $this->valueTypes[$i], $this->isOptionalKey($i));
}

return $preserveKeys->yes() ? $reversed : $reversed->reindex();
return $builder->getArray();
}

public function searchArray(Type $needleType): Type
Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/nsrt/array-reverse.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public function normalArrays(array $a, array $b): void
/**
* @param array{a: 'foo', b: 'bar', c?: 'baz'} $a
* @param array{17: 'foo', 19: 'bar'}|array{foo: 17, bar: 19} $b
* @param array{0: 'A', 1?: 'B', 2?: 'C'} $c
*/
public function constantArrays(array $a, array $b): void
public function constantArrays(array $a, array $b, array $c): void
{
assertType('array{}', array_reverse([]));
assertType('array{}', array_reverse([], true));
Expand All @@ -45,6 +46,9 @@ public function constantArrays(array $a, array $b): void

assertType('array{\'bar\', \'foo\'}|array{bar: 19, foo: 17}', array_reverse($b));
assertType('array{19: \'bar\', 17: \'foo\'}|array{bar: 19, foo: 17}', array_reverse($b, true));

assertType("array{0: 'A'|'B'|'C', 1?: 'A'|'B', 2?: 'A'}", array_reverse($c));
assertType("array{2?: 'C', 1?: 'B', 0: 'A'}", array_reverse($c, true));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,11 @@ public function testBug8881(): void
$this->analyse([__DIR__ . '/data/bug-8881.php'], []);
}

public function testBug11549(): void
{
$this->checkExplicitMixed = true;
$this->checkNullables = true;
$this->analyse([__DIR__ . '/data/bug-11549.php'], []);
}

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

namespace Bug11549;

/**
* @param array{0: string, 1?: string} $a
* @return array{0: string, 1?: string}
*/
function rrr(array $a): array
{
return array_reverse($a);
}
Loading