Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
0c33d75
Move to branch based line analysis
Slamdunk Nov 21, 2022
9c508c3
Corrected `elseif`s conditions
Slamdunk Nov 24, 2022
df986ed
Added `for` and `do-while`
Slamdunk Nov 24, 2022
aff7caf
Added `switch`
Slamdunk Nov 24, 2022
7afb5e6
Added `match`
Slamdunk Nov 24, 2022
ee5db56
Added `return`
Slamdunk Nov 24, 2022
c797803
Added `continue` and `break`
Slamdunk Nov 24, 2022
74f9b32
Added `goto`
Slamdunk Nov 24, 2022
cfdfd5f
Added `try-catch-finally`
Slamdunk Nov 25, 2022
d3d30da
Added `ternary`
Slamdunk Nov 25, 2022
270211a
Added `call-like`
Slamdunk Nov 25, 2022
3337628
Handled empty functions
Slamdunk Nov 28, 2022
e03acf2
Added anonymous functions
Slamdunk Nov 28, 2022
e8af3e1
Added arrow functions
Slamdunk Nov 28, 2022
a6cef72
If condition to return statement
Slamdunk Nov 28, 2022
358256f
Simplified matching of node to be skipped
Slamdunk Nov 28, 2022
fb6e6c2
Functions/Methods: mark as lines only body's ones
Slamdunk Nov 28, 2022
7606d7c
Handle comments and empty lines
Slamdunk Nov 29, 2022
cd4f39d
CodeCoverage: mark as executed all lines of the same branch
Slamdunk Nov 29, 2022
80697da
Added `interface` and `trait`
Slamdunk Nov 29, 2022
57dfea6
Added short ternary and coalesce
Slamdunk Nov 29, 2022
489c8ae
Exclude in-toto non ClassMethods lines from class analysis
Slamdunk Nov 29, 2022
7d6b533
Assign each Stmt a separate branch
Slamdunk Nov 29, 2022
c113904
`match` arms depent too much on autoloading runtime behavior, process…
Slamdunk Nov 30, 2022
5c199ae
Lines ignored by annotations: cover entire range
Slamdunk Nov 30, 2022
8fccb72
Rely only on ExecutableLinesFindingVisitorTest unit test for exec lin…
Slamdunk Nov 30, 2022
ab90d33
Tests different features only for related supported by PHP versions
Slamdunk Nov 30, 2022
9e61b30
SA fixes
Slamdunk Nov 30, 2022
d036036
Adapt missing test cases for PHP <8.1
Slamdunk Nov 30, 2022
5243a2a
Minor optimization
Slamdunk Dec 1, 2022
1eb363a
Multiline strings: skip internal lines
Slamdunk Dec 2, 2022
d909a64
Skip comments within nodes already marked as exec
Slamdunk Dec 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Functions/Methods: mark as lines only body's ones
  • Loading branch information
Slamdunk committed Nov 28, 2022
commit fb6e6c2f41a9059ef3a655cebb930bb0297b8ca0
38 changes: 16 additions & 22 deletions src/StaticAnalysis/ExecutableLinesFindingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function enterNode(Node $node): void
$node instanceof Node\Stmt\ClassConst ||
$node instanceof Node\Stmt\Property ||
$node instanceof Node\Stmt\PropertyProperty ||
$node instanceof Node\Expr\Variable ||
$node instanceof Node\Param ||
$node instanceof Node\Const_ ||
$node instanceof Node\Scalar ||
$node instanceof Node\Identifier ||
Expand Down Expand Up @@ -67,33 +69,25 @@ public function enterNode(Node $node): void
$node instanceof Node\Stmt\ClassMethod ||
$node instanceof Node\Expr\Closure
) {
$startLine = $node->getStartLine();

if ($node instanceof Node\Expr\Closure) {
if ([] === $node->stmts ||
(
1 === count($node->stmts) &&
$node->stmts[0] instanceof Node\Stmt\Nop
)
) {
$startLine = $node->getEndLine();

if ($startLine === $node->getStartLine()) {
return;
}
} else {
$startLine = $node->stmts[0]->getStartLine();
$hasEmptyBody = [] === $node->stmts ||
(
1 === count($node->stmts) &&
$node->stmts[0] instanceof Node\Stmt\Nop
);

if ($hasEmptyBody) {
$startLine = $node->getEndLine();

if ($startLine === $node->getStartLine()) {
return;
}
} else {
$startLine = $node->stmts[0]->getStartLine();
}

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

if ([] === $node->stmts ||
(
1 === count($node->stmts) &&
$node->stmts[0] instanceof Node\Stmt\Nop
)
) {
if ($hasEmptyBody) {
$endLine++;
}

Expand Down
149 changes: 75 additions & 74 deletions tests/_files/source_for_branched_exec_lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
1 // 0
; // 0

function empty1() // +1
{ // 0
} // 0
function empty2(){ // +1
} // 0
function empty1()
{
} // +1
function empty2(){
} // +1

function simple1() // +1
{ // 0
return 1; // 0
function simple1()
{
return 1; // +1
}
function simple2(){ // +1
return 1; // 0
function simple2(){
return 1; // +1
}

$var2 = 1; // -4
Expand All @@ -33,9 +33,9 @@ function simple2(){ // +1
$var2 += 1; // +5
} // -5

function withIf() // +6
{ // 0
$var = 1; // 0
function withIf()
{
$var = 1; // +6
if (false) { // 0
$var += 2; // +1
} // -1
Expand All @@ -46,26 +46,27 @@ class MyClass
{
public const C1 = 1;
public $var1 = 1;
public // +2
function // 0
__construct // 0
( // 0
$var // 0
= // 0
1 // 0
) // 0
{ // 0
$var = 1; // 0
public
function
__construct
(
&
$var
=
1
)
{
$var = 1; // +2
if (false) { // 0
$var += 2; // +1
} // -1
}
public function myEmpty() // +2
{ // 0
} // 0
public function withForeach() // +1
{ // 0
$var = 1; // 0
public function myEmpty()
{
} // +2
public function withForeach()
{
$var = 1; // +1
foreach ([] as $value); // 0
foreach ([] as $value) $var += 2; // 0
foreach ([] as $value) { // 0
Expand All @@ -88,9 +89,9 @@ public function withForeach() // +1
$var += 2; // +4
} // -4
}
public function withWhile() // +5
{ // 0
$var = 1; // 0
public function withWhile()
{
$var = 1; // +5
while (0 === $var); // 0
while (0 === $var) ++$var; // 0
while (0 === $var) { // 0
Expand All @@ -111,9 +112,9 @@ public function withWhile() // +5
++$var; // +4
} // -4
}
public function withIfElseifElse() // +5
{ // 0
$var = 1; // 0
public function withIfElseifElse()
{
$var = 1; // +5
if (0 === $var); // 0
if (0 === $var) { ++$var; } // 0
if (1 === $var): // 0
Expand Down Expand Up @@ -155,9 +156,9 @@ public function withIfElseifElse() // +5
++$var; // +12
} // -12
}
public function withFor() // +13
{ // 0
$var = 1; // 0
public function withFor()
{
$var = 1; // +13
for (;false;); // 0
for (;false;) $var += 2; // 0
for (;false;) { // 0
Expand All @@ -178,9 +179,9 @@ public function withFor() // +13
$var += 2; // +4
} // -4
}
public function withDoWhile() // +5
{ // 0
$var = 1; // 0
public function withDoWhile()
{
$var = 1; // +5
do {} while (0 === $var); // 0
do ++$var; while (0 === $var); // 0
do // 0
Expand All @@ -203,9 +204,9 @@ public function withDoWhile() // +5
) // 0
; // 0
}
public function withSwitch() // +1
{ // 0
$var = 1; // 0
public function withSwitch()
{
$var = 1; // +1
switch ($var) { // 0
case 0: // 0
case 1: // 0
Expand Down Expand Up @@ -233,9 +234,9 @@ public function withSwitch() // +1
++$var; // +8
endswitch; // -8
}
public function withMatch() // +9
{ // 0
$var = 1; // 0
public function withMatch()
{
$var = 1; // +9
$var2 = match ($var) { // 0
0 => ++$var, // +1
1 => ++$var, // +1
Expand Down Expand Up @@ -263,9 +264,9 @@ public function withMatch() // +9
} // 0
; // 0
}
public function withReturn() // +7
{ // 0
$var = 1; // 0
public function withReturn()
{
$var = 1; // +7
if (false) { // 0
++$var; // +1
return // 0
Expand All @@ -279,9 +280,9 @@ public function withReturn() // +7
return; // 0
++$var; // +4
}
public function withContinue() // +1
{ // 0
$var = 1; // 0
public function withContinue()
{
$var = 1; // +1
for ($i = 0; $i < 10; $i++) { // 0
if (false) { // +1
++$var; // +1
Expand All @@ -295,9 +296,9 @@ public function withContinue() // +1
++$var; // +3
} // -4
}
public function withBreak() // +5
{ // 0
$var = 1; // 0
public function withBreak()
{
$var = 1; // +5
for ($i = 0; $i < 10; $i++) { // 0
if (false) { // +1
++$var; // +1
Expand All @@ -311,9 +312,9 @@ public function withBreak() // +5
++$var; // +3
} // -4
}
public function withGoto() // +5
{ // 0
$var = 1; // 0
public function withGoto()
{
$var = 1; // +5
if (false) { // 0
++$var; // +1
goto // 0
Expand All @@ -328,9 +329,9 @@ public function withGoto() // +5
b: // +1
++$var; // 0
}
public function withThrow() // +1
{ // 0
$var = 1; // 0
public function withThrow()
{
$var = 1; // +1
try { // 0
++$var; // +1
throw // 0
Expand All @@ -348,9 +349,9 @@ public function withThrow() // +1
} // -7
++$var; // 0
}
public function withTernaryOperator() // +8
{ // 0
$var // 0
public function withTernaryOperator()
{
$var // +8
= // 0
true // 0
? // 0
Expand All @@ -359,9 +360,9 @@ public function withTernaryOperator() // +8
'b' // +2
; // -2
}
public function withCall() // +3
{ // 0
$var = 1; // 0
public function withCall()
{
$var = 1; // +3
$var = intval($var); // 0
++$var; // +1
$date = new DateTimeImmutable(); // 0
Expand All @@ -373,9 +374,9 @@ public function withCall() // +3
$date = DateTime::createFromImmutable($date); // 0
++$var; // +1
}
public function withClosure() // +1
{ // 0
$myf = function(){}; // 0
public function withClosure()
{
$myf = function(){}; // +1
$myf = function(){ // 0
}; // +1
$myf = function() // -1
Expand Down Expand Up @@ -408,9 +409,9 @@ function // 0
}; // +5
$myf = function(){ $var = 1;}; // -5
}
public function withArrowFn() // +6
{ // 0
$y = 1; // 0
public function withArrowFn()
{
$y = 1; // +6
$fn1 = fn($x) => $x + $y; // 0
$fn1 = fn($x) => // 0
$x + $y; // +1
Expand Down