PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/php-code-coverage/src/Node/Builder.php

https://gitlab.com/alamindev/videogallery
PHP | 244 lines | 109 code | 30 blank | 105 comment | 23 complexity | 32e7bcfc9a4a0aca49b4a1ddf9087e7a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the php-code-coverage package.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\CodeCoverage\Node;
  11. use SebastianBergmann\CodeCoverage\CodeCoverage;
  12. class Builder
  13. {
  14. /**
  15. * @param CodeCoverage $coverage
  16. *
  17. * @return Directory
  18. */
  19. public function build(CodeCoverage $coverage)
  20. {
  21. $files = $coverage->getData();
  22. $commonPath = $this->reducePaths($files);
  23. $root = new Directory(
  24. $commonPath,
  25. null
  26. );
  27. $this->addItems(
  28. $root,
  29. $this->buildDirectoryStructure($files),
  30. $coverage->getTests(),
  31. $coverage->getCacheTokens()
  32. );
  33. return $root;
  34. }
  35. /**
  36. * @param Directory $root
  37. * @param array $items
  38. * @param array $tests
  39. * @param bool $cacheTokens
  40. */
  41. private function addItems(Directory $root, array $items, array $tests, $cacheTokens)
  42. {
  43. foreach ($items as $key => $value) {
  44. if (substr($key, -2) == '/f') {
  45. $key = substr($key, 0, -2);
  46. if (file_exists($root->getPath() . DIRECTORY_SEPARATOR . $key)) {
  47. $root->addFile($key, $value, $tests, $cacheTokens);
  48. }
  49. } else {
  50. $child = $root->addDirectory($key);
  51. $this->addItems($child, $value, $tests, $cacheTokens);
  52. }
  53. }
  54. }
  55. /**
  56. * Builds an array representation of the directory structure.
  57. *
  58. * For instance,
  59. *
  60. * <code>
  61. * Array
  62. * (
  63. * [Money.php] => Array
  64. * (
  65. * ...
  66. * )
  67. *
  68. * [MoneyBag.php] => Array
  69. * (
  70. * ...
  71. * )
  72. * )
  73. * </code>
  74. *
  75. * is transformed into
  76. *
  77. * <code>
  78. * Array
  79. * (
  80. * [.] => Array
  81. * (
  82. * [Money.php] => Array
  83. * (
  84. * ...
  85. * )
  86. *
  87. * [MoneyBag.php] => Array
  88. * (
  89. * ...
  90. * )
  91. * )
  92. * )
  93. * </code>
  94. *
  95. * @param array $files
  96. *
  97. * @return array
  98. */
  99. private function buildDirectoryStructure($files)
  100. {
  101. $result = [];
  102. foreach ($files as $path => $file) {
  103. $path = explode('/', $path);
  104. $pointer = &$result;
  105. $max = count($path);
  106. for ($i = 0; $i < $max; $i++) {
  107. if ($i == ($max - 1)) {
  108. $type = '/f';
  109. } else {
  110. $type = '';
  111. }
  112. $pointer = &$pointer[$path[$i] . $type];
  113. }
  114. $pointer = $file;
  115. }
  116. return $result;
  117. }
  118. /**
  119. * Reduces the paths by cutting the longest common start path.
  120. *
  121. * For instance,
  122. *
  123. * <code>
  124. * Array
  125. * (
  126. * [/home/sb/Money/Money.php] => Array
  127. * (
  128. * ...
  129. * )
  130. *
  131. * [/home/sb/Money/MoneyBag.php] => Array
  132. * (
  133. * ...
  134. * )
  135. * )
  136. * </code>
  137. *
  138. * is reduced to
  139. *
  140. * <code>
  141. * Array
  142. * (
  143. * [Money.php] => Array
  144. * (
  145. * ...
  146. * )
  147. *
  148. * [MoneyBag.php] => Array
  149. * (
  150. * ...
  151. * )
  152. * )
  153. * </code>
  154. *
  155. * @param array $files
  156. *
  157. * @return string
  158. */
  159. private function reducePaths(&$files)
  160. {
  161. if (empty($files)) {
  162. return '.';
  163. }
  164. $commonPath = '';
  165. $paths = array_keys($files);
  166. if (count($files) == 1) {
  167. $commonPath = dirname($paths[0]) . '/';
  168. $files[basename($paths[0])] = $files[$paths[0]];
  169. unset($files[$paths[0]]);
  170. return $commonPath;
  171. }
  172. $max = count($paths);
  173. for ($i = 0; $i < $max; $i++) {
  174. // strip phar:// prefixes
  175. if (strpos($paths[$i], 'phar://') === 0) {
  176. $paths[$i] = substr($paths[$i], 7);
  177. $paths[$i] = strtr($paths[$i], '/', DIRECTORY_SEPARATOR);
  178. }
  179. $paths[$i] = explode(DIRECTORY_SEPARATOR, $paths[$i]);
  180. if (empty($paths[$i][0])) {
  181. $paths[$i][0] = DIRECTORY_SEPARATOR;
  182. }
  183. }
  184. $done = false;
  185. $max = count($paths);
  186. while (!$done) {
  187. for ($i = 0; $i < $max - 1; $i++) {
  188. if (!isset($paths[$i][0]) ||
  189. !isset($paths[$i+1][0]) ||
  190. $paths[$i][0] != $paths[$i+1][0]) {
  191. $done = true;
  192. break;
  193. }
  194. }
  195. if (!$done) {
  196. $commonPath .= $paths[0][0];
  197. if ($paths[0][0] != DIRECTORY_SEPARATOR) {
  198. $commonPath .= DIRECTORY_SEPARATOR;
  199. }
  200. for ($i = 0; $i < $max; $i++) {
  201. array_shift($paths[$i]);
  202. }
  203. }
  204. }
  205. $original = array_keys($files);
  206. $max = count($original);
  207. for ($i = 0; $i < $max; $i++) {
  208. $files[implode('/', $paths[$i])] = $files[$original[$i]];
  209. unset($files[$original[$i]]);
  210. }
  211. ksort($files);
  212. return substr($commonPath, 0, -1);
  213. }
  214. }