aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/simpletest/extensions/coverage/coverage_calculator.php
blob: f1aa57bbab5514197c37253f6dc67efc398df109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* @package        SimpleTest
* @subpackage     Extensions
*/
/**
* @package        SimpleTest
* @subpackage     Extensions
*/
class CoverageCalculator {

    function coverageByFileVariables($file, $coverage) {
        $hnd = fopen($file, 'r');
        if ($hnd == null) {
            throw new Exception("File $file is missing");
        }
        $lines = array();
        for ($i = 1; !feof($hnd); $i++) {
            $line = fgets($hnd);
            $lineCoverage = $this->lineCoverageCodeToStyleClass($coverage, $i);
            $lines[$i] = array('lineCoverage' => $lineCoverage, 'code' => $line);
        }

        fclose($hnd);

        $var = compact('file', 'lines', 'coverage');
        return $var;
    }

    function lineCoverageCodeToStyleClass($coverage, $line) {
        if (!array_key_exists($line, $coverage)) {
            return "comment";
        }
        $code = $coverage[$line];
        if (empty($code)) {
            return "comment";
        }
        switch ($code) {
            case -1:
                return "missed";
            case -2:
                return "dead";
        }

        return "covered";
    }

    function totalLoc($total, $coverage) {
        return $total + sizeof($coverage);
    }

    function lineCoverage($total, $line) {
        # NOTE: counting dead code as covered, as it's almost always an executable line
        # strange artifact of xdebug or underlying system
        return $total + ($line > 0 || $line == -2 ? 1 : 0);
    }

    function totalCoverage($total, $coverage) {
        return $total + array_reduce($coverage, array(&$this, "lineCoverage"));
    }

    static function reportFilename($filename) {
        return preg_replace('|[/\\\\]|', '_', $filename) . '.html';
    }

    function percentCoverageByFile($coverage, $file, &$results) {
        $byFileReport = self::reportFilename($file);

        $loc = sizeof($coverage);
        if ($loc == 0)
        return 0;
        $lineCoverage = array_reduce($coverage, array(&$this, "lineCoverage"));
        $percentage = 100 * ($lineCoverage / $loc);
        $results[0][$file] = array('byFileReport' => $byFileReport, 'percentage' => $percentage);
    }

    function variables($coverage, $untouched) {
        $coverageByFile = array();
        array_walk($coverage, array(&$this, "percentCoverageByFile"), array(&$coverageByFile));

        $totalLoc = array_reduce($coverage, array(&$this, "totalLoc"));

        if ($totalLoc > 0) {
            $totalLinesOfCoverage = array_reduce($coverage, array(&$this, "totalCoverage"));
            $totalPercentCoverage = 100 * ($totalLinesOfCoverage / $totalLoc);
        }

        $untouchedPercentageDenominator = sizeof($coverage) + sizeof($untouched);
        if ($untouchedPercentageDenominator > 0) {
            $filesTouchedPercentage = 100 * sizeof($coverage) / $untouchedPercentageDenominator;
        }

        $var = compact('coverageByFile', 'totalPercentCoverage', 'totalLoc', 'totalLinesOfCoverage', 'filesTouchedPercentage');
        $var['untouched'] = $untouched;
        return $var;
    }
}
?>