Skip to content

Commit

Permalink
Fix #3127 - allow templated param to be accepted where static is expe…
Browse files Browse the repository at this point in the history
…cted
  • Loading branch information
muglug committed Apr 13, 2020
1 parent 2939455 commit 15df39f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Psalm/Internal/Analyzer/TypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,10 @@ private static function isObjectContainedByObject(
|| $intersection_input_type instanceof TTemplateParam
) {
if ($intersection_container_type_lower === $intersection_input_type_lower) {
if ($container_was_static && !$input_was_static) {
if ($container_was_static
&& !$input_was_static
&& !$intersection_input_type instanceof TTemplateParam
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
}
Expand Down
56 changes: 56 additions & 0 deletions tests/Template/ClassTemplateExtendsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3591,6 +3591,62 @@ public function i($changedArgumentName): void {
}
}'
],
'acceptTemplatedObjectAsStaticParam' => [
'<?php
/**
* @psalm-immutable
*/
abstract class Id
{
protected string $id;
final protected function __construct(string $id)
{
$this->id = $id;
}
/**
* @param static $id
*/
final public function equals(self $id): bool
{
return $this->id === $id->id;
}
}
/**
* @template T of Id
*/
final class Ids
{
/**
* @psalm-var list<T>
*/
private array $ids;
/**
* @psalm-param list<T> $ids
*/
private function __construct(array $ids)
{
$this->ids = $ids;
}
/**
* @psalm-param T $id
*/
public function contains(Id $id): bool
{
foreach ($this->ids as $oneId) {
if ($oneId->equals($id)) {
return true;
}
}
return false;
}
}'
],
];
}

Expand Down

0 comments on commit 15df39f

Please sign in to comment.