Skip to content

Commit f2783dc

Browse files
committed
add precision option
1 parent d9e99fd commit f2783dc

File tree

4 files changed

+9
-3
lines changed

4 files changed

+9
-3
lines changed

src/Commands/CheckCoverageCommand.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function configure(): void
2929
->addOption('require', 'r', InputOption::VALUE_REQUIRED, 'Require a minimum code coverage percentage', null)
3030
->addOption('metric', 'm', InputOption::VALUE_REQUIRED, 'Use a specific metric field (element, statement, or method)')
3131
->addOption('coverage-only', 'C', InputOption::VALUE_NONE, 'Display only the code coverage percentage')
32+
->addOption('precision', 'p', InputOption::VALUE_REQUIRED, 'Precision to use when rounding the code coverage percentage', 4)
3233
->setDescription('Checks a clover-format coverage file for a minimum coverage percentage and optionally enforces it.');
3334
}
3435

src/Configuration/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ class Configuration
1919
/** @var string */
2020
public $metricField;
2121

22-
public function __construct(string $filename, bool $requireMode, $required, bool $displayCoverageOnly, string $metricField)
22+
/** @var int */
23+
public $precision;
24+
25+
public function __construct(string $filename, bool $requireMode, $required, bool $displayCoverageOnly, string $metricField, int $precision)
2326
{
2427
$this->filename = $filename;
2528
$this->required = $required;
2629
$this->requireMode = $requireMode;
2730
$this->displayCoverageOnly = $displayCoverageOnly;
2831
$this->metricField = rtrim(strtolower($metricField), 's');
32+
$this->precision = $precision;
2933
}
3034

3135
public function validate(): void

src/Configuration/ConfigurationFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ public static function create(InputInterface $input): Configuration
1212
$requireMode = $input->hasOption('require') && $input->getOption('require') !== null;
1313
$percentage = $requireMode ? (float)$input->getOption('require') : null;
1414
$displayCoverageOnly = $input->hasOption('coverage-only') && $input->getOption('coverage-only') === true;
15+
$precision = $input->hasOption('precision') ? (int)$input->getOption('precision') : 4;
1516
$metricField = 'element';
1617

1718
if ($input->hasOption('metric') && $input->getOption('metric') !== null) {
1819
$metricField = $input->getOption('metric');
1920
}
2021

21-
return new Configuration($filename, $requireMode, $percentage, $displayCoverageOnly, $metricField);
22+
return new Configuration($filename, $requireMode, $percentage, $displayCoverageOnly, $metricField, $precision);
2223
}
2324
}

src/CoverageChecker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function getCoveragePercent(): float
3939
$totalElements = $this->getMetricFieldSum($metrics, $totalName);
4040
$checkedElements = $this->getMetricFieldSum($metrics, $coveredName);
4141

42-
return round(($checkedElements / $totalElements) * 100, 4);
42+
return round(($checkedElements / $totalElements) * 100, $this->config->precision);
4343
}
4444

4545
public function check(float $minPercentage): bool

0 commit comments

Comments
 (0)