Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node scope resolving of array/list expression assignments #1405

Merged
merged 1 commit into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}