Skip to content

Commit

Permalink
Merge pull request #7583 from AndrolGenhald/bugfix/7580
Browse files Browse the repository at this point in the history
  • Loading branch information
weirdan committed Feb 4, 2022
2 parents 438a80d + 5b469ca commit 333f148
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Expand Up @@ -929,6 +929,12 @@ public static function analyzeAssignmentRef(
return false;
}

if (!isset($context->vars_in_scope[$rhs_var_id])) {
// Sometimes the $rhs_var_id isn't set in $vars_in_scope, for example if it's an unknown array offset.
$context->vars_in_scope[$rhs_var_id] = $statements_analyzer->node_data->getType($stmt->expr)
?? Type::getMixed();
}

if (isset($context->references_in_scope[$lhs_var_id])) {
// Decrement old referenced variable's reference count
$reference_count = &$context->referenced_counts[$context->references_in_scope[$lhs_var_id]];
Expand Down
29 changes: 29 additions & 0 deletions tests/ReferenceTest.php
Expand Up @@ -218,6 +218,35 @@ function &ensure_array(array &$what, array $keys): array
}
',
],
'dontCrashOnReferenceToMixedVariableArrayOffset' => [
'code' => '<?php
function func(&$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
'ignored_issues' => ['MixedArrayAccess', 'MissingParamType'],
],
'dontCrashOnReferenceToArrayUnknownOffset' => [
'code' => '<?php
function func(array &$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
],
'dontCrashOnReferenceToArrayMixedOffset' => [
'code' => '<?php
/** @param array{f: mixed} $a */
function func(array &$a): void
{
$_ = &$a["f"];
}
',
'assertions' => [],
],
];
}

Expand Down

0 comments on commit 333f148

Please sign in to comment.