Stream.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. public function count(): int
  178. {
  179. return count($this->tokens);
  180. }
  181. /**
  182. * @return PHP_Token[]
  183. */
  184. public function tokens()
  185. {
  186. return $this->tokens;
  187. }
  188. /**
  189. * @return array
  190. */
  191. public function getClasses()
  192. {
  193. if ($this->classes !== null) {
  194. return $this->classes;
  195. }
  196. $this->parse();
  197. return $this->classes;
  198. }
  199. /**
  200. * @return array
  201. */
  202. public function getFunctions()
  203. {
  204. if ($this->functions !== null) {
  205. return $this->functions;
  206. }
  207. $this->parse();
  208. return $this->functions;
  209. }
  210. /**
  211. * @return array
  212. */
  213. public function getInterfaces()
  214. {
  215. if ($this->interfaces !== null) {
  216. return $this->interfaces;
  217. }
  218. $this->parse();
  219. return $this->interfaces;
  220. }
  221. /**
  222. * @return array
  223. */
  224. public function getTraits()
  225. {
  226. if ($this->traits !== null) {
  227. return $this->traits;
  228. }
  229. $this->parse();
  230. return $this->traits;
  231. }
  232. /**
  233. * Gets the names of all files that have been included
  234. * using include(), include_once(), require() or require_once().
  235. *
  236. * Parameter $categorize set to TRUE causing this function to return a
  237. * multi-dimensional array with categories in the keys of the first dimension
  238. * and constants and their values in the second dimension.
  239. *
  240. * Parameter $category allow to filter following specific inclusion type
  241. *
  242. * @param bool $categorize OPTIONAL
  243. * @param string $category OPTIONAL Either 'require_once', 'require',
  244. * 'include_once', 'include'.
  245. *
  246. * @return array
  247. */
  248. public function getIncludes($categorize = false, $category = null)
  249. {
  250. if ($this->includes === null) {
  251. $this->includes = [
  252. 'require_once' => [],
  253. 'require' => [],
  254. 'include_once' => [],
  255. 'include' => []
  256. ];
  257. foreach ($this->tokens as $token) {
  258. switch (PHP_Token_Util::getClass($token)) {
  259. case 'PHP_Token_REQUIRE_ONCE':
  260. case 'PHP_Token_REQUIRE':
  261. case 'PHP_Token_INCLUDE_ONCE':
  262. case 'PHP_Token_INCLUDE':
  263. $this->includes[$token->getType()][] = $token->getName();
  264. break;
  265. }
  266. }
  267. }
  268. if (isset($this->includes[$category])) {
  269. $includes = $this->includes[$category];
  270. } elseif ($categorize === false) {
  271. $includes = array_merge(
  272. $this->includes['require_once'],
  273. $this->includes['require'],
  274. $this->includes['include_once'],
  275. $this->includes['include']
  276. );
  277. } else {
  278. $includes = $this->includes;
  279. }
  280. return $includes;
  281. }
  282. /**
  283. * Returns the name of the function or method a line belongs to.
  284. *
  285. * @return string or null if the line is not in a function or method
  286. */
  287. public function getFunctionForLine($line)
  288. {
  289. $this->parse();
  290. if (isset($this->lineToFunctionMap[$line])) {
  291. return $this->lineToFunctionMap[$line];
  292. }
  293. }
  294. protected function parse()
  295. {
  296. $this->interfaces = [];
  297. $this->classes = [];
  298. $this->traits = [];
  299. $this->functions = [];
  300. $class = [];
  301. $classEndLine = [];
  302. $trait = false;
  303. $traitEndLine = false;
  304. $interface = false;
  305. $interfaceEndLine = false;
  306. foreach ($this->tokens as $token) {
  307. switch (PHP_Token_Util::getClass($token)) {
  308. case 'PHP_Token_HALT_COMPILER':
  309. return;
  310. case 'PHP_Token_INTERFACE':
  311. $interface = $token->getName();
  312. $interfaceEndLine = $token->getEndLine();
  313. $this->interfaces[$interface] = [
  314. 'methods' => [],
  315. 'parent' => $token->getParent(),
  316. 'keywords' => $token->getKeywords(),
  317. 'docblock' => $token->getDocblock(),
  318. 'startLine' => $token->getLine(),
  319. 'endLine' => $interfaceEndLine,
  320. 'package' => $token->getPackage(),
  321. 'file' => $this->filename
  322. ];
  323. break;
  324. case 'PHP_Token_CLASS':
  325. case 'PHP_Token_TRAIT':
  326. $tmp = [
  327. 'methods' => [],
  328. 'parent' => $token->getParent(),
  329. 'interfaces'=> $token->getInterfaces(),
  330. 'keywords' => $token->getKeywords(),
  331. 'docblock' => $token->getDocblock(),
  332. 'startLine' => $token->getLine(),
  333. 'endLine' => $token->getEndLine(),
  334. 'package' => $token->getPackage(),
  335. 'file' => $this->filename
  336. ];
  337. if ($token->getName() !== null) {
  338. if ($token instanceof PHP_Token_CLASS) {
  339. $class[] = $token->getName();
  340. $classEndLine[] = $token->getEndLine();
  341. $this->classes[$class[count($class) - 1]] = $tmp;
  342. } else {
  343. $trait = $token->getName();
  344. $traitEndLine = $token->getEndLine();
  345. $this->traits[$trait] = $tmp;
  346. }
  347. }
  348. break;
  349. case 'PHP_Token_FUNCTION':
  350. $name = $token->getName();
  351. $tmp = [
  352. 'docblock' => $token->getDocblock(),
  353. 'keywords' => $token->getKeywords(),
  354. 'visibility'=> $token->getVisibility(),
  355. 'signature' => $token->getSignature(),
  356. 'startLine' => $token->getLine(),
  357. 'endLine' => $token->getEndLine(),
  358. 'ccn' => $token->getCCN(),
  359. 'file' => $this->filename
  360. ];
  361. if (empty($class) &&
  362. $trait === false &&
  363. $interface === false) {
  364. $this->functions[$name] = $tmp;
  365. $this->addFunctionToMap(
  366. $name,
  367. $tmp['startLine'],
  368. $tmp['endLine']
  369. );
  370. } elseif (!empty($class)) {
  371. $this->classes[$class[count($class) - 1]]['methods'][$name] = $tmp;
  372. $this->addFunctionToMap(
  373. $class[count($class) - 1] . '::' . $name,
  374. $tmp['startLine'],
  375. $tmp['endLine']
  376. );
  377. } elseif ($trait !== false) {
  378. $this->traits[$trait]['methods'][$name] = $tmp;
  379. $this->addFunctionToMap(
  380. $trait . '::' . $name,
  381. $tmp['startLine'],
  382. $tmp['endLine']
  383. );
  384. } else {
  385. $this->interfaces[$interface]['methods'][$name] = $tmp;
  386. }
  387. break;
  388. case 'PHP_Token_CLOSE_CURLY':
  389. if (!empty($classEndLine) &&
  390. $classEndLine[count($classEndLine) - 1] == $token->getLine()) {
  391. array_pop($classEndLine);
  392. array_pop($class);
  393. } elseif ($traitEndLine !== false &&
  394. $traitEndLine == $token->getLine()) {
  395. $trait = false;
  396. $traitEndLine = false;
  397. } elseif ($interfaceEndLine !== false &&
  398. $interfaceEndLine == $token->getLine()) {
  399. $interface = false;
  400. $interfaceEndLine = false;
  401. }
  402. break;
  403. }
  404. }
  405. }
  406. /**
  407. * @return array
  408. */
  409. public function getLinesOfCode()
  410. {
  411. return $this->linesOfCode;
  412. }
  413. public function rewind(): void
  414. {
  415. $this->position = 0;
  416. }
  417. public function valid(): bool
  418. {
  419. return isset($this->tokens[$this->position]);
  420. }
  421. #[\ReturnTypeWillChange]
  422. public function key()
  423. {
  424. return $this->position;
  425. }
  426. #[\ReturnTypeWillChange]
  427. public function current()
  428. {
  429. return $this->tokens[$this->position];
  430. }
  431. public function next(): void
  432. {
  433. $this->position++;
  434. }
  435. /**
  436. * @param int $offset
  437. */
  438. public function offsetExists($offset): bool
  439. {
  440. return isset($this->tokens[$offset]);
  441. }
  442. #[\ReturnTypeWillChange]
  443. public function offsetGet($offset)
  444. {
  445. if (!$this->offsetExists($offset)) {
  446. throw new OutOfBoundsException(
  447. sprintf(
  448. 'No token at position "%s"',
  449. $offset
  450. )
  451. );
  452. }
  453. return $this->tokens[$offset];
  454. }
  455. /**
  456. * @param int $offset
  457. * @param mixed $value
  458. */
  459. public function offsetSet($offset, $value): void
  460. {
  461. $this->tokens[$offset] = $value;
  462. }
  463. /**
  464. * @param int $offset
  465. *
  466. * @throws OutOfBoundsException
  467. */
  468. public function offsetUnset($offset): void
  469. {
  470. if (!$this->offsetExists($offset)) {
  471. throw new OutOfBoundsException(
  472. sprintf(
  473. 'No token at position "%s"',
  474. $offset
  475. )
  476. );
  477. }
  478. unset($this->tokens[$offset]);
  479. }
  480. /**
  481. * Seek to an absolute position.
  482. *
  483. * @param int $position
  484. *
  485. * @throws OutOfBoundsException
  486. */
  487. public function seek($position): void
  488. {
  489. $this->position = $position;
  490. if (!$this->valid()) {
  491. throw new OutOfBoundsException(
  492. sprintf(
  493. 'No token at position "%s"',
  494. $this->position
  495. )
  496. );
  497. }
  498. }
  499. /**
  500. * @param string $name
  501. * @param int $startLine
  502. * @param int $endLine
  503. */
  504. private function addFunctionToMap($name, $startLine, $endLine)
  505. {
  506. for ($line = $startLine; $line <= $endLine; $line++) {
  507. $this->lineToFunctionMap[$line] = $name;
  508. }
  509. }
  510. }