Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions src/Type/Generic/TemplateTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
namespace PHPStan\Type\Generic;

use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\MixedType;
use PHPStan\Type\NonAcceptingNeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeTraverser;
Expand Down Expand Up @@ -147,6 +150,14 @@ public static function generalizeInferredTemplateType(TemplateType $templateType
} elseif ($type->isConstantValue()->yes() && (!$templateType->getBound()->isScalar()->yes() || $isArrayKey)) {
$type = $type->generalize(GeneralizePrecision::templateArgument());
}

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

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

return $type;
Expand Down
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