Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.91% covered (warning)
84.91%
45 / 53
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DiffPrinter
84.91% covered (warning)
84.91%
45 / 53
66.67% covered (warning)
66.67%
2 / 3
10.34
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 format
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 show
77.14% covered (warning)
77.14%
27 / 35
0.00% covered (danger)
0.00%
0 / 1
6.43
1<?php
2declare( strict_types = 1 );
3
4/**
5 * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org>
6 * @license GPL-3.0-or-later
7 */
8
9namespace Wikimedia\CloverDiff;
10
11use Symfony\Component\Console\Helper\Table;
12use Symfony\Component\Console\Output\OutputInterface;
13
14/**
15 * Print the result of a diff in a
16 * cool table
17 */
18class DiffPrinter {
19
20    /**
21     * @var OutputInterface
22     */
23    private OutputInterface $output;
24
25    /**
26     * @param OutputInterface $output stdout
27     */
28    public function __construct( OutputInterface $output ) {
29        $this->output = $output;
30    }
31
32    /**
33     * Fancy coloring and padding for numbers
34     *
35     * @param float $num
36     *
37     * @return string
38     */
39    private function format( float $num ): string {
40        if ( $num < 50 ) {
41            $color = 'error';
42        } elseif ( $num > 90 ) {
43            $color = 'info';
44        } else {
45            $color = 'comment';
46        }
47        // Pad leading 0s
48        $pad = str_pad(
49            number_format( $num, 2 ),
50            5,
51            '0',
52            STR_PAD_LEFT
53        );
54        // Pad leading space to line up with 100%,
55        // and pick a color!
56        return "<$color>" . str_pad(
57            $pad,
58            6,
59            ' ',
60            STR_PAD_LEFT
61        ) . "</$color>";
62    }
63
64    /**
65     * @param Diff $diff Diff to print
66     *
67     * @return bool Whether any file had lower coverage afterwards
68     */
69    public function show( Diff $diff ): bool {
70        $tableRows = [];
71        foreach ( $diff->getMissingFromNew() as $fname => $val ) {
72            $tableRows[] = [
73                $fname,
74                $this->format( $val ),
75                0,
76            ];
77        }
78
79        foreach ( $diff->getMissingFromOld() as $fname => $val ) {
80            $tableRows[] = [
81                $fname,
82                0,
83                $this->format( $val ),
84            ];
85        }
86
87        $lowered = false;
88        foreach ( $diff->getChanged() as $fname => $info ) {
89            [ $old, $new ] = $info;
90            $tableRows[] = [
91                $fname,
92                $this->format( $old ),
93                $this->format( $new ),
94            ];
95
96            // Fail if any file has less coverage.
97            if ( $new < $old ) {
98                $lowered = true;
99            }
100        }
101
102        if ( $tableRows ) {
103            // Sort all files in order!
104            usort( $tableRows, static function ( $a, $b ) {
105                return strcmp( $a[0], $b[0] );
106            } );
107
108            $table = new Table( $this->output );
109            $table->setHeaders( [
110                'Filename', 'Old %', 'New %'
111            ] )->setRows( $tableRows )->render();
112        } else {
113            $this->output->writeln(
114                '<info>No coverage changes found.</info>'
115            );
116        }
117
118        return $lowered;
119    }
120}