5 * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.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 <sb@sebastian-bergmann.de>
40 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD 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 <sb@sebastian-bergmann.de>
52 * @copyright 2009-2011 Sebastian Bergmann <sb@sebastian-bergmann.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 1.1.1
55 * @link http://github.com/sebastianbergmann/php-code-coverage
56 * @since Class available since Release 1.1.0
58 class PHP_CodeCoverage_Report_Factory
61 * @param PHP_CodeCoverage $coverage
63 public function create(PHP_CodeCoverage $coverage)
65 $files = $coverage->getData();
66 $commonPath = $this->reducePaths($files);
67 $root = new PHP_CodeCoverage_Report_Node_Directory(
73 $this->buildDirectoryStructure($files),
74 $coverage->getTests(),
75 $coverage->getCacheTokens()
82 * @param PHP_CodeCoverage_Report_Node_Directory $root
85 * @param boolean $cacheTokens
87 protected function addItems(PHP_CodeCoverage_Report_Node_Directory $root, array $items, array $tests, $cacheTokens)
89 foreach ($items as $key => $value) {
90 if (substr($key, -2) == '/f') {
91 $key = substr($key, 0, -2);
92 $root->addFile($key, $value, $tests, $cacheTokens);
94 $child = $root->addDirectory($key);
95 $this->addItems($child, $value, $tests, $cacheTokens);
101 * Builds an array representation of the directory structure.
108 * [Money.php] => Array
113 * [MoneyBag.php] => Array
120 * is transformed into
127 * [Money.php] => Array
132 * [MoneyBag.php] => Array
140 * @param array $files
143 protected function buildDirectoryStructure($files)
147 foreach ($files as $path => $file) {
148 $path = explode('/', $path);
152 for ($i = 0; $i < $max; $i++) {
153 if ($i == ($max - 1)) {
159 $pointer = &$pointer[$path[$i] . $type];
169 * Reduces the paths by cutting the longest common start path.
176 * [/home/sb/Money/Money.php] => Array
181 * [/home/sb/Money/MoneyBag.php] => Array
193 * [Money.php] => Array
198 * [MoneyBag.php] => Array
205 * @param array $files
208 protected function reducePaths(&$files)
215 $paths = array_keys($files);
217 if (count($files) == 1) {
218 $commonPath = dirname($paths[0]) . '/';
219 $files[basename($paths[0])] = $files[$paths[0]];
221 unset($files[$paths[0]]);
226 $max = count($paths);
228 for ($i = 0; $i < $max; $i++) {
229 $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
231 if (empty($paths[$i][0])) {
232 $paths[$i][0] = DIRECTORY_SEPARATOR;
237 $max = count($paths);
240 for ($i = 0; $i < $max - 1; $i++) {
241 if (!isset($paths[$i][0]) ||
242 !isset($paths[$i+1][0]) ||
243 $paths[$i][0] != $paths[$i+1][0]) {
250 $commonPath .= $paths[0][0];
252 if ($paths[0][0] != DIRECTORY_SEPARATOR) {
253 $commonPath .= DIRECTORY_SEPARATOR;
256 for ($i = 0; $i < $max; $i++) {
257 array_shift($paths[$i]);
262 $original = array_keys($files);
263 $max = count($original);
265 for ($i = 0; $i < $max; $i++) {
266 $files[join('/', $paths[$i])] = $files[$original[$i]];
267 unset($files[$original[$i]]);
272 return substr($commonPath, 0, -1);