Stream.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. /*
  3. * This file is part of the PHP_TokenStream 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. /**
  11. * A stream of PHP tokens.
  12. *
  13. * @author Sebastian Bergmann <sebastian@phpunit.de>
  14. * @copyright Sebastian Bergmann <sebastian@phpunit.de>
  15. * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
  16. * @link http://github.com/sebastianbergmann/php-token-stream/tree
  17. * @since Class available since Release 1.0.0
  18. */
  19. class PHP_Token_Stream implements ArrayAccess, Countable, SeekableIterator
  20. {
  21. /**
  22. * @var array
  23. */
  24. protected static $customTokens = array(
  25. '(' => 'PHP_Token_OPEN_BRACKET',
  26. ')' => 'PHP_Token_CLOSE_BRACKET',
  27. '[' => 'PHP_Token_OPEN_SQUARE',
  28. ']' => 'PHP_Token_CLOSE_SQUARE',
  29. '{' => 'PHP_Token_OPEN_CURLY',
  30. '}' => 'PHP_Token_CLOSE_CURLY',
  31. ';' => 'PHP_Token_SEMICOLON',
  32. '.' => 'PHP_Token_DOT',
  33. ',' => 'PHP_Token_COMMA',
  34. '=' => 'PHP_Token_EQUAL',
  35. '<' => 'PHP_Token_LT',
  36. '>' => 'PHP_Token_GT',
  37. '+' => 'PHP_Token_PLUS',
  38. '-' => 'PHP_Token_MINUS',
  39. '*' => 'PHP_Token_MULT',
  40. '/' => 'PHP_Token_DIV',
  41. '?' => 'PHP_Token_QUESTION_MARK',
  42. '!' => 'PHP_Token_EXCLAMATION_MARK',
  43. ':' => 'PHP_Token_COLON',
  44. '"' => 'PHP_Token_DOUBLE_QUOTES',
  45. '@' => 'PHP_Token_AT',
  46. '&' => 'PHP_Token_AMPERSAND',
  47. '%' => 'PHP_Token_PERCENT',
  48. '|' => 'PHP_Token_PIPE',
  49. '$' => 'PHP_Token_DOLLAR',
  50. '^' => 'PHP_Token_CARET',
  51. '~' => 'PHP_Token_TILDE',
  52. '`' => 'PHP_Token_BACKTICK'
  53. );
  54. /**
  55. * @var string
  56. */
  57. protected $filename;
  58. /**
  59. * @var array
  60. */
  61. protected $tokens = array();
  62. /**
  63. * @var integer
  64. */
  65. protected $position = 0;
  66. /**
  67. * @var array
  68. */
  69. protected $linesOfCode = array('loc' => 0, 'cloc' => 0, 'ncloc' => 0);
  70. /**
  71. * @var array
  72. */
  73. protected $classes;
  74. /**
  75. * @var array
  76. */
  77. protected $functions;
  78. /**
  79. * @var array
  80. */
  81. protected $includes;
  82. /**
  83. * @var array
  84. */
  85. protected $interfaces;
  86. /**
  87. * @var array
  88. */
  89. protected $traits;
  90. /**
  91. * @var array
  92. */
  93. protected $lineToFunctionMap = array();
  94. /**
  95. * Constructor.
  96. *
  97. * @param string $sourceCode
  98. */
  99. public function __construct($sourceCode)
  100. {
  101. if (is_file($sourceCode)) {
  102. $this->filename = $sourceCode;
  103. $sourceCode = file_get_contents($sourceCode);
  104. }
  105. $this->scan($sourceCode);
  106. }
  107. /**
  108. * Destructor.
  109. */
  110. public function __destruct()
  111. {
  112. $this->tokens = array();
  113. }
  114. /**
  115. * @return string
  116. */
  117. public function __toString()
  118. {
  119. $buffer = '';
  120. foreach ($this as $token) {
  121. $buffer .= $token;
  122. }
  123. return $buffer;
  124. }
  125. /**
  126. * @return string
  127. * @since Method available since Release 1.1.0
  128. */
  129. public function getFilename()
  130. {
  131. return $this->filename;
  132. }
  133. /**
  134. * Scans the source for sequences of characters and converts them into a
  135. * stream of tokens.
  136. *
  137. * @param string $sourceCode
  138. */
  139. protected function scan($sourceCode)
  140. {
  141. $id = 0;
  142. $line = 1;
  143. $tokens = token_get_all($sourceCode);
  144. $numTokens = count($tokens);
  145. $lastNonWhitespaceTokenWasDoubleColon = false;
  146. for ($i = 0; $i < $numTokens; ++$i) {
  147. $token = $tokens[$i];
  148. $skip = 0;
  149. if (is_array($token)) {
  150. $name = substr(token_name($token[0]), 2);
  151. $text = $token[1];
  152. if ($lastNonWhitespaceTokenWasDoubleColon && $name == 'CLASS') {
  153. $name = 'CLASS_NAME_CONSTANT';
  154. } elseif ($name == 'USE' && isset($tokens[$i+2][0]) && $tokens[$i+2][0] == T_FUNCTION) {
  155. $name = 'USE_FUNCTION';
  156. $skip = 2;
  157. }
  158. $tokenClass = 'PHP_Token_' . $name;
  159. } else {
  160. $text = $token;
  161. $tokenClass = self::$customTokens[$token];
  162. }
  163. $this->tokens[] = new $tokenClass($text, $line, $this, $id++);
  164. $lines = substr_count($text, "\n");
  165. $line += $lines;
  166. if ($tokenClass == 'PHP_Token_HALT_COMPILER') {
  167. break;
  168. } elseif ($tokenClass == 'PHP_Token_COMMENT' ||
  169. $tokenClass == 'PHP_Token_DOC_COMMENT') {
  170. $this->linesOfCode['cloc'] += $lines + 1;
  171. }
  172. if ($name == 'DOUBLE_COLON') {
  173. $lastNonWhitespaceTokenWasDoubleColon = true;
  174. } elseif ($name != 'WHITESPACE') {
  175. $lastNonWhitespaceTokenWasDoubleColon = false;
  176. }
  177. $i += $skip;
  178. }
  179. $this->linesOfCode['loc'] = substr_count($sourceCode, "\n");
  180. $this->linesOfCode['ncloc'] = $this->linesOfCode['loc'] -
  181. $this->linesOfCode['cloc'];
  182. }
  183. /**
  184. * @return integer
  185. */
  186. public function count()
  187. {
  188. return count($this->tokens);
  189. }
  190. /**
  191. * @return PHP_Token[]
  192. */
  193. public function tokens()
  194. {
  195. return $this->tokens;
  196. }
  197. /**
  198. * @return array
  199. */
  200. public function getClasses()
  201. {
  202. if ($this->classes !== null) {
  203. return $this->classes;
  204. }
  205. $this->parse();
  206. return $this->classes;
  207. }
  208. /**
  209. * @return array
  210. */
  211. public function getFunctions()
  212. {
  213. if ($this->functions !== null) {
  214. return $this->functions;
  215. }
  216. $this->parse();
  217. return $this->functions;
  218. }
  219. /**
  220. * @return array
  221. */
  222. public function getInterfaces()
  223. {
  224. if ($this->interfaces !== null) {
  225. return $this->interfaces;
  226. }
  227. $this->parse();
  228. return $this->interfaces;
  229. }
  230. /**
  231. * @return array
  232. * @since Method available since Release 1.1.0
  233. */
  234. public function getTraits()
  235. {
  236. if ($this->traits !== null) {
  237. return $this->traits;
  238. }
  239. $this->parse();
  240. return $this->traits;
  241. }
  242. /**
  243. * Gets the names of all files that have been included
  244. * using include(), include_once(), require() or require_once().
  245. *
  246. * Parameter $categorize set to TRUE causing this function to return a
  247. * multi-dimensional array with categories in the keys of the first dimension
  248. * and constants and their values in the second dimension.
  249. *
  250. * Parameter $category allow to filter following specific inclusion type
  251. *
  252. * @param bool $categorize OPTIONAL
  253. * @param string $category OPTIONAL Either 'require_once', 'require',
  254. * 'include_once', 'include'.
  255. * @return array
  256. * @since Method available since Release 1.1.0
  257. */
  258. public function getIncludes($categorize = false, $category = null)
  259. {
  260. if ($this->includes === null) {
  261. $this->includes = array(
  262. 'require_once' => array(),
  263. 'require' => array(),
  264. 'include_once' => array(),
  265. 'include' => array()
  266. );
  267. foreach ($this->tokens as $token) {
  268. switch (get_class($token)) {
  269. case 'PHP_Token_REQUIRE_ONCE':
  270. case 'PHP_Token_REQUIRE':
  271. case 'PHP_Token_INCLUDE_ONCE':
  272. case 'PHP_Token_INCLUDE':
  273. $this->includes[$token->getType()][] = $token->getName();
  274. break;
  275. }
  276. }
  277. }
  278. if (isset($this->includes[$category])) {
  279. $includes = $this->includes[$category];
  280. } elseif ($categorize === false) {
  281. $includes = array_merge(
  282. $this->includes['require_once'],
  283. $this->includes['require'],
  284. $this->includes['include_once'],
  285. $this->includes['include']
  286. );
  287. } else {
  288. $includes = $this->includes;
  289. }
  290. return $includes;
  291. }
  292. /**
  293. * Returns the name of the function or method a line belongs to.
  294. *
  295. * @return string or null if the line is not in a function or method
  296. * @since Method available since Release 1.2.0
  297. */
  298. public function getFunctionForLine($line)
  299. {
  300. $this->parse();
  301. if (isset($this->lineToFunctionMap[$line])) {
  302. return $this->lineToFunctionMap[$line];
  303. }
  304. }
  305. protected function parse()
  306. {
  307. $this->interfaces = array();
  308. $this->classes = array();
  309. $this->traits = array();
  310. $this->functions = array();
  311. $class = array();
  312. $classEndLine = array();
  313. $trait = false;
  314. $traitEndLine = false;
  315. $interface = false;
  316. $interfaceEndLine = false;
  317. foreach ($this->tokens as $token) {
  318. switch (get_class($token)) {
  319. case 'PHP_Token_HALT_COMPILER':
  320. return;
  321. case 'PHP_Token_INTERFACE':
  322. $interface = $token->getName();
  323. $interfaceEndLine = $token->getEndLine();
  324. $this->interfaces[$interface] = array(
  325. 'methods' => array(),
  326. 'parent' => $token->getParent(),
  327. 'keywords' => $token->getKeywords(),
  328. 'docblock' => $token->getDocblock(),
  329. 'startLine' => $token->getLine(),
  330. 'endLine' => $interfaceEndLine,
  331. 'package' => $token->getPackage(),
  332. 'file' => $this->filename
  333. );
  334. break;
  335. case 'PHP_Token_CLASS':
  336. case 'PHP_Token_TRAIT':
  337. $tmp = array(
  338. 'methods' => array(),
  339. 'parent' => $token->getParent(),
  340. 'interfaces'=> $token->getInterfaces(),
  341. 'keywords' => $token->getKeywords(),
  342. 'docblock' => $token->getDocblock(),
  343. 'startLine' => $token->getLine(),
  344. 'endLine' => $token->getEndLine(),
  345. 'package' => $token->getPackage(),
  346. 'file' => $this->filename
  347. );
  348. if ($token instanceof PHP_Token_CLASS) {
  349. $class[] = $token->getName();
  350. $classEndLine[] = $token->getEndLine();
  351. if ($class[count($class)-1] != 'anonymous class') {
  352. $this->classes[$class[count($class)-1]] = $tmp;
  353. }
  354. } else {
  355. $trait = $token->getName();
  356. $traitEndLine = $token->getEndLine();
  357. $this->traits[$trait] = $tmp;
  358. }
  359. break;
  360. case 'PHP_Token_FUNCTION':
  361. $name = $token->getName();
  362. $tmp = array(
  363. 'docblock' => $token->getDocblock(),
  364. 'keywords' => $token->getKeywords(),
  365. 'visibility'=> $token->getVisibility(),
  366. 'signature' => $token->getSignature(),
  367. 'startLine' => $token->getLine(),
  368. 'endLine' => $token->getEndLine(),
  369. 'ccn' => $token->getCCN(),
  370. 'file' => $this->filename
  371. );
  372. if (empty($class) &&
  373. $trait === false &&
  374. $interface === false) {
  375. $this->functions[$name] = $tmp;
  376. $this->addFunctionToMap(
  377. $name,
  378. $tmp['startLine'],
  379. $tmp['endLine']
  380. );
  381. } elseif (!empty($class) && $class[count($class)-1] != 'anonymous class') {
  382. $this->classes[$class[count($class)-1]]['methods'][$name] = $tmp;
  383. $this->addFunctionToMap(
  384. $class[count($class)-1] . '::' . $name,
  385. $tmp['startLine'],
  386. $tmp['endLine']
  387. );
  388. } elseif ($trait !== false) {
  389. $this->traits[$trait]['methods'][$name] = $tmp;
  390. $this->addFunctionToMap(
  391. $trait . '::' . $name,
  392. $tmp['startLine'],
  393. $tmp['endLine']
  394. );
  395. } else {
  396. $this->interfaces[$interface]['methods'][$name] = $tmp;
  397. }
  398. break;
  399. case 'PHP_Token_CLOSE_CURLY':
  400. if (!empty($classEndLine) &&
  401. $classEndLine[count($classEndLine)-1] == $token->getLine()) {
  402. array_pop($classEndLine);
  403. array_pop($class);
  404. } elseif ($traitEndLine !== false &&
  405. $traitEndLine == $token->getLine()) {
  406. $trait = false;
  407. $traitEndLine = false;
  408. } elseif ($interfaceEndLine !== false &&
  409. $interfaceEndLine == $token->getLine()) {
  410. $interface = false;
  411. $interfaceEndLine = false;
  412. }
  413. break;
  414. }
  415. }
  416. }
  417. /**
  418. * @return array
  419. */
  420. public function getLinesOfCode()
  421. {
  422. return $this->linesOfCode;
  423. }
  424. /**
  425. */
  426. public function rewind()
  427. {
  428. $this->position = 0;
  429. }
  430. /**
  431. * @return boolean
  432. */
  433. public function valid()
  434. {
  435. return isset($this->tokens[$this->position]);
  436. }
  437. /**
  438. * @return integer
  439. */
  440. public function key()
  441. {
  442. return $this->position;
  443. }
  444. /**
  445. * @return PHP_Token
  446. */
  447. public function current()
  448. {
  449. return $this->tokens[$this->position];
  450. }
  451. /**
  452. */
  453. public function next()
  454. {
  455. $this->position++;
  456. }
  457. /**
  458. * @param integer $offset
  459. * @return boolean
  460. */
  461. public function offsetExists($offset)
  462. {
  463. return isset($this->tokens[$offset]);
  464. }
  465. /**
  466. * @param integer $offset
  467. * @return mixed
  468. * @throws OutOfBoundsException
  469. */
  470. public function offsetGet($offset)
  471. {
  472. if (!$this->offsetExists($offset)) {
  473. throw new OutOfBoundsException(
  474. sprintf(
  475. 'No token at position "%s"',
  476. $offset
  477. )
  478. );
  479. }
  480. return $this->tokens[$offset];
  481. }
  482. /**
  483. * @param integer $offset
  484. * @param mixed $value
  485. */
  486. public function offsetSet($offset, $value)
  487. {
  488. $this->tokens[$offset] = $value;
  489. }
  490. /**
  491. * @param integer $offset
  492. * @throws OutOfBoundsException
  493. */
  494. public function offsetUnset($offset)
  495. {
  496. if (!$this->offsetExists($offset)) {
  497. throw new OutOfBoundsException(
  498. sprintf(
  499. 'No token at position "%s"',
  500. $offset
  501. )
  502. );
  503. }
  504. unset($this->tokens[$offset]);
  505. }
  506. /**
  507. * Seek to an absolute position.
  508. *
  509. * @param integer $position
  510. * @throws OutOfBoundsException
  511. */
  512. public function seek($position)
  513. {
  514. $this->position = $position;
  515. if (!$this->valid()) {
  516. throw new OutOfBoundsException(
  517. sprintf(
  518. 'No token at position "%s"',
  519. $this->position
  520. )
  521. );
  522. }
  523. }
  524. /**
  525. * @param string $name
  526. * @param integer $startLine
  527. * @param integer $endLine
  528. */
  529. private function addFunctionToMap($name, $startLine, $endLine)
  530. {
  531. for ($line = $startLine; $line <= $endLine; $line++) {
  532. $this->lineToFunctionMap[$line] = $name;
  533. }
  534. }
  535. }