Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat ConstantArrayType as covariant in its keys and values #2464

Merged
merged 1 commit into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ public function inferTemplateTypes(Type $receivedType): TemplateTypeMap

public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
{
$variance = $positionVariance->compose(TemplateTypeVariance::createInvariant());
$variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
$references = [];

foreach ($this->keyTypes as $type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,9 @@ public function testBug8880(): void
]);
}

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

}
39 changes: 39 additions & 0 deletions tests/PHPStan/Rules/Generics/data/bug-9161.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug9161;

/**
* @template-covariant TKey of int|string
* @template-covariant TValue
*/
final class Map
{
/**
* @param array<TKey, TValue> $items
*/
public function __construct(
private array $items = [],
) {
}

/**
* @return array<TKey, TValue>
*/
public function toArray(): array
{
return $this->items;
}

/**
* @return list<array{0: TKey, 1: TValue}>
*/
public function toPairs(): array
{
$pairs = [];
foreach ($this->items as $key => $value) {
$pairs[] = [$key, $value];
}

return $pairs;
}
}