Skip to content

Commit dd33b69

Browse files
Implement link generation.
1 parent a03673e commit dd33b69

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

PHP/CodeCoverage/Report/Node.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ abstract class PHP_CodeCoverage_Report_Node implements Countable
8080
*/
8181
public function __construct($name, PHP_CodeCoverage_Report_Node $parent = NULL)
8282
{
83+
if (substr($name, -1) == '/') {
84+
$name = substr($name, 0, -1);
85+
}
86+
8387
$this->name = $name;
8488
$this->parent = $parent;
8589
}

PHP/CodeCoverage/Report/Node/File.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ protected function processClasses(PHP_Token_Stream $tokens)
485485
$classes = $tokens->getClasses();
486486
unset($tokens);
487487

488+
$link = PHP_CodeCoverage_Util::nodeToId($this) . '.html#';
489+
488490
foreach ($classes as $className => $class) {
489491
$this->classes[$className] = array(
490492
'methods' => array(),
@@ -494,7 +496,8 @@ protected function processClasses(PHP_Token_Stream $tokens)
494496
'ccn' => 0,
495497
'coverage' => 0,
496498
'crap' => 0,
497-
'package' => $class['package']
499+
'package' => $class['package'],
500+
'link' => $link . $class['startLine']
498501
);
499502

500503
$this->startLines[$class['startLine']] = &$this->classes[$className];
@@ -509,7 +512,8 @@ protected function processClasses(PHP_Token_Stream $tokens)
509512
'executedLines' => 0,
510513
'ccn' => $method['ccn'],
511514
'coverage' => 0,
512-
'crap' => 0
515+
'crap' => 0,
516+
'link' => $link . $method['startLine']
513517
);
514518

515519
$this->startLines[$method['startLine']] = &$this->classes[$className]['methods'][$methodName];

PHP/CodeCoverage/Util.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ class PHP_CodeCoverage_Util
8585
'setUp', 'assertPreConditions', 'assertPostConditions', 'tearDown'
8686
);
8787

88+
/**
89+
* @var array
90+
*/
91+
protected static $ids = array();
92+
8893
/**
8994
* Builds an array representation of the directory structure.
9095
*
@@ -476,6 +481,32 @@ public static function reducePaths(&$files)
476481
return substr($commonPath, 0, -1);
477482
}
478483

484+
/**
485+
* @param PHP_CodeCoverage_Report_Node $node
486+
* @return string
487+
*/
488+
public static function nodeToId(PHP_CodeCoverage_Report_Node $node)
489+
{
490+
if (!isset(self::$ids[$node->getPath()])) {
491+
$parent = $node->getParent();
492+
493+
if ($parent === NULL) {
494+
self::$ids[$node->getPath()] = 'index';
495+
} else {
496+
$parentId = self::nodeToId($parent);
497+
498+
if ($parentId == 'index') {
499+
self::$ids[$node->getPath()] = $node->getName();
500+
} else {
501+
self::$ids[$node->getPath()] = $parentId . '_' .
502+
$node->getName();
503+
}
504+
}
505+
}
506+
507+
return self::$ids[$node->getPath()];
508+
}
509+
479510
/**
480511
* @param string $coveredElement
481512
* @return array

Tests/PHP/CodeCoverage/Report/FactoryTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public function testSomething()
7373
{
7474
$root = $this->getCoverageForBankAccount()->getReport();
7575

76-
$this->assertEquals('/usr/local/src/code-coverage/Tests/_files/', $root->getName());
77-
$this->assertEquals('/usr/local/src/code-coverage/Tests/_files/', $root->getPath());
76+
$this->assertEquals('/usr/local/src/code-coverage/Tests/_files', $root->getName());
77+
$this->assertEquals('/usr/local/src/code-coverage/Tests/_files', $root->getPath());
7878
$this->assertEquals(10, $root->getNumExecutableLines());
7979
$this->assertEquals(5, $root->getNumExecutedLines());
8080
$this->assertEquals(1, $root->getNumClasses());
@@ -103,7 +103,8 @@ public function testSomething()
103103
'executedLines' => 1,
104104
'ccn' => 1,
105105
'coverage' => 100,
106-
'crap' => '1'
106+
'crap' => '1',
107+
'link' => 'BankAccount.php.html#6'
107108
),
108109
'setBalance' => array(
109110
'signature' => 'setBalance($balance)',
@@ -113,7 +114,8 @@ public function testSomething()
113114
'executedLines' => 0,
114115
'ccn' => 2,
115116
'coverage' => 0,
116-
'crap' => 6
117+
'crap' => 6,
118+
'link' => 'BankAccount.php.html#11'
117119
),
118120
'depositMoney' => array(
119121
'signature' => 'depositMoney($balance)',
@@ -123,7 +125,8 @@ public function testSomething()
123125
'executedLines' => 2,
124126
'ccn' => 1,
125127
'coverage' => 100,
126-
'crap' => '1'
128+
'crap' => '1',
129+
'link' => 'BankAccount.php.html#20'
127130
),
128131
'withdrawMoney' => array(
129132
'signature' => 'withdrawMoney($balance)',
@@ -133,7 +136,8 @@ public function testSomething()
133136
'executedLines' => 2,
134137
'ccn' => 1,
135138
'coverage' => 100,
136-
'crap' => '1'
139+
'crap' => '1',
140+
'link' => 'BankAccount.php.html#27'
137141
),
138142
),
139143
'startLine' => 2,
@@ -148,7 +152,8 @@ public function testSomething()
148152
'category' => '',
149153
'package' => '',
150154
'subpackage' => ''
151-
)
155+
),
156+
'link' => 'BankAccount.php.html#2'
152157
)
153158
),
154159
$root->getClasses()

0 commit comments

Comments
 (0)