Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/Type/Generic/TemplateTypeVariance.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\IsSuperTypeOfResult;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
use function sprintf;

/**
Expand Down Expand Up @@ -177,6 +180,13 @@ public function isValidVariance(TemplateType $templateType, Type $a, Type $b): I

if ($this->invariant()) {
$result = $a->equals($b);
if (!$result) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjust the corresponding equals-method implementation instead

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And make phpstan also confirmed passing (exit code 0). Both verification runs for the final solution completed successfully. The commit has already been pushed to the PR branch.

$strippedA = self::stripHasOffsetTypes($a);
$strippedB = self::stripHasOffsetTypes($b);
if ($strippedA !== $a || $strippedB !== $b) {
$result = $strippedA->equals($strippedB);
}
}
$reasons = [];
if (!$result) {
if (
Expand Down Expand Up @@ -259,4 +269,15 @@ public function toPhpDocNodeVariance(): string
throw new ShouldNotHappenException();
}

private static function stripHasOffsetTypes(Type $type): Type
{
return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof HasOffsetValueType || $type instanceof HasOffsetType) {
return new MixedType();
}

return $traverse($type);
});
}

}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,11 @@ public function testBug12397(): void
$this->analyse([__DIR__ . '/data/bug-12397.php'], []);
}

public function testBug11507(): void
{
$this->checkNullables = true;
$this->checkExplicitMixed = true;
$this->analyse([__DIR__ . '/data/bug-11507.php'], []);
}

}
50 changes: 50 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-11507.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug11507;

/**
* @template TValue
*/
class Collection
{
/**
* @param array<int, TValue> $items
*/
public function __construct(
public array $items,
) {}

/**
* Run a map over each of the items.
*
* @template TMapValue
*
* @param callable(TValue, int=): TMapValue $callback
* @return Collection<TMapValue>
*/
public function map(callable $callback): Collection
{
$keys = array_keys($this->items);

$items = array_map($callback, $this->items);

$result = array_combine($keys, $items);

return new self($result);
}
}

/**
* @param Collection<non-empty-array<string>> $collection
* @return Collection<non-empty-array<string>>
*/
function foo(Collection $collection): Collection
{
return $collection->map(function (array $item) {
$item['foo'] = '100';

return $item;
});
}
Loading