Skip to content

Commit d15082d

Browse files
Separate classes and methods from functions.
1 parent 570c8f4 commit d15082d

File tree

4 files changed

+159
-39
lines changed

4 files changed

+159
-39
lines changed

PHP/CodeCoverage/Report/Node.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ public function getLineExecutedPercent()
159159
*/
160160
abstract public function getClasses();
161161

162+
/**
163+
* Returns the functions of this node.
164+
*
165+
* @return array
166+
*/
167+
abstract public function getFunctions();
168+
162169
/**
163170
* Returns the number of executable lines.
164171
*
@@ -200,4 +207,18 @@ abstract public function getNumMethods();
200207
* @return integer
201208
*/
202209
abstract public function getNumTestedMethods();
210+
211+
/**
212+
* Returns the number of functions.
213+
*
214+
* @return integer
215+
*/
216+
abstract public function getNumFunctions();
217+
218+
/**
219+
* Returns the number of tested functions.
220+
*
221+
* @return integer
222+
*/
223+
abstract public function getNumTestedFunctions();
203224
}

PHP/CodeCoverage/Report/Node/Directory.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ class PHP_CodeCoverage_Report_Node_Directory extends PHP_CodeCoverage_Report_Nod
7777
*/
7878
protected $classes;
7979

80+
/**
81+
* @var array
82+
*/
83+
protected $functions;
84+
8085
/**
8186
* @var integer
8287
*/
@@ -107,6 +112,16 @@ class PHP_CodeCoverage_Report_Node_Directory extends PHP_CodeCoverage_Report_Nod
107112
*/
108113
protected $numTestedMethods = -1;
109114

115+
/**
116+
* @var integer
117+
*/
118+
protected $numFunctions = -1;
119+
120+
/**
121+
* @var integer
122+
*/
123+
protected $numTestedFunctions = -1;
124+
110125
/**
111126
* Returns an iterator for this node.
112127
*
@@ -214,6 +229,26 @@ public function getClasses()
214229
return $this->classes;
215230
}
216231

232+
/**
233+
* Returns the functions of this node.
234+
*
235+
* @return array
236+
*/
237+
public function getFunctions()
238+
{
239+
if ($this->functions === NULL) {
240+
$this->functions = array();
241+
242+
foreach ($this->children as $child) {
243+
$this->functions = array_merge(
244+
$this->functions, $child->getFunctions()
245+
);
246+
}
247+
}
248+
249+
return $this->functions;
250+
}
251+
217252
/**
218253
* Returns the number of executable lines.
219254
*
@@ -321,4 +356,40 @@ public function getNumTestedMethods()
321356

322357
return $this->numTestedMethods;
323358
}
359+
360+
/**
361+
* Returns the number of functions.
362+
*
363+
* @return integer
364+
*/
365+
public function getNumFunctions()
366+
{
367+
if ($this->numFunctions == -1) {
368+
$this->numFunctions = 0;
369+
370+
foreach ($this->children as $child) {
371+
$this->numFunctions += $child->getNumFunctions();
372+
}
373+
}
374+
375+
return $this->numFunctions;
376+
}
377+
378+
/**
379+
* Returns the number of tested functions.
380+
*
381+
* @return integer
382+
*/
383+
public function getNumTestedFunctions()
384+
{
385+
if ($this->numTestedFunctions == -1) {
386+
$this->numTestedFunctions = 0;
387+
388+
foreach ($this->children as $child) {
389+
$this->numTestedFunctions += $child->getNumTestedFunctions();
390+
}
391+
}
392+
393+
return $this->numTestedFunctions;
394+
}
324395
}

PHP/CodeCoverage/Report/Node/File.php

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node
9292
protected $classes = array();
9393

9494
/**
95-
* @var integer
95+
* @var array
9696
*/
97-
protected $numTestedClasses = 0;
97+
protected $functions = array();
9898

9999
/**
100100
* @var integer
101101
*/
102-
protected $numClasses = NULL;
102+
protected $numTestedClasses = 0;
103103

104104
/**
105105
* @var integer
@@ -111,6 +111,11 @@ class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node
111111
*/
112112
protected $numTestedMethods = NULL;
113113

114+
/**
115+
* @var integer
116+
*/
117+
protected $numTestedFunctions = NULL;
118+
114119
/**
115120
* @var array
116121
*/
@@ -153,6 +158,16 @@ public function getClasses()
153158
return $this->classes;
154159
}
155160

161+
/**
162+
* Returns the functions of this node.
163+
*
164+
* @return array
165+
*/
166+
public function getFunctions()
167+
{
168+
return $this->functions;
169+
}
170+
156171
/**
157172
* Returns the number of executable lines.
158173
*
@@ -180,15 +195,7 @@ public function getNumExecutedLines()
180195
*/
181196
public function getNumClasses()
182197
{
183-
if ($this->numClasses === NULL) {
184-
$this->numClasses = count($this->classes);
185-
186-
if (isset($this->classes['*'])) {
187-
$this->numClasses--;
188-
}
189-
}
190-
191-
return $this->numClasses;
198+
return count($this->classes);
192199
}
193200

194201
/**
@@ -246,6 +253,37 @@ public function getNumTestedMethods()
246253
return $this->numTestedMethods;
247254
}
248255

256+
/**
257+
* Returns the number of functions.
258+
*
259+
* @return integer
260+
*/
261+
public function getNumFunctions()
262+
{
263+
return count($this->functions);
264+
}
265+
266+
/**
267+
* Returns the number of tested functions.
268+
*
269+
* @return integer
270+
*/
271+
public function getNumTestedFunctions()
272+
{
273+
if ($this->numTestedFunctions === NULL) {
274+
$this->numTestedFunctions = 0;
275+
276+
foreach ($this->functions as $function) {
277+
if ($function['executableLines'] > 0 &&
278+
$function['coverage'] == 100) {
279+
$this->numTestedFunctions++;
280+
}
281+
}
282+
}
283+
284+
return $this->numTestedFunctions;
285+
}
286+
249287
/**
250288
* Calculates coverage statistics for the file.
251289
*/
@@ -324,22 +362,20 @@ protected function calculateStatistics()
324362
$class['ccn'] += $method['ccn'];
325363
}
326364

327-
if ($className != '*') {
328-
if ($class['executableLines'] > 0) {
329-
$class['coverage'] = ($class['executedLines'] /
330-
$class['executableLines']) * 100;
331-
} else {
332-
$class['coverage'] = 100;
333-
}
334-
335-
if ($class['coverage'] == 100) {
336-
$this->numTestedClasses++;
337-
}
365+
if ($class['executableLines'] > 0) {
366+
$class['coverage'] = ($class['executedLines'] /
367+
$class['executableLines']) * 100;
368+
} else {
369+
$class['coverage'] = 100;
370+
}
338371

339-
$class['crap'] = PHP_CodeCoverage_Util::crap(
340-
$class['ccn'], $class['coverage']
341-
);
372+
if ($class['coverage'] == 100) {
373+
$this->numTestedClasses++;
342374
}
375+
376+
$class['crap'] = PHP_CodeCoverage_Util::crap(
377+
$class['ccn'], $class['coverage']
378+
);
343379
}
344380
}
345381

@@ -392,27 +428,17 @@ protected function processFunctions()
392428
$functions = $tokens->getFunctions();
393429
unset($tokens);
394430

395-
if (count($functions) > 0 && !isset($this->classes['*'])) {
396-
$this->classes['*'] = array(
397-
'methods' => array(),
398-
'startLine' => 0,
399-
'executableLines' => 0,
400-
'executedLines' => 0,
401-
'ccn' => 0
402-
);
403-
}
404-
405431
foreach ($functions as $functionName => $function) {
406-
$this->classes['*']['methods'][$functionName] = array(
432+
$this->functions[$functionName] = array(
407433
'signature' => $function['signature'],
408434
'startLine' => $function['startLine'],
409435
'executableLines' => 0,
410436
'executedLines' => 0,
411437
'ccn' => $function['ccn']
412438
);
413439

414-
$this->startLines[$function['startLine']] = &$this->classes['*']['methods'][$functionName];
415-
$this->endLines[$function['endLine']] = &$this->classes['*']['methods'][$functionName];
440+
$this->startLines[$function['startLine']] = &$this->functions[$functionName];
441+
$this->endLines[$function['endLine']] = &$this->functions[$functionName];
416442
}
417443
}
418444
}

Tests/PHP/CodeCoverage/Report/FactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,7 @@ public function testSomething()
8484
$this->assertEquals(0, $root->getTestedClassesPercent());
8585
$this->assertEquals(75, $root->getTestedMethodsPercent());
8686
$this->assertEquals(50, $root->getLineExecutedPercent());
87+
$this->assertEquals(0, $root->getNumFunctions());
88+
$this->assertEquals(0, $root->getNumFunctions());
8789
}
8890
}

0 commit comments

Comments
 (0)