Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion plugin/codecov/src/Internal/Driver/UncountedLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static function scan(string $path): array|false

# A last line that only closes, such as `}` or `);`, carries the top-level `return`, and code only when
# it ends a function body that falls through.
if (isset($code[$last]) && $onlyClosers && !isset(ExecutableLines::of($source)[$last])) {
if (isset($code[$last]) && $onlyClosers && !isset(ExecutableLines::bodyEnds($source)[$last])) {
unset($code[$last]);
}

Expand Down
1,089 changes: 972 additions & 117 deletions plugin/codecov/src/Internal/ExecutableLines.php

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions plugin/codecov/tests/Fixture/ExecutableLines/hooks.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ final class HookedProperties
+ $this->b;
}

public \Closure $maker {
get => fn(): int => 1;
}

public int $withDefault = 3 {
set(int $v) {
$this->withDefault = $v * 2;
Expand Down
272 changes: 272 additions & 0 deletions plugin/codecov/tests/Fixture/ExecutableLines/operands.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
<?php
namespace App;

use Lib\{function helper};

final class Operands
{
private const A = 'a';
private const B = 'b';

public function __construct(
private string $name,
) {}

public function glued(): string
{
return 'a' . "\n"
. 'b' . "\n"
. 'c';
}

public function gluedVariable(string $x): string
{
$sql = 'SELECT * '
. 'FROM t '
. 'WHERE x = ' . $x;
return $sql;
}

public function condition(int $a, int $b): int
{
if (
$a > 0
&& $b > 0
) {
return 1;
}
return 0;
}

public function format(string $a, string $b): string
{
return \sprintf(
'%s %s',
$a,
$b,
);
}

public function arguments(object $o, string $x): void
{
$o->run(
'literal',
self::A,
$x,
name: true,
);
$this->own(
'literal',
$x,
);
\in_array(
$x,
[self::A, self::B],
true,
);
}

private function own(string $a, string $b): void {}

public function ternary(bool $flag): string
{
$value = $flag
? 'yes'
: 'no';
return $value;
}

public function cases(string $rule): int
{
switch ($rule) {
case self::A:
return 1;
case self::B:
return 2;
}
return 0;
}

public function exhaustive(int $x): int
{
switch ($x) {
case 1:
return 1;
default:
return 0;
}
}

public function unreachable(?int $x): int
{
$y = $x ?? throw new \LogicException('none');
return $y;
}

public function afterReturn(): \Generator
{
return;
yield;
}

public function constantCondition(object $o): int
{
if (\PHP_INT_SIZE < 4
|| $o->has('x')) {
return 1;
}
return 0;
}

public function endless(object $o): void
{
while (true) {
$o->tick();
}
}

public function elseIf(bool $a, bool $b): int
{
if ($a) {
return 1;
} else if ($b) {
return 2;
}
return 3;
}

public function strings(string $t, string $a, string $b): string
{
$sql = "SELECT *
FROM {$t}
WHERE id = ?";
$list = "$a,$b"
. $t;
return $sql . $list;
}

public function longTarget(string $value): void
{
$this->name
= $value;
}

public function arrowDefault(): \Closure
{
return fn(
int $x = 0,
) => $x;
}

public function versioned(bool $x): int
{
if (\PHP_VERSION_ID < 80400 && $x) {
return \strlen('old');
}
return 0;
}

public function imported(): void
{
helper(
'literal',
);
}

public function revived(int $x): int
{
goto done;
$x++;
done:
return $x;
}

public function breakInBlock(array $items): int
{
foreach ($items as $item) {
if ($item) {
break;
}
}
return 0;
}

public function constantOperand(object $o): bool
{
$ok = \PHP_INT_SIZE < 4
|| $o->has('x');
return $ok;
}

public function closures(): array
{
$arrow = fn() => $this->own(
'literal',
'other',
);
$closure = function () {
$this->own(
'literal',
'other',
);
};
return [$arrow, $closure];
}

public function constantLoop(): int
{
$n = 0;
while (self::A === 'b') {
$n++;
}
return $n;
}

public function operands(object $o, string $a, string $b): string
{
$x = $o->wrap(
$a,
) . 'x';
$y = !($a) . ($b);
$z = $a . "v{$b}";
$o->{$a} = 1;
$w = $o
->prop;
return $x . $y . $z . $w;
}

public function lateConstants(): array
{
return [
static::A,
$this::B,
];
}

public function versionChains(object $o): int
{
if (\PHP_INT_SIZE > 100) $o->a();
if (\PHP_INT_SIZE > 100) {
$o->b();
} elseif ($o->c()) {
$o->d();
}
if (\PHP_INT_SIZE > 100) {
$o->e();
} else if ($o->f()) {
$o->g();
}
if (\PHP_INT_SIZE > 100) {
$o->h();
} else {
$o->i();
}
if (\PHP_INT_SIZE > 100) {
$o->j();
} else $o->k();
if (\PHP_INT_SIZE > 100):
$o->l();
endif;
return 0;
}
}
2 changes: 2 additions & 0 deletions plugin/codecov/tests/Unit/Driver/UncountedLinesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ public function removeDropsBranchesOnIgnoredLinesWithTheirPaths(): void
#[DataSet(["<?php\n\nfunction fail(): never\n{\n throw new \\LogicException('x');\n}", [5 => 1, 6 => 1], [5 => 1]], 'function ending in throw, Xdebug')]
#[DataSet(["<?php\n\nfunction pick(int \$x): int\n{\n if (\$x) {\n return 1;\n } else {\n return 0;\n }\n}", [5 => 1, 6 => 1, 8 => -1, 10 => 1], [5 => 1, 6 => 1, 8 => -1]], 'function whose every branch returns, Xdebug')]
#[DataSet(["<?php\n\nfunction pick(int \$x): int\n{\n if (\$x) {\n return 1;\n }\n \$y = 0;\n}", [5 => 1, 6 => 1, 8 => -1, 9 => -1], [5 => 1, 6 => 1, 8 => -1, 9 => -1]], 'function falling through past an if, PCOV')]
#[DataSet(["<?php\n\nfunction pick(\$a, \$b): void\n{\n if (\$a) {\n return;\n } else if (\$b) {\n return;\n }\n}", [5 => 1, 6 => 1, 7 => -1, 8 => -1, 10 => 1], [5 => 1, 6 => 1, 7 => -1, 8 => -1, 10 => 1]], 'function falling through past an else if, PCOV')]
#[DataSet(["<?php\n\nfunction need(?int \$a): void\n{\n \$y = \$a ?? throw new \\LogicException();\n \\usleep(\$y);\n}", [5 => 1, 6 => 1, 7 => 1], [5 => 1, 6 => 1, 7 => 1]], 'function falling through past ?? throw, PCOV')]
#[DataSet(["<?php\n\n\$g = function (): void {\n \$x = 1;\n};", [3 => -1, 4 => 1, 5 => 1], [3 => -1, 4 => 1, 5 => 1]], 'closure falling through, PCOV')]
#[DataSet(["<?php\n\n\$x = 1;\nif (\$x) {\n \$y = 2;\n}", [3 => 1, 4 => 1, 5 => 1, 6 => 1], [3 => 1, 4 => 1, 5 => 1]], 'top-level if, Xdebug')]
#[DataSet(["<?php\n\nfunction one(): int\n{\n return 1;\n}\n\none();\n?>", [5 => -1, 8 => -1, 9 => -1], [5 => -1, 8 => -1]], 'closing tag, PCOV')]
Expand Down
Loading
Loading