5 * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
38 * @package CodeCoverage
39 * @author Sebastian Bergmann <sebastian@phpunit.de>
40 * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
42 * @link http://github.com/sebastianbergmann/php-code-coverage
43 * @since File available since Release 1.1.0
47 * Factory for PHP_CodeCoverage_Report_Node_* object graphs.
50 * @package CodeCoverage
51 * @author Sebastian Bergmann <sebastian@phpunit.de>
52 * @copyright 2009-2013 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
54 * @link http://github.com/sebastianbergmann/php-code-coverage
55 * @since Class available since Release 1.1.0
57 class PHP_CodeCoverage_Report_Factory
60 * @param PHP_CodeCoverage $coverage
62 public function create(PHP_CodeCoverage
$coverage)
64 $files = $coverage->getData();
65 $commonPath = $this->reducePaths($files);
66 $root = new PHP_CodeCoverage_Report_Node_Directory(
72 $this->buildDirectoryStructure($files),
73 $coverage->getTests(),
74 $coverage->getCacheTokens()
81 * @param PHP_CodeCoverage_Report_Node_Directory $root
84 * @param boolean $cacheTokens
86 protected function addItems(PHP_CodeCoverage_Report_Node_Directory
$root, array $items, array $tests, $cacheTokens)
88 foreach ($items as $key => $value) {
89 if (substr($key, -2) == '/f') {
90 $key = substr($key, 0, -2);
92 if (file_exists($root->getPath() . DIRECTORY_SEPARATOR
. $key)) {
93 $root->addFile($key, $value, $tests, $cacheTokens);
96 $child = $root->addDirectory($key);
97 $this->addItems($child, $value, $tests, $cacheTokens);
103 * Builds an array representation of the directory structure.
110 * [Money.php] => Array
115 * [MoneyBag.php] => Array
122 * is transformed into
129 * [Money.php] => Array
134 * [MoneyBag.php] => Array
142 * @param array $files
145 protected function buildDirectoryStructure($files)
149 foreach ($files as $path => $file) {
150 $path = explode('/', $path);
154 for ($i = 0; $i < $max; $i++
) {
155 if ($i == ($max - 1)) {
161 $pointer = &$pointer[$path[$i] . $type];
171 * Reduces the paths by cutting the longest common start path.
178 * [/home/sb/Money/Money.php] => Array
183 * [/home/sb/Money/MoneyBag.php] => Array
195 * [Money.php] => Array
200 * [MoneyBag.php] => Array
207 * @param array $files
210 protected function reducePaths(&$files)
217 $paths = array_keys($files);
219 if (count($files) == 1) {
220 $commonPath = dirname($paths[0]) . '/';
221 $files[basename($paths[0])] = $files[$paths[0]];
223 unset($files[$paths[0]]);
228 $max = count($paths);
230 for ($i = 0; $i < $max; $i++
) {
231 // strip phar:// prefixes
232 if (strpos($paths[$i], 'phar://') === 0) {
233 $paths[$i] = substr($paths[$i], 7);
235 $paths[$i] = explode(DIRECTORY_SEPARATOR
, $paths[$i]);
237 if (empty($paths[$i][0])) {
238 $paths[$i][0] = DIRECTORY_SEPARATOR
;
243 $max = count($paths);
246 for ($i = 0; $i < $max - 1; $i++
) {
247 if (!isset($paths[$i][0]) ||
248 !isset($paths[$i+
1][0]) ||
249 $paths[$i][0] != $paths[$i+
1][0]) {
256 $commonPath .= $paths[0][0];
258 if ($paths[0][0] != DIRECTORY_SEPARATOR
) {
259 $commonPath .= DIRECTORY_SEPARATOR
;
262 for ($i = 0; $i < $max; $i++
) {
263 array_shift($paths[$i]);
268 $original = array_keys($files);
269 $max = count($original);
271 for ($i = 0; $i < $max; $i++
) {
272 $files[join('/', $paths[$i])] = $files[$original[$i]];
273 unset($files[$original[$i]]);
278 return substr($commonPath, 0, -1);