Core\Debug::stopCoverage
Core\Debug::stopCoverage(): voidStops counting how many times each line runs. The counts so far are kept.
After stopCoverage, the lines that run are not counted. Core\Debug::coverage() still returns the counts from before the stop. Core\Debug::startCoverage() starts counting again and adds to those counts.
When counting is already off, stopCoverage does nothing. It never throws an error, and it needs no setting in nvs.toml.
The examples below count only one part of a program, read the counts after a stop, and call stopCoverage when counting is already off.
Return value
Returns void. Nothing. It does nothing when coverage is not running.
Examples
The examples below run with this configuration file.
# Allows the program to count which lines run. Counting starts when the program starts.[debug]mode = ["coverage"]Count only the part you test
<?nvs// Shows how to stop counting after the part of a program you want to measure.
class Title{ public static function clean(string $title): string { return Core\Str::lower(Core\Str::trim($title)); }}
// Counting started with the program. This line stops it, and the next starts it again.Core\Debug::stopCoverage();Core\Debug::startCoverage();string $first = Title::clean(' Hello World ');
// `stopCoverage` stops counting. The lines after it are not counted.Core\Debug::stopCoverage();echo $first, "\n";echo Title::clean(' Second Post '), "\n";
// Line 8 has the count 1, from the first call. Lines 19 and 20 are not listed.foreach (Core\Debug::coverage() as string $path => array<int> $lines) { foreach ($lines as string $line => int $count) { echo 'line ', $line, ': ', $count, "\n"; }}hello worldsecond postline 8: 1line 13: 1line 15: 1line 18: 1The counts are kept
<?nvs// Shows that the counts are still there after `stopCoverage`.
int $sum = 0;for (int $i = 1; $i <= 4; $i++) { $sum += $i;}
Core\Debug::stopCoverage();
// Counting is off now, but `coverage` still returns the counts from before.// Line 6 ran once for each number, so this prints `4`.foreach (Core\Debug::coverage() as string $path => array<int> $lines) { echo 'line 6 ran ', $lines['6'], " times\n";}
// This loop runs while counting is off. The count for line 6 does not change.for (int $j = 1; $j <= 4; $j++) { $sum += $j;}
foreach (Core\Debug::coverage() as string $path => array<int> $lines) { echo 'line 6 ran ', $lines['6'], " times\n";}
echo 'sum: ', $sum, "\n";line 6 ran 4 timesline 6 ran 4 timessum: 20Stop when nothing is counted
<?nvs// Shows that `stopCoverage` is safe to call when counting is already off.
Core\Debug::stopCoverage();
// Counting is off now. A second call does nothing and does not throw an error.// So a cleanup step can call `stopCoverage` without checking first.Core\Debug::stopCoverage();
// Only the line of the first call was counted.foreach (Core\Debug::coverage() as string $path => array<int> $lines) { echo Core\Arr::count($lines), " line was counted\n";}1 line was counted