PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/tests/phpunit/PHPUnit/Util/Metrics/File.php

https://github.com/mwturnage/frapi-1
PHP | 328 lines | 145 code | 36 blank | 147 comment | 12 complexity | e9cae4ae7923cf4a4a6addd2f34b1525 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2009, 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 Testing
  38. * @package PHPUnit
  39. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  40. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @version SVN: $Id: File.php 4404 2008-12-31 09:27:18Z sb $
  43. * @link http://www.phpunit.de/
  44. * @since File available since Release 3.2.0
  45. */
  46. require_once 'PHPUnit/Util/Class.php';
  47. require_once 'PHPUnit/Util/Metrics.php';
  48. require_once 'PHPUnit/Util/Filter.php';
  49. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  50. /**
  51. * File-Level Metrics.
  52. *
  53. * @category Testing
  54. * @package PHPUnit
  55. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: @package_version@
  59. * @link http://www.phpunit.de/
  60. * @since Class available since Release 3.2.0
  61. */
  62. class PHPUnit_Util_Metrics_File extends PHPUnit_Util_Metrics
  63. {
  64. protected $coverage = 0;
  65. protected $loc = 0;
  66. protected $cloc = 0;
  67. protected $ncloc = 0;
  68. protected $locExecutable = 0;
  69. protected $locExecuted = 0;
  70. protected $filename;
  71. protected $classes = array();
  72. protected $functions = array();
  73. protected $lines = array();
  74. protected $tokens = array();
  75. protected static $cache = array();
  76. /**
  77. * Constructor.
  78. *
  79. * @param string $filename
  80. * @param array $codeCoverage
  81. * @throws RuntimeException
  82. */
  83. protected function __construct($filename, &$codeCoverage = array())
  84. {
  85. if (!file_exists($filename)) {
  86. throw new RuntimeException(
  87. sprintf(
  88. 'File "%s" not found.',
  89. $filename
  90. )
  91. );
  92. }
  93. $this->filename = $filename;
  94. $this->lines = file($filename);
  95. $this->tokens = token_get_all(file_get_contents($filename));
  96. $this->countLines();
  97. $this->setCoverage($codeCoverage);
  98. foreach (PHPUnit_Util_Class::getClassesInFile($filename) as $class) {
  99. $this->classes[$class->getName()] = PHPUnit_Util_Metrics_Class::factory($class, $codeCoverage);
  100. }
  101. foreach (PHPUnit_Util_Class::getFunctionsInFile($filename) as $function) {
  102. $this->functions[$function->getName()] = PHPUnit_Util_Metrics_Function::factory($function, $codeCoverage);
  103. }
  104. }
  105. /**
  106. * Factory.
  107. *
  108. * @param string $filename
  109. * @param array $codeCoverage
  110. * @return PHPUnit_Util_Metrics_File
  111. */
  112. public static function factory($filename, &$codeCoverage = array())
  113. {
  114. if (!isset(self::$cache[$filename])) {
  115. self::$cache[$filename] = new PHPUnit_Util_Metrics_File($filename, $codeCoverage);
  116. }
  117. else if (!empty($codeCoverage) && self::$cache[$filename]->getCoverage() == 0) {
  118. self::$cache[$filename]->setCoverage($codeCoverage);
  119. }
  120. return self::$cache[$filename];
  121. }
  122. /**
  123. * @param array $codeCoverage
  124. */
  125. public function setCoverage(array &$codeCoverage)
  126. {
  127. if (!empty($codeCoverage)) {
  128. $this->calculateCodeCoverage($codeCoverage);
  129. foreach ($this->classes as $class) {
  130. $class->setCoverage($codeCoverage);
  131. }
  132. foreach ($this->functions as $function) {
  133. $function->setCoverage($codeCoverage);
  134. }
  135. }
  136. }
  137. /**
  138. * Returns the path to the file.
  139. *
  140. * @return string
  141. */
  142. public function getPath()
  143. {
  144. return $this->filename;
  145. }
  146. /**
  147. * Classes.
  148. *
  149. * @return array
  150. */
  151. public function getClasses()
  152. {
  153. return $this->classes;
  154. }
  155. /**
  156. * A class.
  157. *
  158. * @param string $className
  159. * @return ReflectionClass
  160. */
  161. public function getClass($className)
  162. {
  163. return $this->classes[$className];
  164. }
  165. /**
  166. * Functions.
  167. *
  168. * @return array
  169. */
  170. public function getFunctions()
  171. {
  172. return $this->functions;
  173. }
  174. /**
  175. * A function.
  176. *
  177. * @param string $functionName
  178. * @return ReflectionClass
  179. */
  180. public function getFunction($functionName)
  181. {
  182. return $this->functions[$functionName];
  183. }
  184. /**
  185. * Lines.
  186. *
  187. * @return array
  188. */
  189. public function getLines()
  190. {
  191. return $this->lines;
  192. }
  193. /**
  194. * Tokens.
  195. *
  196. * @return array
  197. */
  198. public function getTokens()
  199. {
  200. return $this->tokens;
  201. }
  202. /**
  203. * Returns the Code Coverage for the file.
  204. *
  205. * @return float
  206. */
  207. public function getCoverage()
  208. {
  209. return $this->coverage;
  210. }
  211. /**
  212. * Lines of Code (LOC).
  213. *
  214. * @return int
  215. */
  216. public function getLoc()
  217. {
  218. return $this->loc;
  219. }
  220. /**
  221. * Executable Lines of Code (ELOC).
  222. *
  223. * @return int
  224. */
  225. public function getLocExecutable()
  226. {
  227. return $this->locExecutable;
  228. }
  229. /**
  230. * Executed Lines of Code.
  231. *
  232. * @return int
  233. */
  234. public function getLocExecuted()
  235. {
  236. return $this->locExecuted;
  237. }
  238. /**
  239. * Comment Lines of Code (CLOC).
  240. *
  241. * @return int
  242. */
  243. public function getCloc()
  244. {
  245. return $this->cloc;
  246. }
  247. /**
  248. * Non-Comment Lines of Code (NCLOC).
  249. *
  250. * @return int
  251. */
  252. public function getNcloc()
  253. {
  254. return $this->ncloc;
  255. }
  256. /**
  257. * Calculates the Code Coverage for the class.
  258. *
  259. * @param array $codeCoverage
  260. */
  261. protected function calculateCodeCoverage(&$codeCoverage)
  262. {
  263. $statistics = PHPUnit_Util_CodeCoverage::getStatistics(
  264. $codeCoverage,
  265. $this->filename,
  266. 1,
  267. count($this->lines)
  268. );
  269. $this->coverage = $statistics['coverage'];
  270. $this->loc = $statistics['loc'];
  271. $this->locExecutable = $statistics['locExecutable'];
  272. $this->locExecuted = $statistics['locExecuted'];
  273. }
  274. /**
  275. */
  276. protected function countLines()
  277. {
  278. $this->loc = count($this->lines);
  279. $this->cloc = 0;
  280. foreach ($this->tokens as $i => $token) {
  281. if (is_string($token)) {
  282. continue;
  283. }
  284. list ($token, $value) = $token;
  285. if ($token == T_COMMENT || $token == T_DOC_COMMENT) {
  286. $this->cloc += count(explode("\n", $value));
  287. }
  288. }
  289. $this->ncloc = $this->loc - $this->cloc;
  290. }
  291. }
  292. ?>