3
* This file is part of the PHP_CodeCoverage package.
5
* (c) Sebastian Bergmann <sebastian@phpunit.de>
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
12
* Factory for PHP_CodeCoverage_Report_Node_* object graphs.
15
* @package CodeCoverage
16
* @author Sebastian Bergmann <sebastian@phpunit.de>
17
* @copyright Sebastian Bergmann <sebastian@phpunit.de>
18
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
19
* @link http://github.com/sebastianbergmann/php-code-coverage
20
* @since Class available since Release 1.1.0
22
class PHP_CodeCoverage_Report_Factory
25
* @param PHP_CodeCoverage $coverage
27
public function create(PHP_CodeCoverage $coverage)
29
$files = $coverage->getData();
30
$commonPath = $this->reducePaths($files);
31
$root = new PHP_CodeCoverage_Report_Node_Directory(
37
$this->buildDirectoryStructure($files),
38
$coverage->getTests(),
39
$coverage->getCacheTokens()
46
* @param PHP_CodeCoverage_Report_Node_Directory $root
49
* @param boolean $cacheTokens
51
private function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array $items, array $tests, $cacheTokens)
53
foreach ($items as $key => $value) {
54
if (substr($key, -2) == '/f') {
55
$key = substr($key, 0, -2);
57
if (file_exists($root->getPath() . DIRECTORY_SEPARATOR . $key)) {
58
$root->addFile($key, $value, $tests, $cacheTokens);
61
$child = $root->addDirectory($key);
62
$this->addItems($child, $value, $tests, $cacheTokens);
68
* Builds an array representation of the directory structure.
75
* [Money.php] => Array
80
* [MoneyBag.php] => Array
94
* [Money.php] => Array
99
* [MoneyBag.php] => Array
107
* @param array $files
110
private function buildDirectoryStructure($files)
114
foreach ($files as $path => $file) {
115
$path = explode('/', $path);
119
for ($i = 0; $i < $max; $i++) {
120
if ($i == ($max - 1)) {
126
$pointer = &$pointer[$path[$i] . $type];
136
* Reduces the paths by cutting the longest common start path.
143
* [/home/sb/Money/Money.php] => Array
148
* [/home/sb/Money/MoneyBag.php] => Array
160
* [Money.php] => Array
165
* [MoneyBag.php] => Array
172
* @param array $files
175
private function reducePaths(&$files)
182
$paths = array_keys($files);
184
if (count($files) == 1) {
185
$commonPath = dirname($paths[0]) . '/';
186
$files[basename($paths[0])] = $files[$paths[0]];
188
unset($files[$paths[0]]);
193
$max = count($paths);
195
for ($i = 0; $i < $max; $i++) {
196
// strip phar:// prefixes
197
if (strpos($paths[$i], 'phar://') === 0) {
198
$paths[$i] = substr($paths[$i], 7);
199
$paths[$i] = strtr($paths[$i], '/', DIRECTORY_SEPARATOR);
201
$paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
203
if (empty($paths[$i][0])) {
204
$paths[$i][0] = DIRECTORY_SEPARATOR;
209
$max = count($paths);
212
for ($i = 0; $i < $max - 1; $i++) {
213
if (!isset($paths[$i][0]) ||
214
!isset($paths[$i+1][0]) ||
215
$paths[$i][0] != $paths[$i+1][0]) {
222
$commonPath .= $paths[0][0];
224
if ($paths[0][0] != DIRECTORY_SEPARATOR) {
225
$commonPath .= DIRECTORY_SEPARATOR;
228
for ($i = 0; $i < $max; $i++) {
229
array_shift($paths[$i]);
234
$original = array_keys($files);
235
$max = count($original);
237
for ($i = 0; $i < $max; $i++) {
238
$files[join('/', $paths[$i])] = $files[$original[$i]];
239
unset($files[$original[$i]]);
244
return substr($commonPath, 0, -1);