Skip to content

Commit 9526140

Browse files
authored
Merge pull request #15 from hirokinoue/feature/revise-assignment-identification
Revise assignment identification
2 parents 38b6fe5 + 02a0399 commit 9526140

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

src/Parse/Visitor/FunctionLikeFindingVisitor.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Expr\Assign;
99
use PhpParser\Node\Expr\AssignOp;
10+
use PhpParser\Node\Expr\AssignRef;
1011
use PhpParser\Node\FunctionLike;
1112
use PhpParser\NodeVisitor\FindingVisitor;
1213
use PhpParser\Node\Stmt\Class_;
@@ -31,8 +32,10 @@ public function enterNode(Node $node) {
3132
// Record class name
3233
$this->currentNamespace = $node->name ? $node->name->name : null;
3334
}
34-
35-
if ($node instanceof Assign || $node instanceof AssignOp) {
35+
if ($node instanceof Assign ||
36+
$node instanceof AssignOp ||
37+
$node instanceof AssignRef
38+
) {
3639
$node->var->setAttribute('assigned', true); // Mark as assigned
3740
}
3841
if ($node instanceof FunctionLike) {

test/VariableParserTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testParseAssignOperator(): void
107107
$functions = $result->functions;
108108
$this->assertCount(1, $functions);
109109
$this->assertSame('assignFunction', $functions[0]->name);
110-
$this->assertCount(14, $functions[0]->getVariables());
110+
$this->assertCount(15, $functions[0]->getVariables());
111111

112112
$vars = $functions[0]->getVariables();
113113
$this->assertSame('num', $vars[0]->name);
@@ -152,6 +152,9 @@ public function testParseAssignOperator(): void
152152
$this->assertSame('num', $vars[13]->name);
153153
$this->assertSame(18, $vars[13]->lineNumber);
154154
$this->assertSame(true, $vars[13]->assigned);
155+
$this->assertSame('num', $vars[14]->name);
156+
$this->assertSame(19, $vars[14]->lineNumber);
157+
$this->assertSame(true, $vars[14]->assigned);
155158
}
156159

157160
public function testInterpolatedStringFunction(): void

test/fixtures/assign_operator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ function assignFunction(): void
1616
$num >>= 1; // 16行目
1717
$num .= 1; // 17行目
1818
$num ??= 1; // 18行目
19+
$num =& f(); // 19行目
1920
}

0 commit comments

Comments
 (0)