Skip to content

Commit 92deb3f

Browse files
authored
Make NonEmptyArrayType::toArray() return $this
1 parent 5a1a6af commit 92deb3f

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

src/Type/Accessory/NonEmptyArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public function toString(): Type
406406

407407
public function toArray(): Type
408408
{
409-
return new MixedType();
409+
return $this;
410410
}
411411

412412
public function toArrayKey(): Type

src/Type/Php/ArraySliceFunctionReturnTypeExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node\Expr\FuncCall;
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
8+
use PHPStan\Type\Constant\ConstantArrayType;
89
use PHPStan\Type\Constant\ConstantIntegerType;
910
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1011
use PHPStan\Type\Type;
@@ -50,7 +51,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5051
}
5152

5253
if ($valueType->isIterableAtLeastOnce()->yes()) {
53-
return $valueType->toArray();
54+
return TypeCombinator::union($valueType, new ConstantArrayType([], []));
5455
}
5556

5657
return $valueType;

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,7 @@ public function dataFileAsserts(): iterable
12601260
yield from $this->gatherAssertTypes(__DIR__ . '/data/base64_decode.php');
12611261
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9404.php');
12621262
yield from $this->gatherAssertTypes(__DIR__ . '/data/globals.php');
1263+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9208.php');
12631264
}
12641265

12651266
/**

tests/PHPStan/Analyser/data/array-slice.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ class Foo
99

1010
/**
1111
* @param non-empty-array $a
12+
* @param non-empty-list $b
13+
* @param non-empty-array<int>|non-empty-list<string> $c
1214
*/
13-
public function nonEmpty(array $a): void
15+
public function nonEmpty(array $a, array $b, array $c): void
1416
{
1517
assertType('array', array_slice($a, 1));
18+
assertType('list<mixed>', array_slice($b, 1));
19+
assertType('array<int|string>', array_slice($c, 1));
1620
}
1721

1822
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Bug9208;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param int|non-empty-list<int> $id_or_ids
9+
* @return non-empty-list<int>
10+
*/
11+
function f(int|array $id_or_ids): array
12+
{
13+
if (is_array($id_or_ids)) {
14+
assertType('non-empty-list<int>', (array)$id_or_ids);
15+
} else {
16+
assertType('array{int}', (array)$id_or_ids);
17+
}
18+
19+
$ids = (array)$id_or_ids;
20+
assertType('non-empty-list<int>', $ids);
21+
return $ids;
22+
}

0 commit comments

Comments
 (0)