PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/PHP/CodeCoverage/Report/HTML/Node/Directory.php

https://bitbucket.org/chamilo/chamilo/
PHP | 430 lines | 187 code | 58 blank | 185 comment | 13 complexity | 3854385816dfdf271acb860779ae865e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * PHP_CodeCoverage
  4. *
  5. * Copyright (c) 2009-2011, Sebastian Bergmann <sb@sebastian-bergmann.de>.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  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.
  19. *
  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.
  23. *
  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.
  36. *
  37. * @category PHP
  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.0.0
  44. */
  45. require_once 'PHP/CodeCoverage/Report/HTML/Node/Iterator.php';
  46. /**
  47. * Represents a directory in the code coverage information tree.
  48. *
  49. * @category PHP
  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.0.3
  55. * @link http://github.com/sebastianbergmann/php-code-coverage
  56. * @since Class available since Release 1.0.0
  57. */
  58. class PHP_CodeCoverage_Report_HTML_Node_Directory extends PHP_CodeCoverage_Report_HTML_Node implements IteratorAggregate
  59. {
  60. /**
  61. * @var PHP_CodeCoverage_Report_HTML_Node[]
  62. */
  63. protected $children = array();
  64. /**
  65. * @var PHP_CodeCoverage_Report_HTML_Node_Directory[]
  66. */
  67. protected $directories = array();
  68. /**
  69. * @var PHP_CodeCoverage_Report_HTML_Node_File[]
  70. */
  71. protected $files = array();
  72. /**
  73. * @var array
  74. */
  75. protected $classes;
  76. /**
  77. * @var integer
  78. */
  79. protected $numExecutableLines = -1;
  80. /**
  81. * @var integer
  82. */
  83. protected $numExecutedLines = -1;
  84. /**
  85. * @var integer
  86. */
  87. protected $numClasses = -1;
  88. /**
  89. * @var integer
  90. */
  91. protected $numTestedClasses = -1;
  92. /**
  93. * @var integer
  94. */
  95. protected $numMethods = -1;
  96. /**
  97. * @var integer
  98. */
  99. protected $numTestedMethods = -1;
  100. /**
  101. * Returns an iterator for this node.
  102. *
  103. * @return RecursiveIteratorIterator
  104. */
  105. public function getIterator()
  106. {
  107. return new RecursiveIteratorIterator(
  108. new PHP_CodeCoverage_Report_HTML_Node_Iterator($this),
  109. RecursiveIteratorIterator::SELF_FIRST
  110. );
  111. }
  112. /**
  113. * Adds a new directory.
  114. *
  115. * @return PHP_CodeCoverage_Report_HTML_Node_Directory
  116. */
  117. public function addDirectory($name)
  118. {
  119. $directory = new PHP_CodeCoverage_Report_HTML_Node_Directory(
  120. $name, $this
  121. );
  122. $this->children[] = $directory;
  123. $this->directories[] = &$this->children[count($this->children) - 1];
  124. return $directory;
  125. }
  126. /**
  127. * Adds a new file.
  128. *
  129. * @param string $name
  130. * @param array $lines
  131. * @param boolean $yui
  132. * @param boolean $highlight
  133. * @return PHP_CodeCoverage_Report_HTML_Node_File
  134. * @throws RuntimeException
  135. */
  136. public function addFile($name, array $lines, $yui, $highlight)
  137. {
  138. $file = new PHP_CodeCoverage_Report_HTML_Node_File(
  139. $name, $this, $lines, $yui, $highlight
  140. );
  141. $this->children[] = $file;
  142. $this->files[] = &$this->children[count($this->children) - 1];
  143. $this->numExecutableLines = -1;
  144. $this->numExecutedLines = -1;
  145. return $file;
  146. }
  147. /**
  148. * Returns the directories in this directory.
  149. *
  150. * @return array
  151. */
  152. public function getDirectories()
  153. {
  154. return $this->directories;
  155. }
  156. /**
  157. * Returns the files in this directory.
  158. *
  159. * @return array
  160. */
  161. public function getFiles()
  162. {
  163. return $this->files;
  164. }
  165. /**
  166. * Returns the child nodes of this node.
  167. *
  168. * @return array
  169. */
  170. public function getChildNodes()
  171. {
  172. return $this->children;
  173. }
  174. /**
  175. * Returns the classes of this node.
  176. *
  177. * @return array
  178. */
  179. public function getClasses()
  180. {
  181. if ($this->classes === NULL) {
  182. $this->classes = array();
  183. foreach ($this->children as $child) {
  184. $this->classes = array_merge(
  185. $this->classes, $child->getClasses()
  186. );
  187. }
  188. }
  189. return $this->classes;
  190. }
  191. /**
  192. * Returns the number of executable lines.
  193. *
  194. * @return integer
  195. */
  196. public function getNumExecutableLines()
  197. {
  198. if ($this->numExecutableLines == -1) {
  199. $this->numExecutableLines = 0;
  200. foreach ($this->children as $child) {
  201. $this->numExecutableLines += $child->getNumExecutableLines();
  202. }
  203. }
  204. return $this->numExecutableLines;
  205. }
  206. /**
  207. * Returns the number of executed lines.
  208. *
  209. * @return integer
  210. */
  211. public function getNumExecutedLines()
  212. {
  213. if ($this->numExecutedLines == -1) {
  214. $this->numExecutedLines = 0;
  215. foreach ($this->children as $child) {
  216. $this->numExecutedLines += $child->getNumExecutedLines();
  217. }
  218. }
  219. return $this->numExecutedLines;
  220. }
  221. /**
  222. * Returns the number of classes.
  223. *
  224. * @return integer
  225. */
  226. public function getNumClasses()
  227. {
  228. if ($this->numClasses == -1) {
  229. $this->numClasses = 0;
  230. foreach ($this->children as $child) {
  231. $this->numClasses += $child->getNumClasses();
  232. }
  233. }
  234. return $this->numClasses;
  235. }
  236. /**
  237. * Returns the number of tested classes.
  238. *
  239. * @return integer
  240. */
  241. public function getNumTestedClasses()
  242. {
  243. if ($this->numTestedClasses == -1) {
  244. $this->numTestedClasses = 0;
  245. foreach ($this->children as $child) {
  246. $this->numTestedClasses += $child->getNumTestedClasses();
  247. }
  248. }
  249. return $this->numTestedClasses;
  250. }
  251. /**
  252. * Returns the number of methods.
  253. *
  254. * @return integer
  255. */
  256. public function getNumMethods()
  257. {
  258. if ($this->numMethods == -1) {
  259. $this->numMethods = 0;
  260. foreach ($this->children as $child) {
  261. $this->numMethods += $child->getNumMethods();
  262. }
  263. }
  264. return $this->numMethods;
  265. }
  266. /**
  267. * Returns the number of tested methods.
  268. *
  269. * @return integer
  270. */
  271. public function getNumTestedMethods()
  272. {
  273. if ($this->numTestedMethods == -1) {
  274. $this->numTestedMethods = 0;
  275. foreach ($this->children as $child) {
  276. $this->numTestedMethods += $child->getNumTestedMethods();
  277. }
  278. }
  279. return $this->numTestedMethods;
  280. }
  281. /**
  282. * Renders this node.
  283. *
  284. * @param string $target
  285. * @param string $title
  286. * @param string $charset
  287. * @param integer $lowUpperBound
  288. * @param integer $highLowerBound
  289. * @param string $generator
  290. */
  291. public function render($target, $title, $charset = 'UTF-8', $lowUpperBound = 35, $highLowerBound = 70, $generator = '')
  292. {
  293. $this->doRender(
  294. $target, $title, $charset, $lowUpperBound, $highLowerBound, $generator
  295. );
  296. foreach ($this->children as $child) {
  297. $child->render(
  298. $target,
  299. $title,
  300. $charset,
  301. $lowUpperBound,
  302. $highLowerBound,
  303. $generator
  304. );
  305. }
  306. $this->children = array();
  307. }
  308. /**
  309. * @param string $target
  310. * @param string $title
  311. * @param string $charset
  312. * @param integer $lowUpperBound
  313. * @param integer $highLowerBound
  314. * @param string $generator
  315. */
  316. protected function doRender($target, $title, $charset, $lowUpperBound, $highLowerBound, $generator)
  317. {
  318. $cleanId = PHP_CodeCoverage_Util::getSafeFilename($this->getId());
  319. $file = $target . $cleanId . '.html';
  320. $template = new Text_Template(
  321. PHP_CodeCoverage_Report_HTML::$templatePath . 'directory.html'
  322. );
  323. $this->setTemplateVars($template, $title, $charset, $generator);
  324. $template->setVar(
  325. array(
  326. 'total_item' => $this->renderTotalItem(
  327. $lowUpperBound, $highLowerBound
  328. ),
  329. 'items' => $this->renderItems(
  330. $lowUpperBound, $highLowerBound
  331. ),
  332. 'low_upper_bound' => $lowUpperBound,
  333. 'high_lower_bound' => $highLowerBound
  334. )
  335. );
  336. $template->renderTo($file);
  337. $this->directories = array();
  338. $this->files = array();
  339. }
  340. /**
  341. * @param float $lowUpperBound
  342. * @param float $highLowerBound
  343. * @return string
  344. */
  345. protected function renderItems($lowUpperBound, $highLowerBound)
  346. {
  347. $items = $this->doRenderItems(
  348. $this->directories, $lowUpperBound, $highLowerBound, 'coverDirectory'
  349. );
  350. $items .= $this->doRenderItems(
  351. $this->files, $lowUpperBound, $highLowerBound, 'coverFile'
  352. );
  353. return $items;
  354. }
  355. /**
  356. * @param array $items
  357. * @param float $lowUpperBound
  358. * @param float $highLowerBound
  359. * @param string $itemClass
  360. * @return string
  361. */
  362. protected function doRenderItems(array $items, $lowUpperBound, $highLowerBound, $itemClass)
  363. {
  364. $result = '';
  365. foreach ($items as $item) {
  366. $result .= $this->doRenderItemObject(
  367. $item, $lowUpperBound, $highLowerBound, NULL, $itemClass
  368. );
  369. }
  370. return $result;
  371. }
  372. }