Skip to content

Commit 3091c0b

Browse files
phpstan-botstaabm
andcommitted
Address review comments
- Remove bug-14129.php (already covered by set-constant-union-offset-on-constant-array.php) - Move bug-7978.php from NSRT to CallMethodsRuleTest - Add de-duplication for overlapping integer ranges in resolveFiniteScalarKeyTypes() - Add test for overlapping ranges (int<0,3>|int<2,4>) Co-authored-by: Markus Staab <staabm@users.noreply.github.com>
1 parent 24bf9af commit 3091c0b

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,18 @@ private function resolveFiniteScalarKeyTypes(Type $offsetType): ?array
777777
$integerRanges = TypeUtils::getIntegerRanges($offsetType);
778778
if (count($integerRanges) > 0) {
779779
$finiteScalarTypes = [];
780+
$seen = [];
780781
foreach ($integerRanges as $integerRange) {
781782
$finiteTypes = $integerRange->getFiniteTypes();
782783
if ($finiteTypes === []) {
783784
return null;
784785
}
785786

786787
foreach ($finiteTypes as $finiteType) {
788+
if (isset($seen[$finiteType->getValue()])) {
789+
continue;
790+
}
791+
$seen[$finiteType->getValue()] = true;
787792
$finiteScalarTypes[] = $finiteType;
788793
}
789794
}

tests/PHPStan/Analyser/nsrt/bug-14129.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

tests/PHPStan/Analyser/nsrt/set-constant-union-offset-on-constant-array.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ public function doUnionOfRanges(array $a, $intRange): void
4747
assertType('non-empty-array<\'foo\'|int<0, 5>|int<10, 15>, int>', $a);
4848
}
4949

50+
/**
51+
* @param array{foo: int} $a
52+
* @param int<0, 3>|int<2, 4> $intRange
53+
*/
54+
public function doOverlappingRanges(array $a, $intRange): void
55+
{
56+
$a[$intRange] = 256;
57+
assertType('array{foo: int, 0: 256}|array{foo: int, 1: 256}|array{foo: int, 2: 256}|array{foo: int, 3: 256}|array{foo: int, 4: 256}', $a);
58+
}
59+
5060
/**
5161
* @param array{0: 'a', 1: 'b'} $a
5262
* @param int<0,1> $intRange

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3853,4 +3853,12 @@ public function testBug13805(): void
38533853
$this->analyse([__DIR__ . '/data/bug-13805.php'], []);
38543854
}
38553855

3856+
public function testBug7978(): void
3857+
{
3858+
$this->checkThisOnly = false;
3859+
$this->checkNullables = true;
3860+
$this->checkUnionTypes = true;
3861+
$this->analyse([__DIR__ . '/data/bug-7978.php'], []);
3862+
}
3863+
38563864
}

tests/PHPStan/Analyser/nsrt/bug-7978.php renamed to tests/PHPStan/Rules/Methods/data/bug-7978.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
namespace Bug7978;
44

5-
use function PHPStan\Testing\assertType;
6-
75
class Test {
86

97
const FIELD_SETS = [
108
'basic' => ['username', 'password'],
119
'headers' => ['app_id', 'app_key'],
1210
];
1311

12+
/**
13+
* @param array<string, string> $credentials
14+
*/
15+
public function acceptCredentials(array $credentials): void
16+
{
17+
}
18+
1419
public function doSomething(): void
1520
{
1621
foreach (self::FIELD_SETS as $type => $fields) {
1722
$credentials = [];
1823
foreach ($fields as $field) {
1924
$credentials[$field] = 'fake';
2025
}
21-
assertType("array{app_id?: 'fake', app_key?: 'fake', password?: 'fake', username?: 'fake'}", $credentials);
26+
$this->acceptCredentials($credentials);
2227
}
2328
}
2429
}

0 commit comments

Comments
 (0)