27
27
*/
28
28
final class ExecutableLinesFindingVisitor extends NodeVisitorAbstract
29
29
{
30
- private $ nextBranch = 1 ;
30
+ private $ nextBranch = 0 ;
31
31
32
32
/**
33
33
* @var string
@@ -50,17 +50,21 @@ public function enterNode(Node $node): void
50
50
{
51
51
if ($ node instanceof Node \Stmt \Declare_ ||
52
52
$ node instanceof Node \Stmt \DeclareDeclare ||
53
+ $ node instanceof Node \Stmt \Else_ ||
53
54
$ node instanceof Node \Stmt \Interface_ ||
55
+ $ node instanceof Node \Stmt \Label ||
54
56
$ node instanceof Node \Stmt \Namespace_ ||
55
57
$ node instanceof Node \Stmt \Nop ||
58
+ $ node instanceof Node \Stmt \Switch_ ||
59
+ $ node instanceof Node \Stmt \TryCatch ||
56
60
$ node instanceof Node \Stmt \Use_ ||
57
61
$ node instanceof Node \Stmt \UseUse ||
58
62
$ node instanceof Node \Expr \Variable ||
63
+ $ node instanceof Node \Const_ ||
64
+ $ node instanceof Node \Identifier ||
59
65
$ node instanceof Node \Name ||
60
66
$ node instanceof Node \Param ||
61
- $ node instanceof Node \Const_ ||
62
- $ node instanceof Node \Scalar ||
63
- $ node instanceof Node \Identifier
67
+ $ node instanceof Node \Scalar
64
68
) {
65
69
return ;
66
70
}
@@ -120,21 +124,21 @@ public function enterNode(Node $node): void
120
124
return ;
121
125
}
122
126
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
+ // }
138
142
139
143
return ;
140
144
}
@@ -163,87 +167,77 @@ public function enterNode(Node $node): void
163
167
164
168
if ($ node instanceof Node \Stmt \If_ ||
165
169
$ 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_
174
171
) {
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 ;
187
174
}
188
175
189
- $ endLine = $ node ->getEndLine ();
176
+ $ this ->setLineBranch (
177
+ $ node ->cond ->getStartLine (),
178
+ $ node ->cond ->getStartLine (),
179
+ ++$ this ->nextBranch
180
+ );
181
+
182
+ return ;
183
+ }
190
184
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 ();
196
197
}
198
+ end ($ node ->cond );
199
+ $ endLine = current ($ node ->cond )->getEndLine ();
200
+ reset ($ node ->cond );
197
201
}
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 );
201
209
}
202
-
203
- if ([] === $ node -> stmts ) {
210
+
211
+ if (null === $ startLine || null === $ endLine ) {
204
212
return ;
205
213
}
206
214
207
- $ contentStart = max (
208
- $ node ->getStartLine () + 1 ,
209
- $ node ->stmts [0 ]->getStartLine ()
215
+ $ this ->setLineBranch (
216
+ $ startLine ,
217
+ $ endLine ,
218
+ ++$ this ->nextBranch
210
219
);
211
- $ contentEnd = $ endLine ;
212
220
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
+ }
234
223
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
+ );
238
230
239
- if ($ incrementNextBranch ) {
240
- $ this ->nextBranch ++;
241
- }
231
+ return ;
232
+ }
242
233
234
+ if ($ node instanceof Node \Stmt \While_ ||
235
+ $ node instanceof Node \Stmt \Do_
236
+ ) {
243
237
$ this ->setLineBranch (
244
- $ contentStart ,
245
- $ contentEnd - 1 ,
246
- $ stmtBranch
238
+ $ node -> cond -> getStartLine () ,
239
+ $ node -> cond -> getEndLine () ,
240
+ ++ $ this -> nextBranch
247
241
);
248
242
249
243
return ;
@@ -261,43 +255,43 @@ public function enterNode(Node $node): void
261
255
return ;
262
256
}
263
257
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
+ // }
295
289
296
290
if (isset ($ this ->executableLinesGroupedByBranch [$ node ->getStartLine ()])) {
297
291
return ;
298
292
}
299
293
300
- $ this ->setLineBranch ($ node ->getStartLine (), $ node ->getEndLine (), 1 );
294
+ $ this ->setLineBranch ($ node ->getStartLine (), $ node ->getEndLine (), ++ $ this -> nextBranch );
301
295
}
302
296
303
297
public function afterTraverse (array $ nodes ): void
0 commit comments