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