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
6 changes: 4 additions & 2 deletions src/Rules/Arrays/AppendedArrayItemTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Arrays;

use ArrayAccess;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
Expand All @@ -13,6 +14,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ArrayType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

Expand Down Expand Up @@ -67,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array
}

$assignedToType = $propertyReflection->getWritableType();
if (!($assignedToType instanceof ArrayType)) {
if (!($assignedToType instanceof ArrayType) && !(new ObjectType(ArrayAccess::class))->isSuperTypeOf($assignedToType)->yes()) {
return [];
}

Expand All @@ -77,7 +79,7 @@ public function processNode(Node $node, Scope $scope): array
$assignedValueType = $scope->getType($node);
}

$itemType = $assignedToType->getItemType();
$itemType = $assignedToType->getIterableValueType();
if (!$this->ruleLevelHelper->accepts($itemType, $assignedValueType, $scope->isDeclareStrictTypes())) {
$verbosityLevel = VerbosityLevel::getRecommendedLevelByType($itemType, $assignedValueType);
return [
Expand Down
18 changes: 18 additions & 0 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,15 @@ public function isIterableAtLeastOnce(): TrinaryLogic

public function getIterableKeyType(): Type
{
if ($this->isInstanceOf(ArrayAccess::class)->yes()) {
$tKey = GenericTypeVariableResolver::getType($this, ArrayAccess::class, 'TKey');
if ($tKey !== null) {
return $tKey;
}

return new MixedType();
}

$classReflection = $this->getClassReflection();
if ($classReflection === null) {
return new ErrorType();
Expand Down Expand Up @@ -718,6 +727,15 @@ public function getIterableKeyType(): Type

public function getIterableValueType(): Type
{
if ($this->isInstanceOf(ArrayAccess::class)->yes()) {
$tValue = GenericTypeVariableResolver::getType($this, ArrayAccess::class, 'TValue');
if ($tValue !== null) {
return $tValue;
}

return new MixedType();
}

if ($this->isInstanceOf(Iterator::class)->yes()) {
return RecursionGuard::run($this, function (): Type {
return ParametersAcceptorSelector::selectSingle(
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/bug-2676.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function (Wallet $wallet): void
assertType('DoctrineIntersectionTypeIsSupertypeOf\Collection&iterable<Bug2676\BankAccount>', $bankAccounts);

foreach ($bankAccounts as $key => $bankAccount) {
assertType('(int|string)', $key);
assertType('int|string|null', $key);
assertType('Bug2676\BankAccount', $bankAccount);
}
};
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Arrays/AppendedArrayItemTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ public function testAppendedArrayItemType(): void
'Array (array<AppendedArrayItem\Lorem>) does not accept AppendedArrayItem\Baz.',
79,
],
[
'Array (ArrayAccess<int, string>) does not accept int.',
99,
],
[
'Array (ArrayAccess<int, string>&Countable) does not accept int.',
102,
],
[
'Array (ArrayAccess<int, string>&Countable&iterable<int, string>) does not accept int.',
105,
],
[
'Array (SplObjectStorage<int, string>) does not accept int.',
108,
],
]
);
}
Expand Down
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/appended-array-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,33 @@ function (Lorem $lorem)
{
$lorem->staticProperty[] = new Baz();
};

class ArrayAccess
{
/** @var \ArrayAccess<int, string> */
private $collection1;

/** @var \ArrayAccess<int, string>&\Countable */
private $collection2;

/** @var \ArrayAccess<int, string>&\Countable&iterable<int, string> */
private $collection3;

/** @var \SplObjectStorage<int, string> */
private $collection4;

public function doFoo()
{
$this->collection1[] = 'foo';
$this->collection1[] = 1;

$this->collection2[] = 'foo';
$this->collection2[] = 2;

$this->collection3[] = 'foo';
$this->collection3[] = 3;

$this->collection4[] = 'foo';
$this->collection4[] = 4;
}
}