diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index aea601269d..5507652bdf 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -3467,7 +3467,7 @@ static function (): void { } $itemScope = $scope; - if ($arrayItem->value instanceof ArrayDimFetch && $arrayItem->value->dim === null) { + if ($enterExpressionAssign) { $itemScope = $itemScope->enterExpressionAssign($arrayItem->value); } $itemScope = $this->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value); diff --git a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php index aace54d292..361505c081 100644 --- a/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php @@ -117,6 +117,16 @@ public function testRule(): void 148, $tip, ], + [ + 'Property UnusedPrivateProperty\ArrayAssign::$foo is never read, only written.', + 162, + $tip, + ], + [ + 'Property UnusedPrivateProperty\ListAssign::$foo is never read, only written.', + 191, + $tip, + ], [ 'Property UnusedPrivateProperty\WriteToCollection::$collection1 is never read, only written.', 221, diff --git a/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php b/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php index 1e30444c0d..a6f65f3648 100644 --- a/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php +++ b/tests/PHPStan/Rules/Properties/MissingReadOnlyPropertyAssignRuleTest.php @@ -104,6 +104,15 @@ public function testRule(): void ]); } + public function testBug7119(): void + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped('Test requires PHP 8.1.'); + } + + $this->analyse([__DIR__ . '/data/bug-7119.php'], []); + } + public function testBug7314(): void { if (PHP_VERSION_ID < 80100) { diff --git a/tests/PHPStan/Rules/Properties/data/bug-7119.php b/tests/PHPStan/Rules/Properties/data/bug-7119.php new file mode 100644 index 0000000000..4788009633 --- /dev/null +++ b/tests/PHPStan/Rules/Properties/data/bug-7119.php @@ -0,0 +1,23 @@ += 8.1 +declare(strict_types=1); + +namespace Bug7119; + +final class FooBar +{ + private readonly mixed $value; + + /** + * @param array{value: mixed} $data + */ + public function __construct(array $data) + { + //$this->value = $data['value']; // This triggers no PHPStan error. + ['value' => $this->value] = $data; // This triggers PHPStan error. + } + + public function getValue(): mixed + { + return $this->value; + } +}