Skip to content

Commit

Permalink
ConstantArrayType - string offset might exist as integer offset
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 14, 2024
1 parent e5ded04 commit 2fb6632
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -838,19 +838,14 @@ parameters:

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

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

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\StringType is error\\-prone and deprecated\\. Use Type\\:\\:isString\\(\\) instead\\.$#"
count: 1
path: src/Type/Constant/ConstantArrayType.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
count: 2
Expand Down
5 changes: 2 additions & 3 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -619,8 +618,8 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic
foreach ($this->keyTypes as $i => $keyType) {
if (
$keyType instanceof ConstantIntegerType
&& $offsetType instanceof StringType
&& !$offsetType instanceof ConstantStringType
&& !$offsetType->isString()->no()
&& $offsetType->isConstantScalarValue()->no()
) {
return TrinaryLogic::createMaybe();
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,12 @@ public function testBug8084(): void
$this->analyse([__DIR__ . '/data/bug-8084.php'], []);
}

public function testBug10577(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->strictUnnecessaryNullsafePropertyFetch = true;

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

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/Variables/data/bug-10577.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Bug10577;

class HelloWorld
{
private const MAP = [
'10' => 'Test1',
'20' => 'Test2',
];


public function validate(string $value): void
{
$value = trim($value);

if ($value === '') {
throw new \RuntimeException();
}

$value = self::MAP[$value] ?? $value;

// ...
}
}

0 comments on commit 2fb6632

Please sign in to comment.