Skip to content

Commit

Permalink
Fix casting non-empty-string array key type
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 15, 2021
1 parent aa9e2e8 commit 7172e5f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public static function castToArrayKeyType(Type $offsetType): Type
return new IntegerType();
}

if ($offsetType instanceof StringType) {
if ($offsetType instanceof StringType || $offsetType->isNonEmptyString()->yes()) {
return $offsetType;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4970.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-5322.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/splfixedarray-iterator-types.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5372.php');

if (PHP_VERSION_ID >= 70400 || self::$useStaticReflectionProvider) {
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/bug-5372.php');
}
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-5372_2.php');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-5219.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected function foo(string $message): void
$header = sprintf('%s-%s', '', implode('-', ['x']));

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

protected function bar(string $message): void
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/data/non-empty-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public function doFoo(array $a, string $s): void
$a[$s] = 2;

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

/**
Expand All @@ -309,12 +309,12 @@ public function doFoo(string $s, string $nonEmpty, int $i)
assertType('non-empty-string', addslashes($nonEmpty));
assertType('string', addcslashes($s));
assertType('non-empty-string', addcslashes($nonEmpty));

assertType('string', escapeshellarg($s));
assertType('non-empty-string', escapeshellarg($nonEmpty));
assertType('string', escapeshellcmd($s));
assertType('non-empty-string', escapeshellcmd($nonEmpty));

assertType('string', strtoupper($s));
assertType('non-empty-string', strtoupper($nonEmpty));
assertType('string', strtolower($s));
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Arrays/AppendedArrayKeyTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public function testRule(): void
]);
}

public function testBug5372Two(): void
{
$this->analyse([__DIR__ . '/data/bug-5372_2.php'], []);
}

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

namespace Bug5372Two;

use function PHPStan\Testing\assertType;

class X
{
/** @var array<non-empty-string, string> */
private $map = [];

/**
* @param array<non-empty-string> $values
*/
public function __construct(array $values)
{
assertType('array<non-empty-string>', $values);
foreach ($values as $v) {
assertType('non-empty-string', $v);
$this->map[$v] = 'whatever';
}
}
}

0 comments on commit 7172e5f

Please sign in to comment.