Stream.php 16 KB

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