Skip to content

Commit 2fb6632

Browse files
committed
ConstantArrayType - string offset might exist as integer offset
1 parent e5ded04 commit 2fb6632

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -838,19 +838,14 @@ parameters:
838838

839839
-
840840
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
841-
count: 4
841+
count: 3
842842
path: src/Type/Constant/ConstantArrayType.php
843843

844844
-
845845
message: "#^Doing instanceof PHPStan\\\\Type\\\\IntersectionType is error\\-prone and deprecated\\.$#"
846846
count: 1
847847
path: src/Type/Constant/ConstantArrayType.php
848848

849-
-
850-
message: "#^Doing instanceof PHPStan\\\\Type\\\\StringType is error\\-prone and deprecated\\. Use Type\\:\\:isString\\(\\) instead\\.$#"
851-
count: 1
852-
path: src/Type/Constant/ConstantArrayType.php
853-
854849
-
855850
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
856851
count: 2

src/Type/Constant/ConstantArrayType.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use PHPStan\Type\IntersectionType;
4141
use PHPStan\Type\MixedType;
4242
use PHPStan\Type\NeverType;
43-
use PHPStan\Type\StringType;
4443
use PHPStan\Type\Type;
4544
use PHPStan\Type\TypeCombinator;
4645
use PHPStan\Type\UnionType;
@@ -619,8 +618,8 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
619618
foreach ($this->keyTypes as $i => $keyType) {
620619
if (
621620
$keyType instanceof ConstantIntegerType
622-
&& $offsetType instanceof StringType
623-
&& !$offsetType instanceof ConstantStringType
621+
&& !$offsetType->isString()->no()
622+
&& $offsetType->isConstantScalarValue()->no()
624623
) {
625624
return TrinaryLogic::createMaybe();
626625
}

tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,12 @@ public function testBug8084(): void
368368
$this->analyse([__DIR__ . '/data/bug-8084.php'], []);
369369
}
370370

371+
public function testBug10577(): void
372+
{
373+
$this->treatPhpDocTypesAsCertain = true;
374+
$this->strictUnnecessaryNullsafePropertyFetch = true;
375+
376+
$this->analyse([__DIR__ . '/data/bug-10577.php'], []);
377+
}
378+
371379
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Bug10577;
4+
5+
class HelloWorld
6+
{
7+
private const MAP = [
8+
'10' => 'Test1',
9+
'20' => 'Test2',
10+
];
11+
12+
13+
public function validate(string $value): void
14+
{
15+
$value = trim($value);
16+
17+
if ($value === '') {
18+
throw new \RuntimeException();
19+
}
20+
21+
$value = self::MAP[$value] ?? $value;
22+
23+
// ...
24+
}
25+
}

0 commit comments

Comments
 (0)