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

public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
{
$result = parent::setOffsetValueType($offsetType, $valueType, $unionValues);

if ($this->getBound()->isSuperTypeOf($result)->yes()) {
return $this;
}

return $result;
}

public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
{
$result = parent::setExistingOffsetValueType($offsetType, $valueType);

if ($this->getBound()->isSuperTypeOf($result)->yes()) {
return $this;
}

return $result;
}

public function unsetOffset(Type $offsetType, bool $preserveListCertainty = false): Type
{
$result = parent::unsetOffset($offsetType, $preserveListCertainty);

if ($this->getBound()->isSuperTypeOf($result)->yes()) {
return $this;
}

return $result;
}

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

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