Skip to content

Commit 1fb092f

Browse files
committed
Assign each Stmt a separate branch (part 1)
1 parent 489c8ae commit 1fb092f

File tree

2 files changed

+472
-441
lines changed

2 files changed

+472
-441
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 110 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
2929
{
30-
private $nextBranch = 1;
30+
private $nextBranch = 0;
3131

3232
/**
3333
* @var string
@@ -50,17 +50,21 @@ public function enterNode(Node $node): void
5050
{
5151
if ($node instanceof Node\Stmt\Declare_ ||
5252
$node instanceof Node\Stmt\DeclareDeclare ||
53+
$node instanceof Node\Stmt\Else_ ||
5354
$node instanceof Node\Stmt\Interface_ ||
55+
$node instanceof Node\Stmt\Label ||
5456
$node instanceof Node\Stmt\Namespace_ ||
5557
$node instanceof Node\Stmt\Nop ||
58+
$node instanceof Node\Stmt\Switch_ ||
59+
$node instanceof Node\Stmt\TryCatch ||
5660
$node instanceof Node\Stmt\Use_ ||
5761
$node instanceof Node\Stmt\UseUse ||
5862
$node instanceof Node\Expr\Variable ||
63+
$node instanceof Node\Const_ ||
64+
$node instanceof Node\Identifier ||
5965
$node instanceof Node\Name ||
6066
$node instanceof Node\Param ||
61-
$node instanceof Node\Const_ ||
62-
$node instanceof Node\Scalar ||
63-
$node instanceof Node\Identifier
67+
$node instanceof Node\Scalar
6468
) {
6569
return;
6670
}
@@ -120,21 +124,21 @@ public function enterNode(Node $node): void
120124
return;
121125
}
122126

123-
if ($node instanceof Node\Expr\Closure &&
124-
$node->getEndLine() === $node->getStartLine()
125-
) {
126-
return;
127-
}
128-
129-
$branch = ++$this->nextBranch;
130-
131-
foreach ($node->stmts as $stmt) {
132-
if ($stmt instanceof Node\Stmt\Nop) {
133-
continue;
134-
}
135-
136-
$this->setLineBranch($stmt->getStartLine(), $stmt->getEndLine(), $branch);
137-
}
127+
// if ($node instanceof Node\Expr\Closure &&
128+
// $node->getEndLine() === $node->getStartLine()
129+
// ) {
130+
// return;
131+
// }
132+
//
133+
// $branch = ++$this->nextBranch;
134+
//
135+
// foreach ($node->stmts as $stmt) {
136+
// if ($stmt instanceof Node\Stmt\Nop) {
137+
// continue;
138+
// }
139+
//
140+
// $this->setLineBranch($stmt->getStartLine(), $stmt->getEndLine(), $branch);
141+
// }
138142

139143
return;
140144
}
@@ -163,87 +167,77 @@ public function enterNode(Node $node): void
163167

164168
if ($node instanceof Node\Stmt\If_ ||
165169
$node instanceof Node\Stmt\ElseIf_ ||
166-
$node instanceof Node\Stmt\Else_ ||
167-
$node instanceof Node\Stmt\Case_ ||
168-
$node instanceof Node\Stmt\For_ ||
169-
$node instanceof Node\Stmt\Foreach_ ||
170-
$node instanceof Node\Stmt\While_ ||
171-
$node instanceof Node\Stmt\TryCatch ||
172-
$node instanceof Node\Stmt\Catch_ ||
173-
$node instanceof Node\Stmt\Finally_
170+
$node instanceof Node\Stmt\Case_
174171
) {
175-
$incrementNextBranch = false;
176-
177-
if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
178-
$stmtBranch = 1 + $this->executableLinesGroupedByBranch[$node->getStartLine()];
179-
180-
if (false !== array_search($stmtBranch, $this->executableLinesGroupedByBranch, true)) {
181-
$stmtBranch = 1 + $this->nextBranch;
182-
$incrementNextBranch = true;
183-
}
184-
} else {
185-
$stmtBranch = 1 + $this->nextBranch;
186-
$incrementNextBranch = true;
172+
if (null === $node->cond) {
173+
return;
187174
}
188175

189-
$endLine = $node->getEndLine();
176+
$this->setLineBranch(
177+
$node->cond->getStartLine(),
178+
$node->cond->getStartLine(),
179+
++$this->nextBranch
180+
);
181+
182+
return;
183+
}
190184

191-
if ($node instanceof Node\Stmt\If_) {
192-
if ([] !== $node->elseifs) {
193-
$endLine = $node->elseifs[0]->getStartLine();
194-
} elseif (null !== $node->else) {
195-
$endLine = $node->else->getStartLine();
185+
if ($node instanceof Node\Stmt\For_) {
186+
$startLine = null;
187+
$endLine = null;
188+
if ([] !== $node->init) {
189+
$startLine = $node->init[0]->getStartLine();
190+
end($node->init);
191+
$endLine = current($node->init)->getEndLine();
192+
reset($node->init);
193+
}
194+
if ([] !== $node->cond) {
195+
if (null === $startLine) {
196+
$startLine = $node->cond[0]->getStartLine();
196197
}
198+
end($node->cond);
199+
$endLine = current($node->cond)->getEndLine();
200+
reset($node->cond);
197201
}
198-
199-
if (!isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
200-
$this->setLineBranch($node->getStartLine(), $endLine, 1);
202+
if ([] !== $node->loop) {
203+
if (null === $startLine) {
204+
$startLine = $node->loop[0]->getStartLine();
205+
}
206+
end($node->loop);
207+
$endLine = current($node->loop)->getEndLine();
208+
reset($node->loop);
201209
}
202-
203-
if ([] === $node->stmts) {
210+
211+
if (null === $startLine || null === $endLine) {
204212
return;
205213
}
206214

207-
$contentStart = max(
208-
$node->getStartLine() + 1,
209-
$node->stmts[0]->getStartLine()
215+
$this->setLineBranch(
216+
$startLine,
217+
$endLine,
218+
++$this->nextBranch
210219
);
211-
$contentEnd = $endLine;
212220

213-
if (
214-
$node instanceof Node\Stmt\Catch_ ||
215-
$node instanceof Node\Stmt\Finally_
216-
) {
217-
$contentStart = $node->getStartLine();
218-
}
219-
220-
if ($node instanceof Node\Stmt\Case_) {
221-
$contentEnd++;
222-
}
223-
224-
end($node->stmts);
225-
$lastNode = current($node->stmts);
226-
reset($node->stmts);
227-
228-
if (
229-
$lastNode instanceof Node\Stmt\Nop ||
230-
$lastNode instanceof Node\Stmt\Break_
231-
) {
232-
$contentEnd = $lastNode->getEndLine() + 1;
233-
}
221+
return;
222+
}
234223

235-
if (1 > ($contentEnd - $contentStart)) {
236-
return;
237-
}
224+
if ($node instanceof Node\Stmt\Foreach_) {
225+
$this->setLineBranch(
226+
$node->expr->getStartLine(),
227+
$node->valueVar->getEndLine(),
228+
++$this->nextBranch
229+
);
238230

239-
if ($incrementNextBranch) {
240-
$this->nextBranch++;
241-
}
231+
return;
232+
}
242233

234+
if ($node instanceof Node\Stmt\While_ ||
235+
$node instanceof Node\Stmt\Do_
236+
) {
243237
$this->setLineBranch(
244-
$contentStart,
245-
$contentEnd - 1,
246-
$stmtBranch
238+
$node->cond->getStartLine(),
239+
$node->cond->getEndLine(),
240+
++$this->nextBranch
247241
);
248242

249243
return;
@@ -261,43 +255,43 @@ public function enterNode(Node $node): void
261255
return;
262256
}
263257

264-
if (
265-
$node instanceof Node\Stmt\Return_ ||
266-
$node instanceof Node\Stmt\Continue_ ||
267-
$node instanceof Node\Stmt\Break_ ||
268-
$node instanceof Node\Stmt\Goto_ ||
269-
$node instanceof Node\Stmt\Throw_ ||
270-
$node instanceof Node\Stmt\Label ||
271-
$node instanceof Node\Expr\CallLike
272-
) {
273-
$returnBranch = $this->executableLinesGroupedByBranch[$node->getStartLine()];
274-
$returnEndLine = $node->getEndLine();
275-
$nextBranch = null;
276-
277-
if ($node instanceof Node\Stmt\Label) {
278-
$returnEndLine = $node->getStartLine() - 1;
279-
}
280-
281-
foreach ($this->executableLinesGroupedByBranch as $line => $branch) {
282-
if ($line <= $returnEndLine || $branch !== $returnBranch) {
283-
continue;
284-
}
285-
286-
if (null === $nextBranch) {
287-
$nextBranch = ++$this->nextBranch;
288-
}
289-
290-
$this->executableLinesGroupedByBranch[$line] = $nextBranch;
291-
}
292-
293-
return;
294-
}
258+
// if (
259+
// $node instanceof Node\Stmt\Return_ ||
260+
// $node instanceof Node\Stmt\Continue_ ||
261+
// $node instanceof Node\Stmt\Break_ ||
262+
// $node instanceof Node\Stmt\Goto_ ||
263+
// $node instanceof Node\Stmt\Throw_ ||
264+
// $node instanceof Node\Stmt\Label ||
265+
// $node instanceof Node\Expr\CallLike
266+
// ) {
267+
// $returnBranch = $this->executableLinesGroupedByBranch[$node->getStartLine()];
268+
// $returnEndLine = $node->getEndLine();
269+
// $nextBranch = null;
270+
//
271+
// if ($node instanceof Node\Stmt\Label) {
272+
// $returnEndLine = $node->getStartLine() - 1;
273+
// }
274+
//
275+
// foreach ($this->executableLinesGroupedByBranch as $line => $branch) {
276+
// if ($line <= $returnEndLine || $branch !== $returnBranch) {
277+
// continue;
278+
// }
279+
//
280+
// if (null === $nextBranch) {
281+
// $nextBranch = ++$this->nextBranch;
282+
// }
283+
//
284+
// $this->executableLinesGroupedByBranch[$line] = $nextBranch;
285+
// }
286+
//
287+
// return;
288+
// }
295289

296290
if (isset($this->executableLinesGroupedByBranch[$node->getStartLine()])) {
297291
return;
298292
}
299293

300-
$this->setLineBranch($node->getStartLine(), $node->getEndLine(), 1);
294+
$this->setLineBranch($node->getStartLine(), $node->getEndLine(), ++$this->nextBranch);
301295
}
302296

303297
public function afterTraverse(array $nodes): void

0 commit comments

Comments
 (0)