add myCalendar into
[awl.git] / vendor / phpunit / php-code-coverage / PHP / CodeCoverage / Report / Factory.php
blobab0f4005b297b784d163f3f83884d74a491cac84
1 <?php
2 /**
3 * PHP_CodeCoverage
5 * Copyright (c) 2009-2013, Sebastian Bergmann <sebastian@phpunit.de>.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
18 * distribution.
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.
37 * @category PHP
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
46 /**
47 * Factory for PHP_CodeCoverage_Report_Node_* object graphs.
49 * @category PHP
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
59 /**
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(
67 $commonPath, NULL
70 $this->addItems(
71 $root,
72 $this->buildDirectoryStructure($files),
73 $coverage->getTests(),
74 $coverage->getCacheTokens()
77 return $root;
80 /**
81 * @param PHP_CodeCoverage_Report_Node_Directory $root
82 * @param array $items
83 * @param array $tests
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);
95 } else {
96 $child = $root->addDirectory($key);
97 $this->addItems($child, $value, $tests, $cacheTokens);
103 * Builds an array representation of the directory structure.
105 * For instance,
107 * <code>
108 * Array
110 * [Money.php] => Array
112 * ...
115 * [MoneyBag.php] => Array
117 * ...
120 * </code>
122 * is transformed into
124 * <code>
125 * Array
127 * [.] => Array
129 * [Money.php] => Array
131 * ...
134 * [MoneyBag.php] => Array
136 * ...
140 * </code>
142 * @param array $files
143 * @return array
145 protected function buildDirectoryStructure($files)
147 $result = array();
149 foreach ($files as $path => $file) {
150 $path = explode('/', $path);
151 $pointer = &$result;
152 $max = count($path);
154 for ($i = 0; $i < $max; $i++) {
155 if ($i == ($max - 1)) {
156 $type = '/f';
157 } else {
158 $type = '';
161 $pointer = &$pointer[$path[$i] . $type];
164 $pointer = $file;
167 return $result;
171 * Reduces the paths by cutting the longest common start path.
173 * For instance,
175 * <code>
176 * Array
178 * [/home/sb/Money/Money.php] => Array
180 * ...
183 * [/home/sb/Money/MoneyBag.php] => Array
185 * ...
188 * </code>
190 * is reduced to
192 * <code>
193 * Array
195 * [Money.php] => Array
197 * ...
200 * [MoneyBag.php] => Array
202 * ...
205 * </code>
207 * @param array $files
208 * @return string
210 protected function reducePaths(&$files)
212 if (empty($files)) {
213 return '.';
216 $commonPath = '';
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]]);
225 return $commonPath;
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;
242 $done = FALSE;
243 $max = count($paths);
245 while (!$done) {
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]) {
250 $done = TRUE;
251 break;
255 if (!$done) {
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]]);
276 ksort($files);
278 return substr($commonPath, 0, -1);