Passed
Push — master ( 3f4221...33ba2d )
by Maks
03:05
created

JsonLogger   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getLogLines() 0 22 2
A getResultsLine() 0 19 2
1
<?php
2
/**
3
 * This code is licensed under the BSD 3-Clause License.
4
 *
5
 * Copyright (c) 2017, Maks Rafalko
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * * Redistributions of source code must retain the above copyright notice, this
12
 *   list of conditions and the following disclaimer.
13
 *
14
 * * Redistributions in binary form must reproduce the above copyright notice,
15
 *   this list of conditions and the following disclaimer in the documentation
16
 *   and/or other materials provided with the distribution.
17
 *
18
 * * Neither the name of the copyright holder nor the names of its
19
 *   contributors may be used to endorse or promote products derived from
20
 *   this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
 */
33
34
declare(strict_types=1);
35
36
namespace Infection\Logger;
37
38
use Infection\Metrics\MetricsCalculator;
39
use Infection\Mutant\MutantExecutionResult;
40
use Infection\Str;
41
use function json_encode;
42
use const JSON_THROW_ON_ERROR;
43
44
/**
45
 * @internal
46
 */
47
final class JsonLogger implements LineMutationTestingResultsLogger
48
{
49
    private $metricsCalculator;
50
    private $onlyCoveredMode;
51
52
    public function __construct(
53
        MetricsCalculator $metricsCalculator,
54
        bool $onlyCoveredMode
55
    ) {
56
        $this->metricsCalculator = $metricsCalculator;
57
        $this->onlyCoveredMode = $onlyCoveredMode;
58
    }
59
60
    /**
61
     * @return array{0: string}
62
     */
63
    public function getLogLines(): array
64
    {
65
        $data = [
66
            'stats' => [
67
                'totalMutantsCount' => $this->metricsCalculator->getTotalMutantsCount(),
68
                'killedCount' => $this->metricsCalculator->getKilledCount(),
69
                'notCoveredCount' => $this->metricsCalculator->getNotTestedCount(),
70
                'escapedCount' => $this->metricsCalculator->getEscapedCount(),
71
                'errorCount' => $this->metricsCalculator->getErrorCount(),
72
                'timeOutCount' => $this->metricsCalculator->getTimedOutCount(),
73
                'msi' => $this->metricsCalculator->getMutationScoreIndicator(),
74
                'mutationCodeCoverage' => $this->metricsCalculator->getCoverageRate(),
75
                'coveredCodeMsi' => $this->metricsCalculator->getCoveredCodeMutationScoreIndicator(),
76
            ],
77
            'escaped' => $this->getResultsLine($this->metricsCalculator->getEscapedExecutionResults()),
78
            'timeouted' => $this->getResultsLine($this->metricsCalculator->getTimedOutExecutionResults()),
79
            'killed' => $this->getResultsLine($this->metricsCalculator->getKilledExecutionResults()),
80
            'errored' => $this->getResultsLine($this->metricsCalculator->getErrorExecutionResults()),
81
            'uncovered' => $this->onlyCoveredMode ? [] : $this->getResultsLine($this->metricsCalculator->getNotCoveredExecutionResults()),
82
        ];
83
84
        return [json_encode($data, JSON_THROW_ON_ERROR)];
85
    }
86
87
    /**
88
     * @param MutantExecutionResult[] $executionResults
89
     *
90
     * @return array<int, array{mutator: array, diff: string, processOutput: string}>
91
     */
92
    private function getResultsLine(array $executionResults): array
93
    {
94
        $mutatorRows = [];
95
96
        foreach ($executionResults as $index => $mutantProcess) {
97
            $mutatorRows[] = [
98
                'mutator' => [
99
                    'mutatorName' => $mutantProcess->getMutatorName(),
100
                    'originalSourceCode' => $mutantProcess->getOriginalCode(),
101
                    'mutatedSourceCode' => $mutantProcess->getMutatedCode(),
102
                    'originalFilePath' => $mutantProcess->getOriginalFilePath(),
103
                    'originalStartLine' => $mutantProcess->getOriginalStartingLine(),
104
                ],
105
                'diff' => Str::trimLineReturns($mutantProcess->getMutantDiff()),
106
                'processOutput' => Str::trimLineReturns($mutantProcess->getProcessOutput()),
107
            ];
108
        }
109
110
        return $mutatorRows;
111
    }
112
}
113