Skip to content
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
4 changes: 2 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1758,13 +1758,13 @@ parameters:
-
rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantArrayType is error-prone and deprecated. Use Type::getConstantArrays() instead.'
identifier: phpstanApi.instanceofType
count: 2
count: 1
path: src/Type/TypeUtils.php

-
rawMessage: Doing instanceof PHPStan\Type\IntersectionType is error-prone and deprecated.
identifier: phpstanApi.instanceofType
count: 3
count: 4
path: src/Type/TypeUtils.php

-
Expand Down
3 changes: 2 additions & 1 deletion src/Internal/CombinationsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace PHPStan\Internal;

use Traversable;
use function array_shift;

final class CombinationsHelper
{

/**
* @param array<iterable<mixed>> $arrays
* @return iterable<list<mixed>>
* @return Traversable<list<mixed>>
*/
public static function combinations(array $arrays): iterable
{
Expand Down
2 changes: 2 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ public function getAllArrays(): array
} else {
$optionalKeysCombinations = [
[],
array_slice($this->optionalKeys, 0, 1, true),
array_slice($this->optionalKeys, -1, 1, true),
$this->optionalKeys,
];
}
Expand Down
29 changes: 22 additions & 7 deletions src/Type/TypeUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace PHPStan\Type;

use PHPStan\Internal\CombinationsHelper;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateUnionType;
use function array_filter;
use function array_map;
use function array_merge;
use function iterator_to_array;

/**
* @api
Expand Down Expand Up @@ -133,17 +137,28 @@ public static function flattenTypes(Type $type): array
return $type->getAllArrays();
}

if ($type instanceof IntersectionType && $type->isConstantArray()->yes()) {
$newTypes = [];
foreach ($type->getTypes() as $innerType) {
$newTypes[] = self::flattenTypes($innerType);
}

return array_filter(
array_map(
static fn (array $types): Type => TypeCombinator::intersect(...$types),
iterator_to_array(CombinationsHelper::combinations($newTypes)),
),
static fn (Type $type): bool => !$type instanceof NeverType,
);
}

if ($type instanceof UnionType) {
$types = [];
foreach ($type->getTypes() as $innerType) {
if ($innerType instanceof ConstantArrayType) {
foreach ($innerType->getAllArrays() as $array) {
$types[] = $array;
}
continue;
$flattenTypes = self::flattenTypes($innerType);
foreach ($flattenTypes as $flattenType) {
$types[] = $flattenType;
}

$types[] = $innerType;
}

return $types;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ public function testBug7581(): void
public function testBug7903(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-7903.php');
$this->assertCount(24, $errors);
$this->assertCount(29, $errors);
}

public function testBug7901(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,6 @@ public function testBug4747(): void
$this->analyse([__DIR__ . '/data/bug-4747.php'], []);
}

public function testBug6379(): void
{
$this->analyse([__DIR__ . '/data/bug-6379.php'], []);
}

#[RequiresPhp('>= 8.0')]
public function testBug4885(): void
{
Expand Down Expand Up @@ -929,13 +924,6 @@ public function testBug4809(): void
$this->analyse([__DIR__ . '/data/bug-4809.php'], []);
}

public function testBug11602(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-11602.php'], []);
}

public function testBug12593(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down Expand Up @@ -1116,6 +1104,28 @@ public function testPR4385Bis(): void
$this->analyse([__DIR__ . '/data/pr-4385-bis.php'], []);
}

public function testBug7143(): void
{
$this->analyse([__DIR__ . '/data/bug-7143.php'], [
[
"Offset 'foo' might not exist on non-empty-array{foo?: string, bar?: string}.",
12,
],
[
"Offset 'bar' might not exist on non-empty-array{foo?: string, bar?: string}.",
13,
],
[
"Offset 'foo' might not exist on non-empty-array{foo?: string, bar?: string, 1?: 1, 2?: 2, 3?: 3, 4?: 4, 5?: 5, 6?: 6, ...}.",
21,
],
[
"Offset 'bar' might not exist on non-empty-array{foo?: string, bar?: string, 1?: 1, 2?: 2, 3?: 3, 4?: 4, 5?: 5, 6?: 6, ...}.",
22,
],
]);
}

public function testBug12805(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;
Expand Down
23 changes: 0 additions & 23 deletions tests/PHPStan/Rules/Arrays/data/bug-11602.php

This file was deleted.

21 changes: 0 additions & 21 deletions tests/PHPStan/Rules/Arrays/data/bug-6379.php

This file was deleted.

24 changes: 24 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-7143.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bug7143;

class Foo
{
/**
* @param array{foo?: string, bar?: string}&non-empty-array $arr
*/
public function test(array $arr): void
{
echo $arr['foo'];
echo $arr['bar'];
}

/**
* @param array{foo?: string, bar?: string, 1?:1, 2?:2, 3?:3, 4?:4, 5?:5, 6?:6, 7?:7, 8?:8, 9?:9}&non-empty-array $arr
*/
public function test2(array $arr): void
{
echo $arr['foo'];
echo $arr['bar'];
}
}
Loading