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 type of property-get with coalesce #11152

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpParser;
use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Internal\Analyzer\Statements\Expression\Fetch\InstancePropertyFetchAnalyzer;
use Psalm\Internal\Analyzer\Statements\ExpressionAnalyzer;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Issue\InvalidArgument;
Expand All @@ -30,7 +31,16 @@ public static function analyze(
$var_id = '$this->' . $isset_var->name->name;

if (!isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::getMixed();
if (isset($context->vars_in_scope['$this'])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is necessary to keep this test passing:

'suppressInvalidThis' => [
'code' => '<?php
/** @psalm-suppress InvalidScope */
if (!isset($this->value)) {
$this->value = ["x", "y"];
echo count($this->value) - 2;
}',
'assertions' => [],
'ignored_issues' => ['MixedPropertyAssignment', 'MixedArgument'],
],

This checks if this is inside an object context and dynamic property access is possible.

InstancePropertyFetchAnalyzer::analyze(
$statements_analyzer,
$isset_var,
$context,
);
}
if (!isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::getMixed();
}
Comment on lines +34 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of InstancePropertyFetchAnalyzer::analyze is discarded, not sure if/how it needs to be handled.

$context->vars_possibly_in_scope[$var_id] = true;
}
} elseif (!self::isValidStatement($isset_var)) {
Expand Down
29 changes: 29 additions & 0 deletions tests/MagicPropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,20 @@ public function updateStatus(): array {
}
}',
],
'propertyFetchWithNullCoalesce' => [
'code' => '<?php
/**
* @property-read string|null $p
*/
class A {
public function __get(string $name) {
return null;
}
public function f(): string {
return $this->p ?? \'\';
}
}',
],
];
}

Expand Down Expand Up @@ -1189,6 +1203,21 @@ class A {
}',
'error_message' => 'InvalidDocblock',
],
'invalidPropertyFetchWithNullCoalesce' => [
'code' => '<?php
/**
* @property-read string|null $p
*/
class A {
public function __get(string $name) {
return null;
}
public function f(): string {
return $this->q ?? \'\';
}
}',
'error_message' => 'MixedReturnStatement',
],
];
}

Expand Down
Loading