1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Entity;
3:
4: /**
5: * Metrics.
6: *
7: * @author Kitamura Satoshi <with.no.parachute@gmail.com>
8: */
9: class Metrics
10: {
11: /**
12: * Number of statements.
13: *
14: * @var integer
15: */
16: protected $statements;
17:
18: /**
19: * Number of covered statements.
20: *
21: * @var integer
22: */
23: protected $coveredStatements;
24:
25: /**
26: * Line coverage.
27: *
28: * @var float
29: */
30: protected $lineCoverage;
31:
32: /**
33: * Constructor.
34: *
35: * @param array $coverage Coverage data.
36: */
37: public function __construct(array $coverage = array())
38: {
39: if (!empty($coverage)) {
40: // statements
41: // not null
42: $statementsArray = array_filter(
43: $coverage,
44: function ($line) {
45: return $line !== null;
46: }
47: );
48: $this->statements = count($statementsArray);
49:
50: // coveredstatements
51: // gt 0
52: $coveredArray = array_filter(
53: $statementsArray,
54: function ($line) {
55: return $line > 0;
56: }
57: );
58: $this->coveredStatements = count($coveredArray);
59: } else {
60: $this->statements = 0;
61: $this->coveredStatements = 0;
62: }
63: }
64:
65: // API
66:
67: /**
68: * Merge other metrics.
69: *
70: * @param Metrics $that
71: * @return void
72: */
73: public function merge(Metrics $that)
74: {
75: $this->statements += $that->statements;
76: $this->coveredStatements += $that->coveredStatements;
77: $this->lineCoverage = null; // clear previous data
78: }
79:
80: // internal method
81:
82: /**
83: * Calculate line coverage.
84: *
85: * @param integer $statements Number of statements.
86: * @param integer $coveredStatements Number of covered statements.
87: * @return float
88: */
89: protected function calculateLineCoverage($statements, $coveredStatements)
90: {
91: if ($statements === 0) {
92: return 0;
93: }
94:
95: return ($coveredStatements / $statements) * 100;
96: }
97:
98: // accessor
99:
100: /**
101: * Return whether the source file has executable statements.
102: *
103: * @return boolean
104: */
105: public function hasStatements()
106: {
107: return $this->statements !== 0;
108: }
109:
110: /**
111: * Return number of statements.
112: *
113: * @return integer
114: */
115: public function getStatements()
116: {
117: return $this->statements;
118: }
119:
120: /**
121: * Return number of covered statements.
122: *
123: * @return integer
124: */
125: public function getCoveredStatements()
126: {
127: return $this->coveredStatements;
128: }
129:
130: /**
131: * Return line coverage.
132: *
133: * @return float
134: */
135: public function getLineCoverage()
136: {
137: if (!isset($this->lineCoverage)) {
138: $this->lineCoverage = $this->calculateLineCoverage($this->statements, $this->coveredStatements);
139: }
140:
141: return $this->lineCoverage;
142: }
143: }
144: