Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Round MSI scores #1190

Merged
merged 16 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Infection\Configuration\Schema\SchemaConfigurationLoader;
use Infection\Console\ConsoleOutput;
use Infection\Console\Exception\ConfigurationException;
use Infection\Console\Input\MsiParser;
use Infection\Console\LogVerbosity;
use Infection\Container;
use Infection\Engine;
Expand All @@ -49,9 +50,7 @@
use Infection\Metrics\MinMsiCheckFailed;
use Infection\Process\Runner\InitialTestsFailed;
use Infection\TestFramework\TestFrameworkTypes;
use function is_numeric;
use function Safe\sprintf;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -268,17 +267,10 @@ private function initContainer(InputInterface $input): void

/** @var string|null $minMsi */
$minMsi = $input->getOption('min-msi');

if ($minMsi !== null && !is_numeric($minMsi)) {
throw new InvalidArgumentException(sprintf('Expected min-msi to be a float. Got "%s"', $minMsi));
}

/** @var string|null $minCoveredMsi */
$minCoveredMsi = $input->getOption('min-covered-msi');

if ($minCoveredMsi !== null && !is_numeric($minCoveredMsi)) {
throw new InvalidArgumentException(sprintf('Expected min-covered-msi to be a float. Got "%s"', $minCoveredMsi));
}
$precision = MsiParser::detectPrecision($minMsi, $minCoveredMsi);

$this->container = $this->getApplication()->getContainer()->withDynamicParameters(
$configFile === '' ? null : $configFile,
Expand All @@ -293,8 +285,9 @@ private function initContainer(InputInterface $input): void
$initialTestsPhpOptions === '' ? null : $initialTestsPhpOptions,
(bool) $input->getOption('skip-initial-tests'),
$input->getOption('ignore-msi-with-no-mutations'),
$minMsi === null ? null : (float) $minMsi,
$minCoveredMsi === null ? null : (float) $minCoveredMsi,
MsiParser::parse($minMsi, $precision, 'min-msi'),
MsiParser::parse($minCoveredMsi, $precision, 'min-covered-msi'),
$precision,
$testFramework === '' ? null : $testFramework,
$testFrameworkExtraOptions === '' ? null : $testFrameworkExtraOptions,
trim((string) $input->getOption('filter')),
Expand Down
8 changes: 8 additions & 0 deletions src/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Configuration
private $minMsi;
private $showMutations;
private $minCoveredMsi;
private $msiPrecision;
private $threadCount;
private $dryRun;

Expand Down Expand Up @@ -116,6 +117,7 @@ public function __construct(
?float $minMsi,
bool $showMutations,
?float $minCoveredMsi,
int $msiPrecision,
int $threadCount,
bool $dryRun
) {
Expand Down Expand Up @@ -152,6 +154,7 @@ public function __construct(
$this->minMsi = $minMsi;
$this->showMutations = $showMutations;
$this->minCoveredMsi = $minCoveredMsi;
$this->msiPrecision = $msiPrecision;
$this->threadCount = $threadCount;
$this->dryRun = $dryRun;
}
Expand Down Expand Up @@ -285,6 +288,11 @@ public function getMinCoveredMsi(): ?float
return $this->minCoveredMsi;
}

public function getMsiPrecision(): int
{
return $this->msiPrecision;
}

public function getThreadCount(): int
{
return $this->threadCount;
Expand Down
2 changes: 2 additions & 0 deletions src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function create(
?float $minMsi,
bool $showMutations,
?float $minCoveredMsi,
int $msiPrecision,
string $mutatorsInput,
?string $testFramework,
?string $testFrameworkExtraOptions,
Expand Down Expand Up @@ -152,6 +153,7 @@ public function create(
self::retrieveMinMsi($minMsi, $schema),
$showMutations,
self::retrieveMinCoveredMsi($minCoveredMsi, $schema),
$msiPrecision,
$threadCount,
$dryRun
);
Expand Down
103 changes: 103 additions & 0 deletions src/Console/Input/MsiParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Console\Input;

use function count;
use function explode;
use Infection\CannotBeInstantiated;
use function max;
use const PHP_ROUND_HALF_UP;
use function round;
use function Safe\sprintf;
use function strlen;
use function trim;
use Webmozart\Assert\Assert;

/**
* @internal
*/
final class MsiParser
{
use CannotBeInstantiated;

public static function detectPrecision(?string ...$values): int
{
$precisions = [2];

foreach ($values as $value) {
$value = trim((string) $value);

if ($value === '') {
continue;
}

$valueParts = explode('.', $value);

if (count($valueParts) !== 2) {
continue;
}

$precisions[] = strlen($valueParts[1]);
}

return max($precisions);
}

public static function parse(?string $value, int $precision, string $optionName): ?float
{
$value = trim((string) $value);

if ($value === '') {
return null;
}

Assert::numeric(
$value,
sprintf('Expected %s to be a float. Got "%s"', $optionName, $value)
);

$roundedValue = round((float) $value, $precision, PHP_ROUND_HALF_UP);

Assert::range(
$roundedValue,
0,
100,
sprintf('Expected %s to be an element of [0;100]. Got %%s', $optionName)
);

return $roundedValue;
}
}
7 changes: 5 additions & 2 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ public static function create(): self
PrettyPrinterAbstract::class => static function (): Standard {
return new Standard();
},
MetricsCalculator::class => static function (): MetricsCalculator {
return new MetricsCalculator();
MetricsCalculator::class => static function (self $container): MetricsCalculator {
return new MetricsCalculator($container->getConfiguration()->getMsiPrecision());
},
Stopwatch::class => static function (): Stopwatch {
return new Stopwatch();
Expand Down Expand Up @@ -495,6 +495,7 @@ public function withDynamicParameters(
bool $ignoreMsiWithNoMutations,
?float $minMsi,
?float $minCoveredMsi,
int $msiPrecision,
?string $testFramework,
?string $testFrameworkExtraOptions,
string $filter,
Expand Down Expand Up @@ -533,6 +534,7 @@ static function (self $container) use (
$minMsi,
$showMutations,
$minCoveredMsi,
$msiPrecision,
$mutatorsInput,
$testFramework,
$testFrameworkExtraOptions,
Expand All @@ -554,6 +556,7 @@ static function (self $container) use (
$minMsi,
$showMutations,
$minCoveredMsi,
$msiPrecision,
$mutatorsInput,
$testFramework,
$testFrameworkExtraOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/Logger/PerMutatorLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private function createMetricsPerMutators(): array
$calculatorPerMutator = [];

foreach ($processPerMutator as $mutator => $executionResults) {
$calculator = new MetricsCalculator();
$calculator = new MetricsCalculator($this->metricsCalculator->getRoundingPrecision());
$calculator->collect(...$executionResults);

$calculatorPerMutator[$mutator] = $calculator;
Expand Down
18 changes: 15 additions & 3 deletions src/Metrics/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@

namespace Infection\Metrics;

use const PHP_ROUND_HALF_UP;
use function round;

/**
* @internal
*/
final class Calculator
{
private $roundingPrecision;
private $killedCount;
private $errorCount;
private $timedOutCount;
Expand All @@ -62,12 +66,14 @@ final class Calculator
private $coveredMutationScoreIndicator;

public function __construct(
int $roundingPrecision,
int $killedCount,
int $errorCount,
int $timedOutCount,
int $notTestedCount,
int $totalCount
) {
$this->roundingPrecision = $roundingPrecision;
$this->killedCount = $killedCount;
$this->errorCount = $errorCount;
$this->timedOutCount = $timedOutCount;
Expand All @@ -78,6 +84,7 @@ public function __construct(
public static function fromMetrics(MetricsCalculator $calculator): self
{
return new self(
$calculator->getRoundingPrecision(),
$calculator->getKilledCount(),
$calculator->getErrorCount(),
$calculator->getTimedOutCount(),
Expand All @@ -103,7 +110,7 @@ public function getMutationScoreIndicator(): float
$score = 100 * $coveredTotal / $totalCount;
}

return $this->mutationScoreIndicator = $score;
return $this->mutationScoreIndicator = $this->round($score);
}

/**
Expand All @@ -123,7 +130,7 @@ public function getCoverageRate(): float
$coveredRate = 100 * $testedTotal / $totalCount;
}

return $this->coverageRate = $coveredRate;
return $this->coverageRate = $this->round($coveredRate);
}

/**
Expand All @@ -143,6 +150,11 @@ public function getCoveredCodeMutationScoreIndicator(): float
$score = 100 * $coveredTotal / $testedTotal;
}

return $this->coveredMutationScoreIndicator = $score;
return $this->coveredMutationScoreIndicator = $this->round($score);
}

private function round(float $value): float
{
return round($value, $this->roundingPrecision, PHP_ROUND_HALF_UP);
}
}