Skip to content

Commit 8c1c66e

Browse files
Slamdunksebastianbergmann
authored andcommitted
Functions/Methods: mark as lines only body's ones
1 parent 0a19874 commit 8c1c66e

File tree

2 files changed

+91
-96
lines changed

2 files changed

+91
-96
lines changed

src/StaticAnalysis/ExecutableLinesFindingVisitor.php

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function enterNode(Node $node): void
3939
$node instanceof Node\Stmt\ClassConst ||
4040
$node instanceof Node\Stmt\Property ||
4141
$node instanceof Node\Stmt\PropertyProperty ||
42+
$node instanceof Node\Expr\Variable ||
43+
$node instanceof Node\Param ||
4244
$node instanceof Node\Const_ ||
4345
$node instanceof Node\Scalar ||
4446
$node instanceof Node\Identifier ||
@@ -67,33 +69,25 @@ public function enterNode(Node $node): void
6769
$node instanceof Node\Stmt\ClassMethod ||
6870
$node instanceof Node\Expr\Closure
6971
) {
70-
$startLine = $node->getStartLine();
71-
72-
if ($node instanceof Node\Expr\Closure) {
73-
if ([] === $node->stmts ||
74-
(
75-
1 === count($node->stmts) &&
76-
$node->stmts[0] instanceof Node\Stmt\Nop
77-
)
78-
) {
79-
$startLine = $node->getEndLine();
80-
81-
if ($startLine === $node->getStartLine()) {
82-
return;
83-
}
84-
} else {
85-
$startLine = $node->stmts[0]->getStartLine();
72+
$hasEmptyBody = [] === $node->stmts ||
73+
(
74+
1 === count($node->stmts) &&
75+
$node->stmts[0] instanceof Node\Stmt\Nop
76+
);
77+
78+
if ($hasEmptyBody) {
79+
$startLine = $node->getEndLine();
80+
81+
if ($startLine === $node->getStartLine()) {
82+
return;
8683
}
84+
} else {
85+
$startLine = $node->stmts[0]->getStartLine();
8786
}
8887

8988
$endLine = $node->getEndLine() - 1;
9089

91-
if ([] === $node->stmts ||
92-
(
93-
1 === count($node->stmts) &&
94-
$node->stmts[0] instanceof Node\Stmt\Nop
95-
)
96-
) {
90+
if ($hasEmptyBody) {
9791
$endLine++;
9892
}
9993

tests/_files/source_for_branched_exec_lines.php

Lines changed: 75 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
1 // 0
1111
; // 0
1212

13-
function empty1() // +1
14-
{ // 0
15-
} // 0
16-
function empty2(){ // +1
17-
} // 0
13+
function empty1()
14+
{
15+
} // +1
16+
function empty2(){
17+
} // +1
1818

19-
function simple1() // +1
20-
{ // 0
21-
return 1; // 0
19+
function simple1()
20+
{
21+
return 1; // +1
2222
}
23-
function simple2(){ // +1
24-
return 1; // 0
23+
function simple2(){
24+
return 1; // +1
2525
}
2626

2727
$var2 = 1; // -4
@@ -33,9 +33,9 @@ function simple2(){ // +1
3333
$var2 += 1; // +5
3434
} // -5
3535

36-
function withIf() // +6
37-
{ // 0
38-
$var = 1; // 0
36+
function withIf()
37+
{
38+
$var = 1; // +6
3939
if (false) { // 0
4040
$var += 2; // +1
4141
} // -1
@@ -46,26 +46,27 @@ class MyClass
4646
{
4747
public const C1 = 1;
4848
public $var1 = 1;
49-
public // +2
50-
function // 0
51-
__construct // 0
52-
( // 0
53-
$var // 0
54-
= // 0
55-
1 // 0
56-
) // 0
57-
{ // 0
58-
$var = 1; // 0
49+
public
50+
function
51+
__construct
52+
(
53+
&
54+
$var
55+
=
56+
1
57+
)
58+
{
59+
$var = 1; // +2
5960
if (false) { // 0
6061
$var += 2; // +1
6162
} // -1
6263
}
63-
public function myEmpty() // +2
64-
{ // 0
65-
} // 0
66-
public function withForeach() // +1
67-
{ // 0
68-
$var = 1; // 0
64+
public function myEmpty()
65+
{
66+
} // +2
67+
public function withForeach()
68+
{
69+
$var = 1; // +1
6970
foreach ([] as $value); // 0
7071
foreach ([] as $value) $var += 2; // 0
7172
foreach ([] as $value) { // 0
@@ -88,9 +89,9 @@ public function withForeach() // +1
8889
$var += 2; // +4
8990
} // -4
9091
}
91-
public function withWhile() // +5
92-
{ // 0
93-
$var = 1; // 0
92+
public function withWhile()
93+
{
94+
$var = 1; // +5
9495
while (0 === $var); // 0
9596
while (0 === $var) ++$var; // 0
9697
while (0 === $var) { // 0
@@ -111,9 +112,9 @@ public function withWhile() // +5
111112
++$var; // +4
112113
} // -4
113114
}
114-
public function withIfElseifElse() // +5
115-
{ // 0
116-
$var = 1; // 0
115+
public function withIfElseifElse()
116+
{
117+
$var = 1; // +5
117118
if (0 === $var); // 0
118119
if (0 === $var) { ++$var; } // 0
119120
if (1 === $var): // 0
@@ -155,9 +156,9 @@ public function withIfElseifElse() // +5
155156
++$var; // +12
156157
} // -12
157158
}
158-
public function withFor() // +13
159-
{ // 0
160-
$var = 1; // 0
159+
public function withFor()
160+
{
161+
$var = 1; // +13
161162
for (;false;); // 0
162163
for (;false;) $var += 2; // 0
163164
for (;false;) { // 0
@@ -178,9 +179,9 @@ public function withFor() // +13
178179
$var += 2; // +4
179180
} // -4
180181
}
181-
public function withDoWhile() // +5
182-
{ // 0
183-
$var = 1; // 0
182+
public function withDoWhile()
183+
{
184+
$var = 1; // +5
184185
do {} while (0 === $var); // 0
185186
do ++$var; while (0 === $var); // 0
186187
do // 0
@@ -203,9 +204,9 @@ public function withDoWhile() // +5
203204
) // 0
204205
; // 0
205206
}
206-
public function withSwitch() // +1
207-
{ // 0
208-
$var = 1; // 0
207+
public function withSwitch()
208+
{
209+
$var = 1; // +1
209210
switch ($var) { // 0
210211
case 0: // 0
211212
case 1: // 0
@@ -233,9 +234,9 @@ public function withSwitch() // +1
233234
++$var; // +8
234235
endswitch; // -8
235236
}
236-
public function withMatch() // +9
237-
{ // 0
238-
$var = 1; // 0
237+
public function withMatch()
238+
{
239+
$var = 1; // +9
239240
$var2 = match ($var) { // 0
240241
0 => ++$var, // +1
241242
1 => ++$var, // +1
@@ -263,9 +264,9 @@ public function withMatch() // +9
263264
} // 0
264265
; // 0
265266
}
266-
public function withReturn() // +7
267-
{ // 0
268-
$var = 1; // 0
267+
public function withReturn()
268+
{
269+
$var = 1; // +7
269270
if (false) { // 0
270271
++$var; // +1
271272
return // 0
@@ -279,9 +280,9 @@ public function withReturn() // +7
279280
return; // 0
280281
++$var; // +4
281282
}
282-
public function withContinue() // +1
283-
{ // 0
284-
$var = 1; // 0
283+
public function withContinue()
284+
{
285+
$var = 1; // +1
285286
for ($i = 0; $i < 10; $i++) { // 0
286287
if (false) { // +1
287288
++$var; // +1
@@ -295,9 +296,9 @@ public function withContinue() // +1
295296
++$var; // +3
296297
} // -4
297298
}
298-
public function withBreak() // +5
299-
{ // 0
300-
$var = 1; // 0
299+
public function withBreak()
300+
{
301+
$var = 1; // +5
301302
for ($i = 0; $i < 10; $i++) { // 0
302303
if (false) { // +1
303304
++$var; // +1
@@ -311,9 +312,9 @@ public function withBreak() // +5
311312
++$var; // +3
312313
} // -4
313314
}
314-
public function withGoto() // +5
315-
{ // 0
316-
$var = 1; // 0
315+
public function withGoto()
316+
{
317+
$var = 1; // +5
317318
if (false) { // 0
318319
++$var; // +1
319320
goto // 0
@@ -328,9 +329,9 @@ public function withGoto() // +5
328329
b: // +1
329330
++$var; // 0
330331
}
331-
public function withThrow() // +1
332-
{ // 0
333-
$var = 1; // 0
332+
public function withThrow()
333+
{
334+
$var = 1; // +1
334335
try { // 0
335336
++$var; // +1
336337
throw // 0
@@ -348,9 +349,9 @@ public function withThrow() // +1
348349
} // -7
349350
++$var; // 0
350351
}
351-
public function withTernaryOperator() // +8
352-
{ // 0
353-
$var // 0
352+
public function withTernaryOperator()
353+
{
354+
$var // +8
354355
= // 0
355356
true // 0
356357
? // 0
@@ -359,9 +360,9 @@ public function withTernaryOperator() // +8
359360
'b' // +2
360361
; // -2
361362
}
362-
public function withCall() // +3
363-
{ // 0
364-
$var = 1; // 0
363+
public function withCall()
364+
{
365+
$var = 1; // +3
365366
$var = intval($var); // 0
366367
++$var; // +1
367368
$date = new DateTimeImmutable(); // 0
@@ -373,9 +374,9 @@ public function withCall() // +3
373374
$date = DateTime::createFromImmutable($date); // 0
374375
++$var; // +1
375376
}
376-
public function withClosure() // +1
377-
{ // 0
378-
$myf = function(){}; // 0
377+
public function withClosure()
378+
{
379+
$myf = function(){}; // +1
379380
$myf = function(){ // 0
380381
}; // +1
381382
$myf = function() // -1
@@ -408,9 +409,9 @@ function // 0
408409
}; // +5
409410
$myf = function(){ $var = 1;}; // -5
410411
}
411-
public function withArrowFn() // +6
412-
{ // 0
413-
$y = 1; // 0
412+
public function withArrowFn()
413+
{
414+
$y = 1; // +6
414415
$fn1 = fn($x) => $x + $y; // 0
415416
$fn1 = fn($x) => // 0
416417
$x + $y; // +1

0 commit comments

Comments
 (0)