Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public function getItemType(): Type
return $this->itemType;
}

/**
* Build a same-kind array with new key/item types. Subclasses
* (e.g. {@see TemplateArrayType}) override this to preserve their
* extra metadata across array-mutating operations such as offset
* writes and unsets.
*/
protected function withTypes(Type $keyType, Type $itemType): self
{
return new self($keyType, $itemType);
}

public function getReferencedClasses(): array
{
return array_merge(
Expand Down Expand Up @@ -354,7 +365,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
}

return new IntersectionType([
new self(
$this->withTypes(
TypeCombinator::union($this->keyType, $offsetType),
TypeCombinator::union($this->itemType, $valueType),
),
Expand All @@ -364,7 +375,7 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
}

return new IntersectionType([
new self(
$this->withTypes(
TypeCombinator::union($this->keyType, $offsetType),
$unionValues ? TypeCombinator::union($this->itemType, $valueType) : $valueType,
),
Expand Down Expand Up @@ -621,7 +632,7 @@ public function traverse(callable $cb): Type
return new ConstantArrayType([], []);
}

return new self($keyType, $itemType);
return $this->withTypes($keyType, $itemType);
}

return $this;
Expand Down Expand Up @@ -664,7 +675,7 @@ public function traverseSimultaneously(Type $right, callable $cb): Type
return new ConstantArrayType([], []);
}

return new self($keyType, $itemType);
return $this->withTypes($keyType, $itemType);
}

return $this;
Expand Down
12 changes: 12 additions & 0 deletions src/Type/Generic/TemplateArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ public function __construct(
$this->default = $default;
}

protected function withTypes(Type $keyType, Type $itemType): ArrayType
{
return new self(
$this->scope,
$this->strategy,
$this->variance,
$this->name,
new ArrayType($keyType, $itemType),
$this->default,
);
}

}
27 changes: 27 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\TemplateArrayType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
Expand Down Expand Up @@ -398,6 +399,21 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
$isList = $this->isList()->yes();
$isArray = $this->isArray()->yes();
$isNonEmptyArray = $this->isIterableAtLeastOnce()->yes();
// When a TemplateArrayType carries the array refinement, we describe
// it via its own describe() (e.g. "T of array") rather than collapsing
// it into a generic `array<...>` prefix. In that case the
// `NonEmptyArrayType` and `AccessoryArrayListType` markers must
// describe themselves explicitly — they cannot be absorbed into a
// non-existent `non-empty-array` prefix.
$hasTemplateArray = false;
if ($isArray || $isList) {
foreach ($this->types as $type) {
if ($type instanceof TemplateArrayType) {
$hasTemplateArray = true;
break;
}
}
}
$describedTypes = [];
foreach ($this->getSortedTypes() as $i => $type) {
if ($type instanceof AccessoryNonEmptyStringType
Expand Down Expand Up @@ -436,6 +452,14 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
continue;
}
if ($isList || $isArray) {
if ($type instanceof TemplateArrayType) {
// Preserve the template's own describe (e.g. "T of array")
// instead of collapsing it to a generic array shape — the
// other intersection members already carry the array
// refinement.
$describedTypes[$i] = $type->describe($level);
continue;
}
if ($type instanceof ArrayType) {
$keyType = $type->getKeyType();
$valueType = $type->getItemType();
Expand Down Expand Up @@ -473,6 +497,9 @@ private function describeItself(VerbosityLevel $level, bool $skipAccessoryTypes)
continue;
}
if ($type instanceof NonEmptyArrayType || $type instanceof AccessoryArrayListType) {
if ($hasTemplateArray) {
$describedTypes[$i] = $type->describe($level);
}
continue;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Functions/data/bug-3931.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
function addSomeKey(array $arr, int $value): array {
$arr['mykey'] = $value;
assertType("non-empty-array&hasOffsetValue('mykey', int)", $arr); // should preserve T
assertType("T of array (function Bug3931\\addSomeKey(), argument)&hasOffsetValue('mykey', int)&non-empty-array", $arr);
return $arr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1066,4 +1066,9 @@ public function testBug10924(): void
$this->analyse([__DIR__ . '/../Methods/data/bug-10924.php'], []);
}

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

}
27 changes: 27 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-10749.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Bug10749;

use ArrayAccess;

/**
* @template T of array<string, mixed>
* @implements ArrayAccess<key-of<T>, value-of<T>>
*/
abstract class Base implements ArrayAccess
{

/** @var T */
protected array $data = [];

/**
* @template K of key-of<T>
* @param K|null $offset
* @param T[K] $value
*/
public function offsetSet($offset, $value): void
{
$this->data[$offset] = $value;
}

}
Loading