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

Merged
merged 2 commits into from
Feb 16, 2025

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 +45
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.

@MoonE MoonE changed the base branch from 5.x to 6.x February 16, 2025 19:38
@MoonE MoonE force-pushed the fix-property-get-coalesce-type branch from c0672fe to ab26b95 Compare February 16, 2025 19:38
@MoonE MoonE force-pushed the fix-property-get-coalesce-type branch from ab26b95 to a3ffc49 Compare February 16, 2025 19:39
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Feb 16, 2025
@orklah
Copy link
Collaborator

orklah commented Feb 16, 2025

Thanks!

@orklah orklah merged commit be5dded into vimeo:6.x Feb 16, 2025
50 of 52 checks passed
@MoonE MoonE deleted the fix-property-get-coalesce-type branch February 16, 2025 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix The PR will be included in 'Fixes' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@property-read dynamic property type not working sometimes
2 participants