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

Conversation

MoonE
Copy link
Contributor

@MoonE MoonE commented Nov 9, 2024

Fixes #11151

If the property was not already known inside the IssetAnalyzer::analyze function, it was added as mixed to the context.

This adds a check for dynamic properties.

Comment on lines +34 to +43
if (isset($context->vars_in_scope['$this'])) {
InstancePropertyFetchAnalyzer::analyze(
$statements_analyzer,
$isset_var,
$context,
);
}
if (!isset($context->vars_in_scope[$var_id])) {
$context->vars_in_scope[$var_id] = Type::getMixed();
}
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.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@property-read dynamic property type not working sometimes
1 participant