Skip to content

Commit

Permalink
Merge pull request #7684 from AndrolGenhald/bugfix/minor-reference-fixes
Browse files Browse the repository at this point in the history
Fix some minor issues with references.
  • Loading branch information
orklah committed Feb 20, 2022
2 parents d7d846e + 8f710cc commit b8cda9e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Expand Up @@ -925,7 +925,7 @@ public static function analyzeAssignmentRef(
)
);
}
if (!empty($var_comments)) {
if (!empty($var_comments) && $var_comments[0]->type !== null && $var_comments[0]->var_id === null) {
IssueBuffer::maybeAdd(
new InvalidDocblock(
"Docblock type cannot be used for reference assignment",
Expand Down Expand Up @@ -990,7 +990,8 @@ public static function analyzeAssignmentRef(

$context->vars_in_scope[$lhs_var_id]->parent_nodes[$lhs_node->id] = $lhs_node;

if (($stmt->var instanceof ArrayDimFetch || $stmt->var instanceof PropertyFetch)
if ($statements_analyzer->data_flow_graph !== null
&& ($stmt->var instanceof ArrayDimFetch || $stmt->var instanceof PropertyFetch)
&& $stmt->var->var instanceof Variable
&& is_string($stmt->var->var->name)
) {
Expand All @@ -1006,6 +1007,15 @@ public static function analyzeAssignmentRef(
: "property-assignment-as-reference"
);
}

if ($root_var_id === '$this') {
// Variables on `$this` are always used
$statements_analyzer->data_flow_graph->addPath(
$lhs_node,
new DataFlowNode('variable-use', 'variable use', null),
'variable-use',
);
}
}

if ($stmt->var instanceof ArrayDimFetch) {
Expand Down
10 changes: 10 additions & 0 deletions tests/ReferenceTest.php
Expand Up @@ -247,6 +247,16 @@ function func(array &$a): void
',
'assertions' => [],
],
'allowDocblockTypingOtherVariable' => [
'code' => '<?php
$a = 1;
/** @var string $a */
$b = &$a;
',
'assertions' => [
'$b' => 'string',
],
],
];
}

Expand Down
14 changes: 14 additions & 0 deletions tests/UnusedVariableTest.php
Expand Up @@ -2587,6 +2587,20 @@ function takesAnInt(): void {
}
}',
],
'referenceInPropertyIsNotUnused' => [
'code' => '<?php
class Foo
{
/** @var int|null */
public $bar = null;
public function setBarRef(int $ref): void
{
$this->bar = &$ref;
}
}
',
],
];
}

Expand Down

0 comments on commit b8cda9e

Please sign in to comment.