Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-worman committed May 26, 2023
1 parent b99857c commit be30105
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
Expand Up @@ -981,17 +981,23 @@ public static function analyzeAssignmentRef(
$context->references_to_external_scope[$lhs_var_id] = true;
}
if (strpos($rhs_var_id, '->') !== false) {
IssueBuffer::maybeAdd(new UnsupportedPropertyReferenceUsage(
new CodeLocation($statements_analyzer->getSource(), $stmt),
));
IssueBuffer::maybeAdd(
new UnsupportedPropertyReferenceUsage(
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
// Reference to object property, we always consider object properties to be an external scope for references
// TODO handle differently so it's detected as unused if the object is unused?
$context->references_to_external_scope[$lhs_var_id] = true;
}
if (strpos($rhs_var_id, '::') !== false) {
IssueBuffer::maybeAdd(new UnsupportedPropertyReferenceUsage(
new CodeLocation($statements_analyzer->getSource(), $stmt),
));
IssueBuffer::maybeAdd(
new UnsupportedPropertyReferenceUsage(
new CodeLocation($statements_analyzer->getSource(), $stmt),
),
$statements_analyzer->getSuppressedIssues(),
);
}

$lhs_location = new CodeLocation($statements_analyzer->getSource(), $stmt->var);
Expand Down
35 changes: 35 additions & 0 deletions tests/UnsupportedPropertyReferenceUsage.php
Expand Up @@ -5,11 +5,30 @@
namespace Psalm\Tests;

use Psalm\Tests\Traits\InvalidCodeAnalysisTestTrait;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;

class UnsupportedPropertyReferenceUsage extends TestCase
{
use ValidCodeAnalysisTestTrait;
use InvalidCodeAnalysisTestTrait;

public function providerValidCodeParse(): iterable
{
return [
'can be suppressed' => [
'code' => <<<'PHP'
<?php
class A {
public int $b = 0;
}
$a = new A();
/** @psalm-suppress UnsupportedPropertyReferenceUsage */
$b = &$a->b;
PHP,
],
];
}

public function providerInvalidCodeParse(): iterable
{
return [
Expand Down Expand Up @@ -53,6 +72,22 @@ public function __construct(
'error_levels' => [],
'php_version' => '8.1',
],
'instance property, foreach assignment' => [
'code' => <<<'PHP'
<?php
class A {
public readonly array $b = [1, 2, 3];
}
$a = new A();
$b = &$a->b;
foreach ($a->b as &$b) {
$b = 4; // Fatal error
}
PHP,
'error_message' => 'UnsupportedPropertyReferenceUsage',
'error_levels' => [],
'php_version' => '8.1',
],
];
}
}

0 comments on commit be30105

Please sign in to comment.