ReflectionClosure.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. <?php
  2. /* ===========================================================================
  3. * Copyright (c) 2018-2019 Zindex Software
  4. *
  5. * Licensed under the MIT License
  6. * =========================================================================== */
  7. namespace Opis\Closure;
  8. use Closure;
  9. use ReflectionFunction;
  10. class ReflectionClosure extends ReflectionFunction
  11. {
  12. protected $code;
  13. protected $tokens;
  14. protected $hashedName;
  15. protected $useVariables;
  16. protected $isStaticClosure;
  17. protected $isScopeRequired;
  18. protected $isBindingRequired;
  19. protected $isShortClosure;
  20. protected static $files = array();
  21. protected static $classes = array();
  22. protected static $functions = array();
  23. protected static $constants = array();
  24. protected static $structures = array();
  25. /**
  26. * ReflectionClosure constructor.
  27. * @param Closure $closure
  28. * @param string|null $code
  29. * @throws \ReflectionException
  30. */
  31. public function __construct(Closure $closure, $code = null)
  32. {
  33. $this->code = $code;
  34. parent::__construct($closure);
  35. }
  36. /**
  37. * @return bool
  38. */
  39. public function isStatic()
  40. {
  41. if ($this->isStaticClosure === null) {
  42. $this->isStaticClosure = strtolower(substr($this->getCode(), 0, 6)) === 'static';
  43. }
  44. return $this->isStaticClosure;
  45. }
  46. public function isShortClosure()
  47. {
  48. if ($this->isShortClosure === null) {
  49. $code = $this->getCode();
  50. if ($this->isStatic()) {
  51. $code = substr($code, 6);
  52. }
  53. $this->isShortClosure = strtolower(substr(trim($code), 0, 2)) === 'fn';
  54. }
  55. return $this->isShortClosure;
  56. }
  57. /**
  58. * @return string
  59. */
  60. public function getCode()
  61. {
  62. if($this->code !== null){
  63. return $this->code;
  64. }
  65. $fileName = $this->getFileName();
  66. $line = $this->getStartLine() - 1;
  67. $match = ClosureStream::STREAM_PROTO . '://';
  68. if ($line === 1 && substr($fileName, 0, strlen($match)) === $match) {
  69. return $this->code = substr($fileName, strlen($match));
  70. }
  71. $className = null;
  72. $fn = false;
  73. if (null !== $className = $this->getClosureScopeClass()) {
  74. $className = '\\' . trim($className->getName(), '\\');
  75. }
  76. if($php7 = PHP_MAJOR_VERSION === 7){
  77. switch (PHP_MINOR_VERSION){
  78. case 0:
  79. $php7_types = array('string', 'int', 'bool', 'float');
  80. break;
  81. case 1:
  82. $php7_types = array('string', 'int', 'bool', 'float', 'void');
  83. break;
  84. case 2:
  85. default:
  86. $php7_types = array('string', 'int', 'bool', 'float', 'void', 'object');
  87. }
  88. $fn = PHP_MINOR_VERSION === 4;
  89. }
  90. $ns = $this->getNamespaceName();
  91. $nsf = $ns == '' ? '' : ($ns[0] == '\\' ? $ns : '\\' . $ns);
  92. $_file = var_export($fileName, true);
  93. $_dir = var_export(dirname($fileName), true);
  94. $_namespace = var_export($ns, true);
  95. $_class = var_export(trim($className, '\\'), true);
  96. $_function = $ns . ($ns == '' ? '' : '\\') . '{closure}';
  97. $_method = ($className == '' ? '' : trim($className, '\\') . '::') . $_function;
  98. $_function = var_export($_function, true);
  99. $_method = var_export($_method, true);
  100. $_trait = null;
  101. $tokens = $this->getTokens();
  102. $state = $lastState = 'start';
  103. $inside_anonymous = false;
  104. $isShortClosure = false;
  105. $anonymous_mark = 0;
  106. $open = 0;
  107. $code = '';
  108. $id_start = $id_start_ci = $id_name = $context = '';
  109. $classes = $functions = $constants = null;
  110. $use = array();
  111. $lineAdd = 0;
  112. $isUsingScope = false;
  113. $isUsingThisObject = false;
  114. for($i = 0, $l = count($tokens); $i < $l; $i++) {
  115. $token = $tokens[$i];
  116. switch ($state) {
  117. case 'start':
  118. if ($token[0] === T_FUNCTION || $token[0] === T_STATIC) {
  119. $code .= $token[1];
  120. $state = $token[0] === T_FUNCTION ? 'function' : 'static';
  121. } elseif ($fn && $token[0] === T_FN) {
  122. $isShortClosure = true;
  123. $code .= $token[1];
  124. $state = 'closure_args';
  125. }
  126. break;
  127. case 'static':
  128. if ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_FUNCTION) {
  129. $code .= $token[1];
  130. if ($token[0] === T_FUNCTION) {
  131. $state = 'function';
  132. }
  133. } elseif ($fn && $token[0] === T_FN) {
  134. $isShortClosure = true;
  135. $code .= $token[1];
  136. $state = 'closure_args';
  137. } else {
  138. $code = '';
  139. $state = 'start';
  140. }
  141. break;
  142. case 'function':
  143. switch ($token[0]){
  144. case T_STRING:
  145. $code = '';
  146. $state = 'named_function';
  147. break;
  148. case '(':
  149. $code .= '(';
  150. $state = 'closure_args';
  151. break;
  152. default:
  153. $code .= is_array($token) ? $token[1] : $token;
  154. }
  155. break;
  156. case 'named_function':
  157. if($token[0] === T_FUNCTION || $token[0] === T_STATIC){
  158. $code = $token[1];
  159. $state = $token[0] === T_FUNCTION ? 'function' : 'static';
  160. } elseif ($fn && $token[0] === T_FN) {
  161. $isShortClosure = true;
  162. $code .= $token[1];
  163. $state = 'closure_args';
  164. }
  165. break;
  166. case 'closure_args':
  167. switch ($token[0]){
  168. case T_NS_SEPARATOR:
  169. case T_STRING:
  170. $id_start = $token[1];
  171. $id_start_ci = strtolower($id_start);
  172. $id_name = '';
  173. $context = 'args';
  174. $state = 'id_name';
  175. $lastState = 'closure_args';
  176. break;
  177. case T_USE:
  178. $code .= $token[1];
  179. $state = 'use';
  180. break;
  181. case T_DOUBLE_ARROW:
  182. $code .= $token[1];
  183. if ($isShortClosure) {
  184. $state = 'closure';
  185. }
  186. break;
  187. case '=':
  188. $code .= $token;
  189. $lastState = 'closure_args';
  190. $state = 'ignore_next';
  191. break;
  192. case ':':
  193. $code .= ':';
  194. $state = 'return';
  195. break;
  196. case '{':
  197. $code .= '{';
  198. $state = 'closure';
  199. $open++;
  200. break;
  201. default:
  202. $code .= is_array($token) ? $token[1] : $token;
  203. }
  204. break;
  205. case 'use':
  206. switch ($token[0]){
  207. case T_VARIABLE:
  208. $use[] = substr($token[1], 1);
  209. $code .= $token[1];
  210. break;
  211. case '{':
  212. $code .= '{';
  213. $state = 'closure';
  214. $open++;
  215. break;
  216. case ':':
  217. $code .= ':';
  218. $state = 'return';
  219. break;
  220. default:
  221. $code .= is_array($token) ? $token[1] : $token;
  222. break;
  223. }
  224. break;
  225. case 'return':
  226. switch ($token[0]){
  227. case T_WHITESPACE:
  228. case T_COMMENT:
  229. case T_DOC_COMMENT:
  230. $code .= $token[1];
  231. break;
  232. case T_NS_SEPARATOR:
  233. case T_STRING:
  234. $id_start = $token[1];
  235. $id_start_ci = strtolower($id_start);
  236. $id_name = '';
  237. $context = 'return_type';
  238. $state = 'id_name';
  239. $lastState = 'return';
  240. break 2;
  241. case T_DOUBLE_ARROW:
  242. $code .= $token[1];
  243. if ($isShortClosure) {
  244. $state = 'closure';
  245. }
  246. break;
  247. case '{':
  248. $code .= '{';
  249. $state = 'closure';
  250. $open++;
  251. break;
  252. default:
  253. $code .= is_array($token) ? $token[1] : $token;
  254. break;
  255. }
  256. break;
  257. case 'closure':
  258. switch ($token[0]){
  259. case T_CURLY_OPEN:
  260. case T_DOLLAR_OPEN_CURLY_BRACES:
  261. case T_STRING_VARNAME:
  262. case '{':
  263. $code .= '{';
  264. $open++;
  265. break;
  266. case '}':
  267. $code .= '}';
  268. if(--$open === 0 && !$isShortClosure){
  269. break 3;
  270. } elseif ($inside_anonymous) {
  271. $inside_anonymous = !($open === $anonymous_mark);
  272. }
  273. break;
  274. case '(':
  275. case '[':
  276. $code .= $token[0];
  277. if ($isShortClosure) {
  278. $open++;
  279. }
  280. break;
  281. case ')':
  282. case ']':
  283. if ($isShortClosure) {
  284. if ($open === 0) {
  285. break 3;
  286. }
  287. --$open;
  288. }
  289. $code .= $token[0];
  290. break;
  291. case ',':
  292. case ';':
  293. if ($isShortClosure && $open === 0) {
  294. break 3;
  295. }
  296. $code .= $token[0];
  297. break;
  298. case T_LINE:
  299. $code .= $token[2] - $line + $lineAdd;
  300. break;
  301. case T_FILE:
  302. $code .= $_file;
  303. break;
  304. case T_DIR:
  305. $code .= $_dir;
  306. break;
  307. case T_NS_C:
  308. $code .= $_namespace;
  309. break;
  310. case T_CLASS_C:
  311. $code .= $_class;
  312. break;
  313. case T_FUNC_C:
  314. $code .= $_function;
  315. break;
  316. case T_METHOD_C:
  317. $code .= $_method;
  318. break;
  319. case T_COMMENT:
  320. if (substr($token[1], 0, 8) === '#trackme') {
  321. $timestamp = time();
  322. $code .= '/**' . PHP_EOL;
  323. $code .= '* Date : ' . date(DATE_W3C, $timestamp) . PHP_EOL;
  324. $code .= '* Timestamp : ' . $timestamp . PHP_EOL;
  325. $code .= '* Line : ' . ($line + 1) . PHP_EOL;
  326. $code .= '* File : ' . $_file . PHP_EOL . '*/' . PHP_EOL;
  327. $lineAdd += 5;
  328. } else {
  329. $code .= $token[1];
  330. }
  331. break;
  332. case T_VARIABLE:
  333. if($token[1] == '$this' && !$inside_anonymous){
  334. $isUsingThisObject = true;
  335. }
  336. $code .= $token[1];
  337. break;
  338. case T_STATIC:
  339. $isUsingScope = true;
  340. $code .= $token[1];
  341. break;
  342. case T_NS_SEPARATOR:
  343. case T_STRING:
  344. $id_start = $token[1];
  345. $id_start_ci = strtolower($id_start);
  346. $id_name = '';
  347. $context = 'root';
  348. $state = 'id_name';
  349. $lastState = 'closure';
  350. break 2;
  351. case T_NEW:
  352. $code .= $token[1];
  353. $context = 'new';
  354. $state = 'id_start';
  355. $lastState = 'closure';
  356. break 2;
  357. case T_USE:
  358. $code .= $token[1];
  359. $context = 'use';
  360. $state = 'id_start';
  361. $lastState = 'closure';
  362. break;
  363. case T_INSTANCEOF:
  364. $code .= $token[1];
  365. $context = 'instanceof';
  366. $state = 'id_start';
  367. $lastState = 'closure';
  368. break;
  369. case T_OBJECT_OPERATOR:
  370. case T_DOUBLE_COLON:
  371. $code .= $token[1];
  372. $lastState = 'closure';
  373. $state = 'ignore_next';
  374. break;
  375. case T_FUNCTION:
  376. $code .= $token[1];
  377. $state = 'closure_args';
  378. break;
  379. case T_TRAIT_C:
  380. if ($_trait === null) {
  381. $startLine = $this->getStartLine();
  382. $endLine = $this->getEndLine();
  383. $structures = $this->getStructures();
  384. $_trait = '';
  385. foreach ($structures as &$struct) {
  386. if ($struct['type'] === 'trait' &&
  387. $struct['start'] <= $startLine &&
  388. $struct['end'] >= $endLine
  389. ) {
  390. $_trait = ($ns == '' ? '' : $ns . '\\') . $struct['name'];
  391. break;
  392. }
  393. }
  394. $_trait = var_export($_trait, true);
  395. }
  396. $code .= $_trait;
  397. break;
  398. default:
  399. $code .= is_array($token) ? $token[1] : $token;
  400. }
  401. break;
  402. case 'ignore_next':
  403. switch ($token[0]){
  404. case T_WHITESPACE:
  405. case T_COMMENT:
  406. case T_DOC_COMMENT:
  407. $code .= $token[1];
  408. break;
  409. case T_CLASS:
  410. case T_NEW:
  411. case T_STATIC:
  412. case T_VARIABLE:
  413. case T_STRING:
  414. case T_CLASS_C:
  415. case T_FILE:
  416. case T_DIR:
  417. case T_METHOD_C:
  418. case T_FUNC_C:
  419. case T_FUNCTION:
  420. case T_INSTANCEOF:
  421. case T_LINE:
  422. case T_NS_C:
  423. case T_TRAIT_C:
  424. case T_USE:
  425. $code .= $token[1];
  426. $state = $lastState;
  427. break;
  428. default:
  429. $state = $lastState;
  430. $i--;
  431. }
  432. break;
  433. case 'id_start':
  434. switch ($token[0]){
  435. case T_WHITESPACE:
  436. case T_COMMENT:
  437. case T_DOC_COMMENT:
  438. $code .= $token[1];
  439. break;
  440. case T_NS_SEPARATOR:
  441. case T_STRING:
  442. case T_STATIC:
  443. $id_start = $token[1];
  444. $id_start_ci = strtolower($id_start);
  445. $id_name = '';
  446. $state = 'id_name';
  447. break 2;
  448. case T_VARIABLE:
  449. $code .= $token[1];
  450. $state = $lastState;
  451. break;
  452. case T_CLASS:
  453. $code .= $token[1];
  454. $state = 'anonymous';
  455. break;
  456. default:
  457. $i--;//reprocess last
  458. $state = 'id_name';
  459. }
  460. break;
  461. case 'id_name':
  462. switch ($token[0]){
  463. case T_NS_SEPARATOR:
  464. case T_STRING:
  465. $id_name .= $token[1];
  466. break;
  467. case T_WHITESPACE:
  468. case T_COMMENT:
  469. case T_DOC_COMMENT:
  470. $id_name .= $token[1];
  471. break;
  472. case '(':
  473. if ($isShortClosure) {
  474. $open++;
  475. }
  476. if($context === 'new' || false !== strpos($id_name, '\\')){
  477. if($id_start !== '\\'){
  478. if ($classes === null) {
  479. $classes = $this->getClasses();
  480. }
  481. if (isset($classes[$id_start_ci])) {
  482. $id_start = $classes[$id_start_ci];
  483. }
  484. if($id_start[0] !== '\\'){
  485. $id_start = $nsf . '\\' . $id_start;
  486. }
  487. }
  488. } else {
  489. if($id_start !== '\\'){
  490. if($functions === null){
  491. $functions = $this->getFunctions();
  492. }
  493. if(isset($functions[$id_start_ci])){
  494. $id_start = $functions[$id_start_ci];
  495. }
  496. }
  497. }
  498. $code .= $id_start . $id_name . '(';
  499. $state = $lastState;
  500. break;
  501. case T_VARIABLE:
  502. case T_DOUBLE_COLON:
  503. if($id_start !== '\\') {
  504. if($id_start_ci === 'self' || $id_start_ci === 'static' || $id_start_ci === 'parent'){
  505. $isUsingScope = true;
  506. } elseif (!($php7 && in_array($id_start_ci, $php7_types))){
  507. if ($classes === null) {
  508. $classes = $this->getClasses();
  509. }
  510. if (isset($classes[$id_start_ci])) {
  511. $id_start = $classes[$id_start_ci];
  512. }
  513. if($id_start[0] !== '\\'){
  514. $id_start = $nsf . '\\' . $id_start;
  515. }
  516. }
  517. }
  518. $code .= $id_start . $id_name . $token[1];
  519. $state = $token[0] === T_DOUBLE_COLON ? 'ignore_next' : $lastState;
  520. break;
  521. default:
  522. if($id_start !== '\\'){
  523. if($context === 'use' ||
  524. $context === 'instanceof' ||
  525. $context === 'args' ||
  526. $context === 'return_type' ||
  527. $context === 'extends'
  528. ){
  529. if($id_start_ci === 'self' || $id_start_ci === 'static' || $id_start_ci === 'parent'){
  530. $isUsingScope = true;
  531. } elseif (!($php7 && in_array($id_start_ci, $php7_types))){
  532. if($classes === null){
  533. $classes = $this->getClasses();
  534. }
  535. if(isset($classes[$id_start_ci])){
  536. $id_start = $classes[$id_start_ci];
  537. }
  538. if($id_start[0] !== '\\'){
  539. $id_start = $nsf . '\\' . $id_start;
  540. }
  541. }
  542. } else {
  543. if($constants === null){
  544. $constants = $this->getConstants();
  545. }
  546. if(isset($constants[$id_start])){
  547. $id_start = $constants[$id_start];
  548. }
  549. }
  550. }
  551. $code .= $id_start . $id_name;
  552. $state = $lastState;
  553. $i--;//reprocess last token
  554. }
  555. break;
  556. case 'anonymous':
  557. switch ($token[0]) {
  558. case T_NS_SEPARATOR:
  559. case T_STRING:
  560. $id_start = $token[1];
  561. $id_start_ci = strtolower($id_start);
  562. $id_name = '';
  563. $state = 'id_name';
  564. $context = 'extends';
  565. $lastState = 'anonymous';
  566. break;
  567. case '{':
  568. $state = 'closure';
  569. if (!$inside_anonymous) {
  570. $inside_anonymous = true;
  571. $anonymous_mark = $open;
  572. }
  573. $i--;
  574. break;
  575. default:
  576. $code .= is_array($token) ? $token[1] : $token;
  577. }
  578. break;
  579. }
  580. }
  581. if ($isShortClosure) {
  582. $code .= ';';
  583. $this->useVariables = $this->getStaticVariables();
  584. } else {
  585. $this->useVariables = empty($use) ? $use : array_intersect_key($this->getStaticVariables(), array_flip($use));
  586. }
  587. $this->isShortClosure = $isShortClosure;
  588. $this->isBindingRequired = $isUsingThisObject;
  589. $this->isScopeRequired = $isUsingScope;
  590. $this->code = $code;
  591. return $this->code;
  592. }
  593. /**
  594. * @return array
  595. */
  596. public function getUseVariables()
  597. {
  598. if($this->useVariables !== null){
  599. return $this->useVariables;
  600. }
  601. $tokens = $this->getTokens();
  602. $use = array();
  603. $state = 'start';
  604. foreach ($tokens as &$token) {
  605. $is_array = is_array($token);
  606. switch ($state) {
  607. case 'start':
  608. if ($is_array && $token[0] === T_USE) {
  609. $state = 'use';
  610. }
  611. break;
  612. case 'use':
  613. if ($is_array) {
  614. if ($token[0] === T_VARIABLE) {
  615. $use[] = substr($token[1], 1);
  616. }
  617. } elseif ($token == ')') {
  618. break 2;
  619. }
  620. break;
  621. }
  622. }
  623. $this->useVariables = empty($use) ? $use : array_intersect_key($this->getStaticVariables(), array_flip($use));
  624. return $this->useVariables;
  625. }
  626. /**
  627. * return bool
  628. */
  629. public function isBindingRequired()
  630. {
  631. if($this->isBindingRequired === null){
  632. $this->getCode();
  633. }
  634. return $this->isBindingRequired;
  635. }
  636. /**
  637. * return bool
  638. */
  639. public function isScopeRequired()
  640. {
  641. if($this->isScopeRequired === null){
  642. $this->getCode();
  643. }
  644. return $this->isScopeRequired;
  645. }
  646. /**
  647. * @return string
  648. */
  649. protected function getHashedFileName()
  650. {
  651. if ($this->hashedName === null) {
  652. $this->hashedName = sha1($this->getFileName());
  653. }
  654. return $this->hashedName;
  655. }
  656. /**
  657. * @return array
  658. */
  659. protected function getFileTokens()
  660. {
  661. $key = $this->getHashedFileName();
  662. if (!isset(static::$files[$key])) {
  663. static::$files[$key] = token_get_all(file_get_contents($this->getFileName()));
  664. }
  665. return static::$files[$key];
  666. }
  667. /**
  668. * @return array
  669. */
  670. protected function getTokens()
  671. {
  672. if ($this->tokens === null) {
  673. $tokens = $this->getFileTokens();
  674. $startLine = $this->getStartLine();
  675. $endLine = $this->getEndLine();
  676. $results = array();
  677. $start = false;
  678. foreach ($tokens as &$token) {
  679. if (!is_array($token)) {
  680. if ($start) {
  681. $results[] = $token;
  682. }
  683. continue;
  684. }
  685. $line = $token[2];
  686. if ($line <= $endLine) {
  687. if ($line >= $startLine) {
  688. $start = true;
  689. $results[] = $token;
  690. }
  691. continue;
  692. }
  693. break;
  694. }
  695. $this->tokens = $results;
  696. }
  697. return $this->tokens;
  698. }
  699. /**
  700. * @return array
  701. */
  702. protected function getClasses()
  703. {
  704. $key = $this->getHashedFileName();
  705. if (!isset(static::$classes[$key])) {
  706. $this->fetchItems();
  707. }
  708. return static::$classes[$key];
  709. }
  710. /**
  711. * @return array
  712. */
  713. protected function getFunctions()
  714. {
  715. $key = $this->getHashedFileName();
  716. if (!isset(static::$functions[$key])) {
  717. $this->fetchItems();
  718. }
  719. return static::$functions[$key];
  720. }
  721. /**
  722. * @return array
  723. */
  724. protected function getConstants()
  725. {
  726. $key = $this->getHashedFileName();
  727. if (!isset(static::$constants[$key])) {
  728. $this->fetchItems();
  729. }
  730. return static::$constants[$key];
  731. }
  732. /**
  733. * @return array
  734. */
  735. protected function getStructures()
  736. {
  737. $key = $this->getHashedFileName();
  738. if (!isset(static::$structures[$key])) {
  739. $this->fetchItems();
  740. }
  741. return static::$structures[$key];
  742. }
  743. protected function fetchItems()
  744. {
  745. $key = $this->getHashedFileName();
  746. $classes = array();
  747. $functions = array();
  748. $constants = array();
  749. $structures = array();
  750. $tokens = $this->getFileTokens();
  751. $open = 0;
  752. $state = 'start';
  753. $lastState = '';
  754. $prefix = '';
  755. $name = '';
  756. $alias = '';
  757. $isFunc = $isConst = false;
  758. $startLine = $endLine = 0;
  759. $structType = $structName = '';
  760. $structIgnore = false;
  761. foreach ($tokens as $token) {
  762. switch ($state) {
  763. case 'start':
  764. switch ($token[0]) {
  765. case T_CLASS:
  766. case T_INTERFACE:
  767. case T_TRAIT:
  768. $state = 'before_structure';
  769. $startLine = $token[2];
  770. $structType = $token[0] == T_CLASS
  771. ? 'class'
  772. : ($token[0] == T_INTERFACE ? 'interface' : 'trait');
  773. break;
  774. case T_USE:
  775. $state = 'use';
  776. $prefix = $name = $alias = '';
  777. $isFunc = $isConst = false;
  778. break;
  779. case T_FUNCTION:
  780. $state = 'structure';
  781. $structIgnore = true;
  782. break;
  783. case T_NEW:
  784. $state = 'new';
  785. break;
  786. case T_OBJECT_OPERATOR:
  787. case T_DOUBLE_COLON:
  788. $state = 'invoke';
  789. break;
  790. }
  791. break;
  792. case 'use':
  793. switch ($token[0]) {
  794. case T_FUNCTION:
  795. $isFunc = true;
  796. break;
  797. case T_CONST:
  798. $isConst = true;
  799. break;
  800. case T_NS_SEPARATOR:
  801. $name .= $token[1];
  802. break;
  803. case T_STRING:
  804. $name .= $token[1];
  805. $alias = $token[1];
  806. break;
  807. case T_AS:
  808. $lastState = 'use';
  809. $state = 'alias';
  810. break;
  811. case '{':
  812. $prefix = $name;
  813. $name = $alias = '';
  814. $state = 'use-group';
  815. break;
  816. case ',':
  817. case ';':
  818. if ($name === '' || $name[0] !== '\\') {
  819. $name = '\\' . $name;
  820. }
  821. if ($alias !== '') {
  822. if ($isFunc) {
  823. $functions[strtolower($alias)] = $name;
  824. } elseif ($isConst) {
  825. $constants[$alias] = $name;
  826. } else {
  827. $classes[strtolower($alias)] = $name;
  828. }
  829. }
  830. $name = $alias = '';
  831. $state = $token === ';' ? 'start' : 'use';
  832. break;
  833. }
  834. break;
  835. case 'use-group':
  836. switch ($token[0]) {
  837. case T_NS_SEPARATOR:
  838. $name .= $token[1];
  839. break;
  840. case T_STRING:
  841. $name .= $token[1];
  842. $alias = $token[1];
  843. break;
  844. case T_AS:
  845. $lastState = 'use-group';
  846. $state = 'alias';
  847. break;
  848. case ',':
  849. case '}':
  850. if ($prefix === '' || $prefix[0] !== '\\') {
  851. $prefix = '\\' . $prefix;
  852. }
  853. if ($alias !== '') {
  854. if ($isFunc) {
  855. $functions[strtolower($alias)] = $prefix . $name;
  856. } elseif ($isConst) {
  857. $constants[$alias] = $prefix . $name;
  858. } else {
  859. $classes[strtolower($alias)] = $prefix . $name;
  860. }
  861. }
  862. $name = $alias = '';
  863. $state = $token === '}' ? 'use' : 'use-group';
  864. break;
  865. }
  866. break;
  867. case 'alias':
  868. if ($token[0] === T_STRING) {
  869. $alias = $token[1];
  870. $state = $lastState;
  871. }
  872. break;
  873. case 'new':
  874. switch ($token[0]) {
  875. case T_WHITESPACE:
  876. case T_COMMENT:
  877. case T_DOC_COMMENT:
  878. break 2;
  879. case T_CLASS:
  880. $state = 'structure';
  881. $structIgnore = true;
  882. break;
  883. default:
  884. $state = 'start';
  885. }
  886. break;
  887. case 'invoke':
  888. switch ($token[0]) {
  889. case T_WHITESPACE:
  890. case T_COMMENT:
  891. case T_DOC_COMMENT:
  892. break 2;
  893. default:
  894. $state = 'start';
  895. }
  896. break;
  897. case 'before_structure':
  898. if ($token[0] == T_STRING) {
  899. $structName = $token[1];
  900. $state = 'structure';
  901. }
  902. break;
  903. case 'structure':
  904. switch ($token[0]) {
  905. case '{':
  906. case T_CURLY_OPEN:
  907. case T_DOLLAR_OPEN_CURLY_BRACES:
  908. case T_STRING_VARNAME:
  909. $open++;
  910. break;
  911. case '}':
  912. if (--$open == 0) {
  913. if(!$structIgnore){
  914. $structures[] = array(
  915. 'type' => $structType,
  916. 'name' => $structName,
  917. 'start' => $startLine,
  918. 'end' => $endLine,
  919. );
  920. }
  921. $structIgnore = false;
  922. $state = 'start';
  923. }
  924. break;
  925. default:
  926. if (is_array($token)) {
  927. $endLine = $token[2];
  928. }
  929. }
  930. break;
  931. }
  932. }
  933. static::$classes[$key] = $classes;
  934. static::$functions[$key] = $functions;
  935. static::$constants[$key] = $constants;
  936. static::$structures[$key] = $structures;
  937. }
  938. }