Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.12.x' into 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 7, 2024
2 parents b37d269 + 2e65472 commit 57519cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,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 @@ -858,14 +857,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 @@ -24,8 +24,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 @@ -47,6 +48,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 @@ -315,4 +315,11 @@ public function testBug11032(): void
$this->analyse([__DIR__ . '/data/bug-11032.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);
}

0 comments on commit 57519cc

Please sign in to comment.