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
38 changes: 38 additions & 0 deletions src/Type/Generic/TemplateConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,42 @@ public function __construct(
$this->default = $default;
}

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
if ($this->isOffsetWithinBound($offsetType, $valueType)) {
return $this;
}

return parent::setOffsetValueType($offsetType, $valueType, $unionValues);
}

public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
{
if ($this->isOffsetWithinBound($offsetType, $valueType)) {
return $this;
}

return parent::setExistingOffsetValueType($offsetType, $valueType);
}

private function isOffsetWithinBound(?Type $offsetType, Type $valueType): bool
{
if ($offsetType === null) {
return false;
}

$boundKeyTypes = $this->bound->getKeyTypes();
$boundValueTypes = $this->bound->getValueTypes();

foreach ($boundKeyTypes as $i => $boundKeyType) {
if (!$offsetType->equals($boundKeyType)) {
continue;
}

return $boundValueTypes[$i]->accepts($valueType, true)->yes();
}

return false;
}

}
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1310,4 +1310,9 @@ public function testBug9669(): void
$this->analyse([__DIR__ . '/data/bug-9669.php'], []);
}

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

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

namespace Bug10172;

class HelloWorld
{
/**
* @template T of array{data: array<mixed>}
*
* @param T $a
*
* @return T
*/
public function foo(array $a): array
{
foreach ($a['data'] as $i) {
}

return $a;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1003,4 +1003,9 @@ public function testCloneWith(): void
]);
}

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

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

namespace Bug7170;

/**
* @template Tdata of array{extension?: array<mixed>}
*/
class Data
{
/**
* @var Tdata
*/
private $data;

/**
* @param Tdata $data
*/
public function __construct(array $data = [])
{
$this->data = $data;
}

public function setExtensionProperty(): void
{
if (!isset($this->data['extension'])) {
$this->data['extension'] = [];
}
}
}

class NonGeneric
{
/**
* @var array{extension?: array<mixed>}
*/
private $data;

public function setData(): void
{
if (!isset($this->data['extension'])) {
$this->data['extension'] = [];
}
}
}
Loading