Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.88% covered (success)
96.88%
31 / 32
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Diff
96.88% covered (success)
96.88%
31 / 32
75.00% covered (warning)
75.00%
3 / 4
10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 getMissingFromOld
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 getMissingFromNew
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getChanged
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Copyright (C) 2018 Kunal Mehta <legoktm@debian.org>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19namespace Wikimedia\CloverDiff;
20
21/**
22 * Represents changes to coverage
23 */
24class Diff {
25
26    /**
27     * @var int[]|string[]
28     */
29    private array $missingFromOld;
30
31    /**
32     * @var int[]|string[]
33     */
34    private array $missingFromNew;
35
36    /**
37     * @var array
38     */
39    private array $changed;
40
41    /**
42     * @var array
43     */
44    private array $oldFiles;
45
46    /**
47     * @var array
48     */
49    private array $newFiles;
50
51    /**
52     * @param array $oldFiles Parsed clover.xml
53     * @param array $newFiles Parsed clover.xml
54     */
55    public function __construct( array $oldFiles, array $newFiles ) {
56        // Use array_filter to remove files that have 0 coverage, because
57        // it's not useful to output a 0 -> 0 diff report
58        $this->missingFromNew = array_diff(
59            array_keys( array_filter( $oldFiles ) ),
60            array_keys( $newFiles )
61        );
62        $this->missingFromOld = array_diff(
63            array_keys( array_filter( $newFiles ) ),
64            array_keys( $oldFiles )
65        );
66
67        $changed = [];
68        foreach ( $oldFiles as $path => $oldInfo ) {
69            if ( isset( $newFiles[$path] ) ) {
70                $newInfo = $newFiles[$path];
71                // Even though it's already been rounded, we want to round
72                // here just in case the change pushed it over the threshold
73                // for rounding
74                if ( round( $oldInfo, 2 ) !== round( $newInfo, 2 ) ) {
75                    $changed[] = $path;
76                }
77            }
78        }
79        $this->changed = $changed;
80
81        // @todo remove the files we don't care about anymore
82        $this->oldFiles = $oldFiles;
83        $this->newFiles = $newFiles;
84    }
85
86    /**
87     * Get files that are missing from the old XML file
88     *
89     * @return array
90     */
91    public function getMissingFromOld(): array {
92        $rows = [];
93        foreach ( $this->missingFromOld as $fname ) {
94            $rows[$fname] = $this->newFiles[$fname];
95        }
96
97        return $rows;
98    }
99
100    /**
101     * Get files that are missing from the new XML file
102     *
103     * @return array
104     */
105    public function getMissingFromNew(): array {
106        $rows = [];
107        foreach ( $this->missingFromNew as $fname ) {
108            $rows[$fname] = $this->oldFiles[$fname];
109        }
110
111        return $rows;
112    }
113
114    /**
115     * Get files that are in both, but have different values
116     *
117     * @return array
118     */
119    public function getChanged(): array {
120        $rows = [];
121        foreach ( $this->changed as $fname ) {
122            $rows[$fname] = [
123                $this->oldFiles[$fname],
124                $this->newFiles[$fname],
125            ];
126        }
127
128        return $rows;
129    }
130}