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

/script/lib/PHPUnit/Util/File.php

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