1: <?php
2: namespace Contrib\Bundle\CoverallsV1Bundle\Entity;
3:
4: /**
5: * Data represents "source_files" element of Coveralls' "json_file".
6: *
7: * @author Kitamura Satoshi <with.no.parachute@gmail.com>
8: */
9: class SourceFile extends Coveralls
10: {
11: /**
12: * Source filename.
13: *
14: * @var string
15: */
16: protected $name;
17:
18: /**
19: * Source content.
20: *
21: * @var string
22: */
23: protected $source;
24:
25: /**
26: * Coverage data of the source file.
27: *
28: * @var array
29: */
30: protected $coverage;
31:
32: /**
33: * Absolute path.
34: *
35: * @var string
36: */
37: protected $path;
38:
39: /**
40: * Line number of the source file.
41: *
42: * @var integer
43: */
44: protected $fileLines;
45:
46: /**
47: * Metrics.
48: *
49: * @var Metrics
50: */
51: protected $metrics;
52:
53: /**
54: * Constructor.
55: *
56: * @param string $path Absolute path.
57: * @param string $name Source filename.
58: * @param string $eol End of line.
59: */
60: public function __construct($path, $name, $eol = "\n")
61: {
62: $this->path = $path;
63: $this->name = $name;
64: $this->source = trim(file_get_contents($path));
65:
66: $lines = explode($eol, $this->source);
67: $this->fileLines = count($lines);
68: $this->coverage = array_fill(0, $this->fileLines, null);
69: }
70:
71: /**
72: * {@inheritdoc}
73: *
74: * @see \Contrib\Bundle\CoverallsBundle\Entity\ArrayConvertable::toArray()
75: */
76: public function toArray()
77: {
78: return array(
79: 'name' => $this->name,
80: 'source' => $this->source,
81: 'coverage' => $this->coverage,
82: );
83: }
84:
85: // API
86:
87: /**
88: * Add coverage.
89: *
90: * @param integer $lineNum Line number.
91: * @param integer $count Number of covered.
92: * @return void
93: */
94: public function addCoverage($lineNum, $count)
95: {
96: if (array_key_exists($lineNum, $this->coverage)) {
97: $this->coverage[$lineNum] += $count;
98: }
99: }
100:
101: /**
102: * Return line coverage.
103: *
104: * @return float
105: */
106: public function reportLineCoverage()
107: {
108: return $this->getMetrics()->getLineCoverage();
109: }
110:
111: // accessor
112:
113: /**
114: * Return source filename.
115: *
116: * @return string
117: */
118: public function getName()
119: {
120: return $this->name;
121: }
122:
123: /**
124: * Return source content.
125: *
126: * @return string
127: */
128: public function getSource()
129: {
130: return $this->source;
131: }
132:
133: /**
134: * Return coverage data of the source file.
135: *
136: * @return array
137: */
138: public function getCoverage()
139: {
140: return $this->coverage;
141: }
142:
143: /**
144: * Return absolute path.
145: *
146: * @return string
147: */
148: public function getPath()
149: {
150: return $this->path;
151: }
152:
153: /**
154: * Return line number of the source file.
155: *
156: * @return integer
157: */
158: public function getFileLines()
159: {
160: return $this->fileLines;
161: }
162:
163: /**
164: * Return metrics.
165: *
166: * @return \Contrib\Bundle\CoverallsV1Bundle\Entity\Metrics
167: */
168: public function getMetrics()
169: {
170: if (!isset($this->metrics)) {
171: $this->metrics = new Metrics($this->coverage);
172: }
173:
174: return $this->metrics;
175: }
176: }
177: