diff --git a/src/Type/Generic/TemplateConstantArrayType.php b/src/Type/Generic/TemplateConstantArrayType.php index fbc13bad6d..9cdae5ec6b 100644 --- a/src/Type/Generic/TemplateConstantArrayType.php +++ b/src/Type/Generic/TemplateConstantArrayType.php @@ -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; + } + } diff --git a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php index 88b2bb4a2c..f9ec8d6a4b 100644 --- a/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/Properties/data/bug-7170.php b/tests/PHPStan/Rules/Properties/data/bug-7170.php new file mode 100644 index 0000000000..1953273a59 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-7170.php @@ -0,0 +1,44 @@ +} + */ +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} + */ + private $data; + public function setData(): void + { + if (!isset($this->data['extension'])) { + $this->data['extension'] = []; + } + } +}