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
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
11/**
12 * Represents changes to coverage
13 */
14class Diff {
15
16    /**
17     * @var int[]|string[]
18     */
19    private array $missingFromOld;
20
21    /**
22     * @var int[]|string[]
23     */
24    private array $missingFromNew;
25
26    /**
27     * @var array
28     */
29    private array $changed;
30
31    /**
32     * @var array
33     */
34    private array $oldFiles;
35
36    /**
37     * @var array
38     */
39    private array $newFiles;
40
41    /**
42     * @param array $oldFiles Parsed clover.xml
43     * @param array $newFiles Parsed clover.xml
44     */
45    public function __construct( array $oldFiles, array $newFiles ) {
46        // Use array_filter to remove files that have 0 coverage, because
47        // it's not useful to output a 0 -> 0 diff report
48        $this->missingFromNew = array_diff(
49            array_keys( array_filter( $oldFiles ) ),
50            array_keys( $newFiles )
51        );
52        $this->missingFromOld = array_diff(
53            array_keys( array_filter( $newFiles ) ),
54            array_keys( $oldFiles )
55        );
56
57        $changed = [];
58        foreach ( $oldFiles as $path => $oldInfo ) {
59            if ( isset( $newFiles[$path] ) ) {
60                $newInfo = $newFiles[$path];
61                // Even though it's already been rounded, we want to round
62                // here just in case the change pushed it over the threshold
63                // for rounding
64                if ( round( $oldInfo, 2 ) !== round( $newInfo, 2 ) ) {
65                    $changed[] = $path;
66                }
67            }
68        }
69        $this->changed = $changed;
70
71        // @todo remove the files we don't care about anymore
72        $this->oldFiles = $oldFiles;
73        $this->newFiles = $newFiles;
74    }
75
76    /**
77     * Get files that are missing from the old XML file
78     *
79     * @return array
80     */
81    public function getMissingFromOld(): array {
82        $rows = [];
83        foreach ( $this->missingFromOld as $fname ) {
84            $rows[$fname] = $this->newFiles[$fname];
85        }
86
87        return $rows;
88    }
89
90    /**
91     * Get files that are missing from the new XML file
92     *
93     * @return array
94     */
95    public function getMissingFromNew(): array {
96        $rows = [];
97        foreach ( $this->missingFromNew as $fname ) {
98            $rows[$fname] = $this->oldFiles[$fname];
99        }
100
101        return $rows;
102    }
103
104    /**
105     * Get files that are in both, but have different values
106     *
107     * @return array
108     */
109    public function getChanged(): array {
110        $rows = [];
111        foreach ( $this->changed as $fname ) {
112            $rows[$fname] = [
113                $this->oldFiles[$fname],
114                $this->newFiles[$fname],
115            ];
116        }
117
118        return $rows;
119    }
120}