Skip to content

Commit

Permalink
Fix node scope resolving of array/list expression assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Jun 8, 2022
1 parent 778308d commit db3863a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnusedPrivatePropertyRuleTest.php
Expand Up @@ -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,
Expand Down
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-7119.php
@@ -0,0 +1,23 @@
<?php // lint >= 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;
}
}

0 comments on commit db3863a

Please sign in to comment.