Skip to content

Core\Debug::coverage

Core\Debug::coverage(): array<array<int>>

Returns how many times each line of the program ran.

The result is an array with one entry for each file. The key is the file path. The value is an array from a line number to the number of times that line ran. A line that never ran is not in the result. So the result grows with the code that ran, not with the size of the program.

nvs run counts lines when nvs.toml has "coverage" in 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() and Core\Debug::startCoverage() stop and start it. coverage returns the counts at any time, also while counting is off. Without the setting, the result is an empty array.

The examples below list every line that ran, check whether one line ran, and count the calls of a method.

Return value

Returns array<array<int>>. Only lines that ran are listed. Lines are counted from the first line when [debug] mode includes "coverage", and when nvs test runs with a coverage option. Otherwise the array is empty.

Examples

The examples below run with this configuration file.

nvs.toml
# Allows the program to count which lines run, and each side of every condition.
# Line counting starts when the program starts.
[debug]
mode = ["coverage", "branch"]

List the lines that ran

<?nvs
// Shows how to list the lines of a program that ran, with a count for each line.
array<string> $names = ['Ada', 'Grace', 'Alan'];
foreach ($names as string $name) {
echo 'Hello, ', $name, "\n";
}
// `coverage` returns an array with one entry for each file. The key is the path.
// Each value is an array from a line number to the number of times it ran.
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
Hello, Ada
Hello, Grace
Hello, Alan
01-list-the-lines-that-ran.nvs
line 4: 1
line 5: 3
line 6: 3
line 11: 1

Find a line that never ran

<?nvs
// Shows how to check whether a line ran. A line that never ran is not in the result.
class Shipping
{
public static function cost(int $total): int
{
if ($total >= 50) {
return 0;
}
return 5;
}
}
// `true` also counts each side of the `if`. The setting in `nvs.toml` allows it.
Core\Debug::startCoverage(true);
echo 'cost: ', Shipping::cost(80), "\n";
echo 'cost: ', Shipping::cost(120), "\n";
Core\Debug::stopCoverage();
// Both totals are 50 or more, so line 11 never ran. This prints `line 11 ran: no`.
foreach (Core\Debug::coverage() as string $path => array<int> $lines) {
echo 'line 9 ran: ', Core\Arr::hasKey($lines, '9') ? 'yes' : 'no', "\n";
echo 'line 11 ran: ', Core\Arr::hasKey($lines, '11') ? 'yes' : 'no', "\n";
}
Output
cost: 0
cost: 0
line 9 ran: yes
line 11 ran: no

Count how often a method runs

<?nvs
// Shows how to find out how many times a method ran.
class Price
{
public static function withFee(int $cents): int
{
return $cents + 25;
}
}
array<int> $prices = [100, 250, 400, 80, 1000];
int $sum = 0;
foreach ($prices as int $cents) {
$sum += Price::withFee($cents);
}
echo 'total: ', $sum, "\n";
// Line 8 is the body of `withFee`. Its count is the number of calls, so this prints `5`.
foreach (Core\Debug::coverage() as string $path => array<int> $lines) {
echo 'withFee ran ', $lines['8'], " times\n";
}
Output
total: 1955
withFee ran 5 times