Skip to content

Commit 2949e3d

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix false positive "offset might not exist" for string offset in union
- Skip "might not exist" report for string types with integer offsets in NonexistentOffsetInArrayDimFetchCheck decomposition check - When a union of constant strings (e.g. ''|':') is accessed with a valid integer offset, the decomposition incorrectly flagged it because one member (empty string) returned "no" for the offset - The union-level check already correctly returns "maybe" for such cases - New regression test in tests/PHPStan/Rules/Arrays/data/bug-13688.php Closes phpstan/phpstan#13688
1 parent c36922b commit 2949e3d

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

src/Rules/Arrays/NonexistentOffsetInArrayDimFetchCheck.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ public function check(
126126
if (
127127
$innerType->hasOffsetValueType($innerDimType)->no()
128128
) {
129+
if ($innerType->isString()->yes() && $innerDimType->isInteger()->yes()) {
130+
continue;
131+
}
129132
$report = true;
130133
break 2;
131134
}

tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,4 +1277,9 @@ public function testBug14308(): void
12771277
$this->analyse([__DIR__ . '/data/bug-14308.php'], []);
12781278
}
12791279

1280+
public function testBug13688(): void
1281+
{
1282+
$this->analyse([__DIR__ . '/data/bug-13688.php'], []);
1283+
}
1284+
12801285
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13688;
4+
5+
$inputs = [ '', ':' ];
6+
7+
foreach ( $inputs as $input )
8+
{
9+
$inputLen = \strlen($input);
10+
$hasTrailingColon = $inputLen > 0 && $input[$inputLen-1] === ':';
11+
echo $hasTrailingColon ? "{$input} has trailing colon\n" : "{$input} does not have trailing colon\n";
12+
}

0 commit comments

Comments
 (0)