PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/PHPUnit/Util/File.php

https://github.com/grandison/budo16
PHP | 364 lines | 234 code | 46 blank | 84 comment | 54 complexity | f0ccb87eacdf23972dde1ebd8f831605 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPUnit
  4. *
  5. * Copyright (c) 2002-2010, 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-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. * @link http://www.phpunit.de/
  43. * @since File available since Release 3.4.0
  44. */
  45. if (!defined('T_NAMESPACE')) {
  46. define('T_NAMESPACE', 377);
  47. }
  48. require_once 'PHPUnit/Util/Filter.php';
  49. PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
  50. /**
  51. * File helpers.
  52. *
  53. * @category Testing
  54. * @package PHPUnit
  55. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  56. * @copyright 2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
  57. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  58. * @version Release: 3.4.15
  59. * @link http://www.phpunit.de/
  60. * @since Class available since Release 3.4.0
  61. */
  62. class PHPUnit_Util_File
  63. {
  64. /**
  65. * @var array
  66. */
  67. protected static $countCache = array();
  68. /**
  69. * @var array
  70. */
  71. protected static $classesFunctionsCache = array();
  72. /**
  73. * Counts LOC, CLOC, and NCLOC for a file.
  74. *
  75. * @param string $filename
  76. * @return array
  77. */
  78. public static function countLines($filename)
  79. {
  80. if (!isset(self::$countCache[$filename])) {
  81. $buffer = file_get_contents($filename);
  82. $loc = substr_count($buffer, "\n");
  83. $cloc = 0;
  84. foreach (token_get_all($buffer) as $i => $token) {
  85. if (is_string($token)) {
  86. continue;
  87. }
  88. list ($token, $value) = $token;
  89. if ($token == T_COMMENT || $token == T_DOC_COMMENT) {
  90. $cloc += substr_count($value, "\n") + 1;
  91. }
  92. }
  93. self::$countCache[$filename] = array(
  94. 'loc' => $loc, 'cloc' => $cloc, 'ncloc' => $loc - $cloc
  95. );
  96. }
  97. return self::$countCache[$filename];
  98. }
  99. /**
  100. * Returns information on the classes declared in a sourcefile.
  101. *
  102. * @param string $filename
  103. * @return array
  104. */
  105. public static function getClassesInFile($filename)
  106. {
  107. if (!isset(self::$classesFunctionsCache[$filename])) {
  108. self::parseFile($filename);
  109. }
  110. return self::$classesFunctionsCache[$filename]['classes'];
  111. }
  112. /**
  113. * Returns information on the functions declared in a sourcefile.
  114. *
  115. * @param string $filename
  116. * @return array
  117. */
  118. public static function getFunctionsInFile($filename)
  119. {
  120. if (!isset(self::$classesFunctionsCache[$filename])) {
  121. self::parseFile($filename);
  122. }
  123. return self::$classesFunctionsCache[$filename]['functions'];
  124. }
  125. /**
  126. * Parses a file for class, method, and function information.
  127. *
  128. * @param string $filename
  129. */
  130. protected static function parseFile($filename)
  131. {
  132. self::$classesFunctionsCache[$filename] = array(
  133. 'classes' => array(), 'functions' => array()
  134. );
  135. $tokens = token_get_all(
  136. file_get_contents($filename)
  137. );
  138. $numTokens = count($tokens);
  139. $blocks = array();
  140. $line = 1;
  141. $currentBlock = FALSE;
  142. $currentNamespace = FALSE;
  143. $currentClass = FALSE;
  144. $currentFunction = FALSE;
  145. $currentFunctionStartLine = FALSE;
  146. $currentFunctionTokens = array();
  147. $currentDocComment = FALSE;
  148. $currentSignature = FALSE;
  149. $currentSignatureStartToken = FALSE;
  150. for ($i = 0; $i < $numTokens; $i++) {
  151. if ($currentFunction !== FALSE) {
  152. $currentFunctionTokens[] = $tokens[$i];
  153. }
  154. if (is_string($tokens[$i])) {
  155. if ($tokens[$i] == '{') {
  156. if ($currentBlock == T_CLASS) {
  157. $block = $currentClass;
  158. }
  159. else if ($currentBlock == T_FUNCTION) {
  160. $currentSignature = '';
  161. for ($j = $currentSignatureStartToken; $j < $i; $j++) {
  162. if (is_string($tokens[$j])) {
  163. $currentSignature .= $tokens[$j];
  164. } else {
  165. $currentSignature .= $tokens[$j][1];
  166. }
  167. }
  168. $currentSignature = trim($currentSignature);
  169. $block = $currentFunction;
  170. $currentSignatureStartToken = FALSE;
  171. }
  172. else {
  173. $block = FALSE;
  174. }
  175. array_push($blocks, $block);
  176. $currentBlock = FALSE;
  177. }
  178. else if ($tokens[$i] == '}') {
  179. $block = array_pop($blocks);
  180. if ($block !== FALSE && $block !== NULL) {
  181. if ($block == $currentFunction) {
  182. if ($currentDocComment !== FALSE) {
  183. $docComment = $currentDocComment;
  184. $currentDocComment = FALSE;
  185. } else {
  186. $docComment = '';
  187. }
  188. $tmp = array(
  189. 'docComment' => $docComment,
  190. 'signature' => $currentSignature,
  191. 'startLine' => $currentFunctionStartLine,
  192. 'endLine' => $line,
  193. 'tokens' => $currentFunctionTokens
  194. );
  195. if ($currentClass === FALSE) {
  196. self::$classesFunctionsCache[$filename]['functions'][$currentFunction] = $tmp;
  197. } else {
  198. self::$classesFunctionsCache[$filename]['classes'][$currentClass]['methods'][$currentFunction] = $tmp;
  199. }
  200. $currentFunction = FALSE;
  201. $currentFunctionStartLine = FALSE;
  202. $currentFunctionTokens = array();
  203. $currentSignature = FALSE;
  204. }
  205. else if ($block == $currentClass) {
  206. self::$classesFunctionsCache[$filename]['classes'][$currentClass]['endLine'] = $line;
  207. $currentClass = FALSE;
  208. $currentClassStartLine = FALSE;
  209. }
  210. }
  211. }
  212. continue;
  213. }
  214. switch ($tokens[$i][0]) {
  215. case T_NAMESPACE: {
  216. $currentNamespace = $tokens[$i+2][1];
  217. for ($j = $i+3; $j < $numTokens; $j += 2) {
  218. if ($tokens[$j][0] == T_NS_SEPARATOR) {
  219. $currentNamespace .= '\\' . $tokens[$j+1][1];
  220. } else {
  221. break;
  222. }
  223. }
  224. }
  225. break;
  226. case T_CURLY_OPEN: {
  227. $currentBlock = T_CURLY_OPEN;
  228. array_push($blocks, $currentBlock);
  229. }
  230. break;
  231. case T_DOLLAR_OPEN_CURLY_BRACES: {
  232. $currentBlock = T_DOLLAR_OPEN_CURLY_BRACES;
  233. array_push($blocks, $currentBlock);
  234. }
  235. break;
  236. case T_CLASS: {
  237. $currentBlock = T_CLASS;
  238. if ($currentNamespace === FALSE) {
  239. $currentClass = $tokens[$i+2][1];
  240. } else {
  241. $currentClass = $currentNamespace . '\\' .
  242. $tokens[$i+2][1];
  243. }
  244. if ($currentDocComment !== FALSE) {
  245. $docComment = $currentDocComment;
  246. $currentDocComment = FALSE;
  247. } else {
  248. $docComment = '';
  249. }
  250. self::$classesFunctionsCache[$filename]['classes'][$currentClass] = array(
  251. 'methods' => array(),
  252. 'docComment' => $docComment,
  253. 'startLine' => $line
  254. );
  255. }
  256. break;
  257. case T_FUNCTION: {
  258. if (!((is_array($tokens[$i+2]) &&
  259. $tokens[$i+2][0] == T_STRING) ||
  260. (is_string($tokens[$i+2]) &&
  261. $tokens[$i+2] == '&' &&
  262. is_array($tokens[$i+3]) &&
  263. $tokens[$i+3][0] == T_STRING))) {
  264. continue;
  265. }
  266. $currentBlock = T_FUNCTION;
  267. $currentFunctionStartLine = $line;
  268. $done = FALSE;
  269. $currentSignatureStartToken = $i - 1;
  270. do {
  271. switch ($tokens[$currentSignatureStartToken][0]) {
  272. case T_ABSTRACT:
  273. case T_FINAL:
  274. case T_PRIVATE:
  275. case T_PUBLIC:
  276. case T_PROTECTED:
  277. case T_STATIC:
  278. case T_WHITESPACE: {
  279. $currentSignatureStartToken--;
  280. }
  281. break;
  282. default: {
  283. $currentSignatureStartToken++;
  284. $done = TRUE;
  285. }
  286. }
  287. }
  288. while (!$done);
  289. if (isset($tokens[$i+2][1])) {
  290. $functionName = $tokens[$i+2][1];
  291. }
  292. else if (isset($tokens[$i+3][1])) {
  293. $functionName = $tokens[$i+3][1];
  294. }
  295. if ($currentNamespace === FALSE) {
  296. $currentFunction = $functionName;
  297. } else {
  298. $currentFunction = $currentNamespace . '\\' .
  299. $functionName;
  300. }
  301. }
  302. break;
  303. case T_DOC_COMMENT: {
  304. $currentDocComment = $tokens[$i][1];
  305. }
  306. break;
  307. }
  308. $line += substr_count($tokens[$i][1], "\n");
  309. }
  310. }
  311. }
  312. ?>