Skip to content

Commit 7172e5f

Browse files
committed
Fix casting non-empty-string array key type
1 parent aa9e2e8 commit 7172e5f

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

src/Type/ArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ public static function castToArrayKeyType(Type $offsetType): Type
317317
return new IntegerType();
318318
}
319319

320-
if ($offsetType instanceof StringType) {
320+
if ($offsetType instanceof StringType || $offsetType->isNonEmptyString()->yes()) {
321321
return $offsetType;
322322
}
323323

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,11 @@ public function dataFileAsserts(): iterable
447447
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4970.php');
448448
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5322.php');
449449
yield from $this->gatherAssertTypes(__DIR__ . '/data/splfixedarray-iterator-types.php');
450-
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5372.php');
450+
451+
if (PHP_VERSION_ID >= 70400 || self::$useStaticReflectionProvider) {
452+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5372.php');
453+
}
454+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-5372_2.php');
451455
}
452456

453457
/**

tests/PHPStan/Analyser/data/bug-5219.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protected function foo(string $message): void
1212
$header = sprintf('%s-%s', '', implode('-', ['x']));
1313

1414
assertType('non-empty-string', $header);
15-
assertType('array<string, string>&nonEmpty', [$header => $message]);
15+
assertType('array<non-empty-string, string>&nonEmpty', [$header => $message]);
1616
}
1717

1818
protected function bar(string $message): void

tests/PHPStan/Analyser/data/non-empty-string.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public function doFoo(array $a, string $s): void
282282
$a[$s] = 2;
283283

284284
// there might be non-empty-string that becomes a number instead
285-
assertType('array<string, int>&nonEmpty', $a);
285+
assertType('array<non-empty-string, int>&nonEmpty', $a);
286286
}
287287

288288
/**
@@ -309,12 +309,12 @@ public function doFoo(string $s, string $nonEmpty, int $i)
309309
assertType('non-empty-string', addslashes($nonEmpty));
310310
assertType('string', addcslashes($s));
311311
assertType('non-empty-string', addcslashes($nonEmpty));
312-
312+
313313
assertType('string', escapeshellarg($s));
314314
assertType('non-empty-string', escapeshellarg($nonEmpty));
315315
assertType('string', escapeshellcmd($s));
316316
assertType('non-empty-string', escapeshellcmd($nonEmpty));
317-
317+
318318
assertType('string', strtoupper($s));
319319
assertType('non-empty-string', strtoupper($nonEmpty));
320320
assertType('string', strtolower($s));

tests/PHPStan/Rules/Arrays/AppendedArrayKeyTypeRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public function testRule(): void
5656
]);
5757
}
5858

59+
public function testBug5372Two(): void
60+
{
61+
$this->analyse([__DIR__ . '/data/bug-5372_2.php'], []);
62+
}
63+
5964
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug5372Two;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class X
8+
{
9+
/** @var array<non-empty-string, string> */
10+
private $map = [];
11+
12+
/**
13+
* @param array<non-empty-string> $values
14+
*/
15+
public function __construct(array $values)
16+
{
17+
assertType('array<non-empty-string>', $values);
18+
foreach ($values as $v) {
19+
assertType('non-empty-string', $v);
20+
$this->map[$v] = 'whatever';
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)