-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix inferring template type from non-empty-string
- Loading branch information
1 parent
35a66a2
commit 6a33de9
Showing
7 changed files
with
114 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace Bug5372; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template TKey of array-key | ||
* @template T | ||
*/ | ||
class Collection | ||
{ | ||
|
||
/** @var array<TKey, T> */ | ||
private $values; | ||
|
||
/** | ||
* @param array<TKey, T> $values | ||
*/ | ||
public function __construct(array $values) | ||
{ | ||
$this->values = $values; | ||
} | ||
|
||
/** | ||
* @template V | ||
* | ||
* @param callable(T): V $callback | ||
* | ||
* @return self<TKey, V> | ||
*/ | ||
public function map(callable $callback): self { | ||
return new self(array_map($callback, $this->values)); | ||
} | ||
|
||
/** | ||
* @template V of string | ||
* | ||
* @param callable(T): V $callback | ||
* | ||
* @return self<TKey, V> | ||
*/ | ||
public function map2(callable $callback): self { | ||
return new self(array_map($callback, $this->values)); | ||
} | ||
} | ||
|
||
class Foo | ||
{ | ||
|
||
/** @param Collection<int, string> $list */ | ||
function takesStrings(Collection $list): void { | ||
echo serialize($list); | ||
} | ||
|
||
/** @param class-string $classString */ | ||
public function doFoo(string $classString) | ||
{ | ||
$col = new Collection(['foo', 'bar']); | ||
assertType('Bug5372\Collection<int, string>', $col); | ||
|
||
$newCol = $col->map(static fn(string $var): string => $var . 'bar'); | ||
assertType('Bug5372\Collection<int, string>', $newCol); | ||
$this->takesStrings($newCol); | ||
|
||
$newCol = $col->map(static fn(string $var): string => $classString); | ||
assertType('Bug5372\Collection<int, string>', $newCol); | ||
$this->takesStrings($newCol); | ||
|
||
$newCol = $col->map2(static fn(string $var): string => $classString); | ||
assertType('Bug5372\Collection<int, class-string>', $newCol); | ||
$this->takesStrings($newCol); | ||
} | ||
|
||
} |