Completed
Push — 1.0 ( c1044b...753b1d )
by David
02:30
created

Message   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 106
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B addCoverageMessage() 0 26 3
A addDifferencesHtml() 0 6 1
A addDifference() 0 4 1
B getDifferencesHtml() 0 50 6
A getLinkToMethodInCommit() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
4
namespace TheCodingMachine\WashingMachine\Gitlab;
5
6
7
use TheCodingMachine\WashingMachine\Clover\Analysis\Difference;
8
use TheCodingMachine\WashingMachine\Clover\CoverageDetectorInterface;
9
use TheCodingMachine\WashingMachine\Clover\CrapMethodFetcherInterface;
10
use TheCodingMachine\WashingMachine\Clover\DiffService;
11
12
/**
13
 * A class to build the message sent to Gitlab.
14
 */
15
class Message
16
{
17
    private $msg = '';
18
19
    public function addCoverageMessage(CoverageDetectorInterface $coverageDetector, CoverageDetectorInterface $previousCoverageDetector)
20
    {
21
        $coverage = $coverageDetector->getCoveragePercentage();
22
        $previousCoverage = $previousCoverageDetector->getCoveragePercentage();
23
24
        $additionalText = '';
25
        $style = '';
26
        if ($coverage > $previousCoverage + 0.0001) {
27
            $additionalText = sprintf('(<em>+%.2f%%</em>)', ($coverage - $previousCoverage)*100);
28
            $style .= 'background-color: #00994c; color: white';
29
        } elseif ($coverage < $previousCoverage - 0.0001) {
30
            $additionalText = sprintf('(<em>-%.2f%%</em>)', ($previousCoverage - $coverage)*100);
31
            $style .= 'background-color: #ff6666; color: white';
32
        }
33
34
35
        // Note: there is a failure in the way Gitlab escapes HTML for the tables. Let's use this!.
36
        $this->msg .= sprintf('<table>
37
<tr>
38
<td>PHP&nbsp;code&nbsp;coverage:</td>
39
<td style="font-weight: bold">%.2f%%</td>
40
<td style="%s">%s</td>
41
<td width="99%%"></td>
42
</tr>
43
</table><br/>', $coverageDetector->getCoveragePercentage()*100, $style, $additionalText);
44
    }
45
46
47
    public function addDifferencesHtml(CrapMethodFetcherInterface $methodFetcher, CrapMethodFetcherInterface $previousMethodFetcher, DiffService $diffService, string $commitId, string $gitlabUrl, string $projectName)
48
    {
49
        $differences = $diffService->getMeaningfulDifferences($methodFetcher, $previousMethodFetcher);
50
51
        $this->msg .= $this->getDifferencesHtml($differences, $commitId, $gitlabUrl, $projectName);
52
    }
53
54
    public function addDifference(Difference $difference, string $commitId, string $gitlabUrl, string $projectName)
55
    {
56
        $this->msg .= $this->getDifferencesHtml([$difference], $commitId, $gitlabUrl, $projectName);
57
    }
58
59
60
    private function getDifferencesHtml(array $differences, string $commitId, string $gitlabUrl, string $projectName) : string
61
    {
62
        if (empty($differences)) {
63
            return 'No meaningful differences in code complexity detected.';
64
        }
65
66
        $tableTemplate = '<table>
67
<tr>
68
<th></th>
69
<th>C.R.A.P.</th>
70
<th>Variation</th>
71
<th width="99%%"></th>
72
</tr>
73
%s
74
</table>';
75
        $tableRows = '';
76
        $crapScoreEmojiGenerator = EmojiGenerator::createCrapScoreEmojiGenerator();
77
        foreach ($differences as $difference) {
78
            $style = '';
79
            if (!$difference->isNew()) {
80
81
                if ($difference->getCrapDifference() < 0) {
82
                    $style = 'background-color: #00994c; color: white';
83
                } else {
84
                    $style = 'background-color: #ff6666; color: white';
85
                }
86
                $differenceCol = sprintf('%+d', $difference->getCrapDifference());
87
                $crapScoreEmoji = '';
88
            } else {
89
                $differenceCol = '<em>New</em>';
90
                // For new rows, let's display an emoji
91
                $crapScoreEmoji = $crapScoreEmojiGenerator->getEmoji($difference->getCrapScore());
92
            }
93
94
            if ($difference->getFile() !== null) {
95
                $link = $this->getLinkToMethodInCommit($gitlabUrl, $projectName, $commitId, $difference->getFile(), $difference->getLine());
96
                $fullLink = sprintf('<a href="%s">%s</a>', $link, $difference->getMethodShortName());
97
            } else {
98
                $fullLink = $difference->getMethodShortName();
99
            }
100
101
            $tableRows .= sprintf('<tr>
102
<td><code>%s</code></td>
103
<td style="text-align:center">%d%s</td>
104
<td style="text-align:center;%s">%s</td>
105
</tr>', $fullLink, $difference->getCrapScore(), $crapScoreEmoji, $style, $differenceCol);
106
        }
107
108
        return sprintf($tableTemplate, $tableRows);
109
    }
110
111
    private function getLinkToMethodInCommit(string $gitlabUrl, string $projectName, string $commit, string $filePath, int $line)
112
    {
113
        return rtrim($gitlabUrl, '/').'/'.$projectName.'/blob/'.$commit.'/'.ltrim($filePath, '/').'#L'.$line;
114
    }
115
116
    public function __toString()
117
    {
118
        return $this->msg;
119
    }
120
}