Skip to content

Core\Debug::startCoverage

Core\Debug::startCoverage(bool $branches = false): voidCore\Debug::startCoverage(
    bool $branches = false,
): void

Starts counting how many times each line of the program runs. You read the counts with Core\Debug::coverage().

Counting only works when nvs.toml allows it. Add "coverage" to the mode list under [debug]. The nvs.toml beside the examples shows this. With this setting, counting starts when the program starts. Core\Debug::stopCoverage() stops it, and startCoverage starts it again. Each new start adds to the counts that are already there.

startCoverage(true) also counts each side of every if and loop condition. This needs "branch" in mode as well. coverage() returns only the line counts.

If mode does not include the setting a call needs, the call throws a RuntimeError. The message names the line to add. A server never counts lines, whatever the setting says.

The examples below count the lines of one method call, show the error when "branch" is missing, and start counting a second time.

Parameters

  • $branchesbooldefault falseWhen true, each side of every if and loop condition is counted too. coverage does not return these counts. The coverage report of nvs test shows them. [debug] mode must then also include "branch".

Return value

Returns void. Nothing. Counting goes on until stopCoverage.

Errors

  • RuntimeError[debug] mode in nvs.toml does not include the name this method needs.

Examples

The examples below run with this configuration file.

nvs.toml
# Allows the program to count which lines run. Counting starts when the program starts.
[debug]
mode = ["coverage"]

Count the lines of one call

<?nvs
// Shows how to count the lines that one method call runs.
class Basket
{
public static function total(array<int> $prices): int
{
int $sum = 0;
foreach ($prices as int $price) {
$sum += $price;
}
return $sum;
}
}
// The setting in `nvs.toml` starts counting when the program starts.
// This line stops it, so the lines above it are not counted.
Core\Debug::stopCoverage();
// `startCoverage` starts counting again. Each line that runs adds 1 to its count.
Core\Debug::startCoverage();
int $total = Basket::total([4, 5, 6]);
Core\Debug::stopCoverage();
echo 'total: ', $total, "\n";
// This prints each line that ran, and how many times it ran.
// Line 10 adds one price, so its count is 3.
foreach (Core\Debug::coverage() as string $path => array<int> $lines) {
echo Core\Path::fileName($path), "\n";
foreach ($lines as string $line => int $count) {
echo ' line ', $line, ': ', $count, "\n";
}
}
Output
total: 15
01-count-the-lines-of-one-call.nvs
line 8: 1
line 9: 3
line 10: 3
line 12: 1
line 18: 1
line 22: 1
line 23: 1

Branches need their own setting

<?nvs
// Shows the error you get when `[debug] mode` does not allow branch counting.
// `startCoverage(true)` also counts each side of every condition. The `nvs.toml` beside this
// file does not allow `"branch"`, so this call throws a `RuntimeError`.
try {
Core\Debug::startCoverage(true);
} catch (RuntimeError $e) {
echo $e->message, "\n";
}
// Line counting needs only `"coverage"`, so this call works.
Core\Debug::startCoverage();
echo "line counting is on\n";
Output
Core\Debug::startCoverage cannot start, because `[debug] mode` does not include `"branch"`.
help: add `"branch"` to `mode` under `[debug]` in nvs.toml.
line counting is on

Start again and the counts go on

<?nvs
// Shows that a second `startCoverage` adds to the counts that are already there.
class Greeter
{
public static function greet(string $name): string
{
return 'Hello, ' . $name;
}
}
Core\Debug::stopCoverage();
// The first round of counting covers one call.
Core\Debug::startCoverage();
echo Greeter::greet('Ada'), "\n";
Core\Debug::stopCoverage();
// This call runs while counting is off, so it is not counted.
echo Greeter::greet('Grace'), "\n";
// The second round covers one more call. The count for line 8 is now 2.
Core\Debug::startCoverage();
echo Greeter::greet('Alan'), "\n";
Core\Debug::stopCoverage();
foreach (Core\Debug::coverage() as string $path => array<int> $lines) {
echo 'line 8 ran ', $lines['8'], " times\n";
}
Output
Hello, Ada
Hello, Grace
Hello, Alan
line 8 ran 2 times