Skip to content
Closed
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
14 changes: 13 additions & 1 deletion src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\IsSuperTypeOfResult;
Expand Down Expand Up @@ -44,7 +45,9 @@ class HasOffsetType implements CompoundType, AccessoryType
use NonGenericTypeTrait;
use UndecidedComparisonCompoundTypeTrait;
use NonRemoveableTypeTrait;
use NonGeneralizableTypeTrait;
use NonGeneralizableTypeTrait {
generalize as traitGeneralize;
}

/**
* @api
Expand All @@ -53,6 +56,15 @@ public function __construct(private ConstantStringType|ConstantIntegerType $offs
{
}

public function generalize(GeneralizePrecision $precision): Type
{
if ($precision->isTemplateArgument()) {
return new MixedType();
}

return $this->traitGeneralize($precision);
}

public function getOffsetType(): ConstantStringType|ConstantIntegerType
{
return $this->offsetType;
Expand Down
14 changes: 13 additions & 1 deletion src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\IntersectionType;
Expand Down Expand Up @@ -48,12 +49,23 @@ class HasOffsetValueType implements CompoundType, AccessoryType
use NonGenericTypeTrait;
use UndecidedComparisonCompoundTypeTrait;
use NonRemoveableTypeTrait;
use NonGeneralizableTypeTrait;
use NonGeneralizableTypeTrait {
generalize as traitGeneralize;
}

public function __construct(private ConstantStringType|ConstantIntegerType $offsetType, private Type $valueType)
{
}

public function generalize(GeneralizePrecision $precision): Type
{
if ($precision->isTemplateArgument()) {
return new MixedType();
}

return $this->traitGeneralize($precision);
}

public function getOffsetType(): ConstantStringType|ConstantIntegerType
{
return $this->offsetType;
Expand Down
10 changes: 10 additions & 0 deletions src/Type/Generic/TemplateTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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\NonAcceptingNeverType;
Expand Down Expand Up @@ -147,6 +149,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 $type->generalize(GeneralizePrecision::templateArgument());
}

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