Skip to content

Commit 8f14683

Browse files
More work on support for traits.
1 parent 8b698cf commit 8f14683

File tree

3 files changed

+186
-0
lines changed

3 files changed

+186
-0
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 traits of this node.
164+
*
165+
* @return array
166+
*/
167+
abstract public function getTraits();
168+
162169
/**
163170
* Returns the functions of this node.
164171
*
@@ -194,6 +201,20 @@ abstract public function getNumClasses();
194201
*/
195202
abstract public function getNumTestedClasses();
196203

204+
/**
205+
* Returns the number of traits.
206+
*
207+
* @return integer
208+
*/
209+
abstract public function getNumTraits();
210+
211+
/**
212+
* Returns the number of tested traits.
213+
*
214+
* @return integer
215+
*/
216+
abstract public function getNumTestedTraits();
217+
197218
/**
198219
* Returns the number of methods.
199220
*

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 $traits;
84+
8085
/**
8186
* @var array
8287
*/
@@ -102,6 +107,16 @@ class PHP_CodeCoverage_Report_Node_Directory extends PHP_CodeCoverage_Report_Nod
102107
*/
103108
protected $numTestedClasses = -1;
104109

110+
/**
111+
* @var integer
112+
*/
113+
protected $numTraits = -1;
114+
115+
/**
116+
* @var integer
117+
*/
118+
protected $numTestedTraits = -1;
119+
105120
/**
106121
* @var integer
107122
*/
@@ -229,6 +244,26 @@ public function getClasses()
229244
return $this->classes;
230245
}
231246

247+
/**
248+
* Returns the traits of this node.
249+
*
250+
* @return array
251+
*/
252+
public function getTraits()
253+
{
254+
if ($this->traits === NULL) {
255+
$this->traits = array();
256+
257+
foreach ($this->children as $child) {
258+
$this->traits = array_merge(
259+
$this->traits, $child->getTraits()
260+
);
261+
}
262+
}
263+
264+
return $this->traits;
265+
}
266+
232267
/**
233268
* Returns the functions of this node.
234269
*
@@ -321,6 +356,42 @@ public function getNumTestedClasses()
321356
return $this->numTestedClasses;
322357
}
323358

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

PHP/CodeCoverage/Report/Node/File.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ class PHP_CodeCoverage_Report_Node_File extends PHP_CodeCoverage_Report_Node
9696
*/
9797
protected $classes = array();
9898

99+
/**
100+
* @var array
101+
*/
102+
protected $traits = array();
103+
99104
/**
100105
* @var array
101106
*/
@@ -163,6 +168,16 @@ public function getClasses()
163168
return $this->classes;
164169
}
165170

171+
/**
172+
* Returns the traits of this node.
173+
*
174+
* @return array
175+
*/
176+
public function getTraits()
177+
{
178+
return $this->traits;
179+
}
180+
166181
/**
167182
* Returns the functions of this node.
168183
*
@@ -213,6 +228,26 @@ public function getNumTestedClasses()
213228
return $this->numTestedClasses;
214229
}
215230

231+
/**
232+
* Returns the number of traits.
233+
*
234+
* @return integer
235+
*/
236+
public function getNumTraits()
237+
{
238+
return count($this->traits);
239+
}
240+
241+
/**
242+
* Returns the number of tested traits.
243+
*
244+
* @return integer
245+
*/
246+
public function getNumTestedTraits()
247+
{
248+
return $this->numTestedTraits;
249+
}
250+
216251
/**
217252
* Returns the number of methods.
218253
*
@@ -230,6 +265,14 @@ public function getNumMethods()
230265
}
231266
}
232267
}
268+
269+
foreach ($this->traits as $trait) {
270+
foreach ($trait['methods'] as $method) {
271+
if ($method['executableLines'] > 0) {
272+
$this->numMethods++;
273+
}
274+
}
275+
}
233276
}
234277

235278
return $this->numMethods;
@@ -253,6 +296,15 @@ public function getNumTestedMethods()
253296
}
254297
}
255298
}
299+
300+
foreach ($this->traits as $trait) {
301+
foreach ($trait['methods'] as $method) {
302+
if ($method['executableLines'] > 0 &&
303+
$method['coverage'] == 100) {
304+
$this->numTestedMethods++;
305+
}
306+
}
307+
}
256308
}
257309

258310
return $this->numTestedMethods;
@@ -304,6 +356,7 @@ public function isLineIgnored($line)
304356
protected function calculateStatistics()
305357
{
306358
$this->processClasses();
359+
$this->processTraits();
307360
$this->processFunctions();
308361

309362
$max = count(file($this->getPath()));
@@ -434,6 +487,47 @@ protected function processClasses()
434487
}
435488
}
436489

490+
/**
491+
*
492+
*/
493+
protected function processTraits()
494+
{
495+
$tokens = PHP_Token_Stream_CachingFactory::get($this->getPath());
496+
$traits = $tokens->getTraits();
497+
unset($tokens);
498+
499+
foreach ($traits as $traitName => $trait) {
500+
$this->traits[$traitName] = array(
501+
'methods' => array(),
502+
'startLine' => $trait['startLine'],
503+
'executableLines' => 0,
504+
'executedLines' => 0,
505+
'ccn' => 0,
506+
'coverage' => 0,
507+
'crap' => 0,
508+
'package' => $trait['package']
509+
);
510+
511+
$this->startLines[$trait['startLine']] = &$this->traits[$traitName];
512+
$this->endLines[$trait['endLine']] = &$this->traits[$traitName];
513+
514+
foreach ($trait['methods'] as $methodName => $method) {
515+
$this->traits[$traitName]['methods'][$methodName] = array(
516+
'signature' => $method['signature'],
517+
'startLine' => $method['startLine'],
518+
'executableLines' => 0,
519+
'executedLines' => 0,
520+
'ccn' => $method['ccn'],
521+
'coverage' => 0,
522+
'crap' => 0
523+
);
524+
525+
$this->startLines[$method['startLine']] = &$this->traits[$traitName]['methods'][$methodName];
526+
$this->endLines[$method['endLine']] = &$this->traits[$traitName]['methods'][$methodName];
527+
}
528+
}
529+
}
530+
437531
/**
438532
*
439533
*/

0 commit comments

Comments
 (0)