Drupal investigation

PHP.php 75KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. <?php
  2. /**
  3. * Tokenizes PHP code.
  4. *
  5. * PHP version 5
  6. *
  7. * @category PHP
  8. * @package PHP_CodeSniffer
  9. * @author Greg Sherwood <gsherwood@squiz.net>
  10. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  11. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  12. * @link http://pear.php.net/package/PHP_CodeSniffer
  13. */
  14. /**
  15. * Tokenizes PHP code.
  16. *
  17. * @category PHP
  18. * @package PHP_CodeSniffer
  19. * @author Greg Sherwood <gsherwood@squiz.net>
  20. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  21. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  22. * @version Release: @package_version@
  23. * @link http://pear.php.net/package/PHP_CodeSniffer
  24. */
  25. class PHP_CodeSniffer_Tokenizers_PHP
  26. {
  27. /**
  28. * If TRUE, files that appear to be minified will not be processed.
  29. *
  30. * @var boolean
  31. */
  32. public $skipMinified = false;
  33. /**
  34. * A list of tokens that are allowed to open a scope.
  35. *
  36. * This array also contains information about what kind of token the scope
  37. * opener uses to open and close the scope, if the token strictly requires
  38. * an opener, if the token can share a scope closer, and who it can be shared
  39. * with. An example of a token that shares a scope closer is a CASE scope.
  40. *
  41. * @var array
  42. */
  43. public $scopeOpeners = array(
  44. T_IF => array(
  45. 'start' => array(
  46. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  47. T_COLON => T_COLON,
  48. ),
  49. 'end' => array(
  50. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  51. T_ENDIF => T_ENDIF,
  52. T_ELSE => T_ELSE,
  53. T_ELSEIF => T_ELSEIF,
  54. ),
  55. 'strict' => false,
  56. 'shared' => false,
  57. 'with' => array(
  58. T_ELSE => T_ELSE,
  59. T_ELSEIF => T_ELSEIF,
  60. ),
  61. ),
  62. T_TRY => array(
  63. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  64. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  65. 'strict' => true,
  66. 'shared' => false,
  67. 'with' => array(),
  68. ),
  69. T_CATCH => array(
  70. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  71. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  72. 'strict' => true,
  73. 'shared' => false,
  74. 'with' => array(),
  75. ),
  76. T_FINALLY => array(
  77. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  78. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  79. 'strict' => true,
  80. 'shared' => false,
  81. 'with' => array(),
  82. ),
  83. T_ELSE => array(
  84. 'start' => array(
  85. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  86. T_COLON => T_COLON,
  87. ),
  88. 'end' => array(
  89. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  90. T_ENDIF => T_ENDIF,
  91. ),
  92. 'strict' => false,
  93. 'shared' => false,
  94. 'with' => array(
  95. T_IF => T_IF,
  96. T_ELSEIF => T_ELSEIF,
  97. ),
  98. ),
  99. T_ELSEIF => array(
  100. 'start' => array(
  101. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  102. T_COLON => T_COLON,
  103. ),
  104. 'end' => array(
  105. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  106. T_ENDIF => T_ENDIF,
  107. T_ELSE => T_ELSE,
  108. T_ELSEIF => T_ELSEIF,
  109. ),
  110. 'strict' => false,
  111. 'shared' => false,
  112. 'with' => array(
  113. T_IF => T_IF,
  114. T_ELSE => T_ELSE,
  115. ),
  116. ),
  117. T_FOR => array(
  118. 'start' => array(
  119. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  120. T_COLON => T_COLON,
  121. ),
  122. 'end' => array(
  123. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  124. T_ENDFOR => T_ENDFOR,
  125. ),
  126. 'strict' => false,
  127. 'shared' => false,
  128. 'with' => array(),
  129. ),
  130. T_FOREACH => array(
  131. 'start' => array(
  132. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  133. T_COLON => T_COLON,
  134. ),
  135. 'end' => array(
  136. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  137. T_ENDFOREACH => T_ENDFOREACH,
  138. ),
  139. 'strict' => false,
  140. 'shared' => false,
  141. 'with' => array(),
  142. ),
  143. T_INTERFACE => array(
  144. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  145. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  146. 'strict' => true,
  147. 'shared' => false,
  148. 'with' => array(),
  149. ),
  150. T_FUNCTION => array(
  151. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  152. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  153. 'strict' => true,
  154. 'shared' => false,
  155. 'with' => array(),
  156. ),
  157. T_CLASS => array(
  158. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  159. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  160. 'strict' => true,
  161. 'shared' => false,
  162. 'with' => array(),
  163. ),
  164. T_TRAIT => array(
  165. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  166. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  167. 'strict' => true,
  168. 'shared' => false,
  169. 'with' => array(),
  170. ),
  171. T_USE => array(
  172. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  173. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  174. 'strict' => false,
  175. 'shared' => false,
  176. 'with' => array(),
  177. ),
  178. T_DECLARE => array(
  179. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  180. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  181. 'strict' => false,
  182. 'shared' => false,
  183. 'with' => array(),
  184. ),
  185. T_NAMESPACE => array(
  186. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  187. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  188. 'strict' => false,
  189. 'shared' => false,
  190. 'with' => array(),
  191. ),
  192. T_WHILE => array(
  193. 'start' => array(
  194. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  195. T_COLON => T_COLON,
  196. ),
  197. 'end' => array(
  198. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  199. T_ENDWHILE => T_ENDWHILE,
  200. ),
  201. 'strict' => false,
  202. 'shared' => false,
  203. 'with' => array(),
  204. ),
  205. T_DO => array(
  206. 'start' => array(T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET),
  207. 'end' => array(T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET),
  208. 'strict' => true,
  209. 'shared' => false,
  210. 'with' => array(),
  211. ),
  212. T_SWITCH => array(
  213. 'start' => array(
  214. T_OPEN_CURLY_BRACKET => T_OPEN_CURLY_BRACKET,
  215. T_COLON => T_COLON,
  216. ),
  217. 'end' => array(
  218. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  219. T_ENDSWITCH => T_ENDSWITCH,
  220. ),
  221. 'strict' => true,
  222. 'shared' => false,
  223. 'with' => array(),
  224. ),
  225. T_CASE => array(
  226. 'start' => array(
  227. T_COLON => T_COLON,
  228. T_SEMICOLON => T_SEMICOLON,
  229. ),
  230. 'end' => array(
  231. T_BREAK => T_BREAK,
  232. T_RETURN => T_RETURN,
  233. T_CONTINUE => T_CONTINUE,
  234. T_THROW => T_THROW,
  235. T_EXIT => T_EXIT,
  236. ),
  237. 'strict' => true,
  238. 'shared' => true,
  239. 'with' => array(
  240. T_DEFAULT => T_DEFAULT,
  241. T_CASE => T_CASE,
  242. T_SWITCH => T_SWITCH,
  243. ),
  244. ),
  245. T_DEFAULT => array(
  246. 'start' => array(
  247. T_COLON => T_COLON,
  248. T_SEMICOLON => T_SEMICOLON,
  249. ),
  250. 'end' => array(
  251. T_BREAK => T_BREAK,
  252. T_RETURN => T_RETURN,
  253. T_CONTINUE => T_CONTINUE,
  254. T_THROW => T_THROW,
  255. T_EXIT => T_EXIT,
  256. ),
  257. 'strict' => true,
  258. 'shared' => true,
  259. 'with' => array(
  260. T_CASE => T_CASE,
  261. T_SWITCH => T_SWITCH,
  262. ),
  263. ),
  264. T_START_HEREDOC => array(
  265. 'start' => array(T_START_HEREDOC => T_START_HEREDOC),
  266. 'end' => array(T_END_HEREDOC => T_END_HEREDOC),
  267. 'strict' => true,
  268. 'shared' => false,
  269. 'with' => array(),
  270. ),
  271. );
  272. /**
  273. * A list of tokens that end the scope.
  274. *
  275. * This array is just a unique collection of the end tokens
  276. * from the _scopeOpeners array. The data is duplicated here to
  277. * save time during parsing of the file.
  278. *
  279. * @var array
  280. */
  281. public $endScopeTokens = array(
  282. T_CLOSE_CURLY_BRACKET => T_CLOSE_CURLY_BRACKET,
  283. T_ENDIF => T_ENDIF,
  284. T_ENDFOR => T_ENDFOR,
  285. T_ENDFOREACH => T_ENDFOREACH,
  286. T_ENDWHILE => T_ENDWHILE,
  287. T_ENDSWITCH => T_ENDSWITCH,
  288. T_BREAK => T_BREAK,
  289. T_END_HEREDOC => T_END_HEREDOC,
  290. );
  291. /**
  292. * A cache of different token types, resolved into arrays.
  293. *
  294. * @var array()
  295. * @see standardiseToken()
  296. */
  297. private static $_resolveTokenCache = array();
  298. /**
  299. * Creates an array of tokens when given some PHP code.
  300. *
  301. * Starts by using token_get_all() but does a lot of extra processing
  302. * to insert information about the context of the token.
  303. *
  304. * @param string $string The string to tokenize.
  305. * @param string $eolChar The EOL character to use for splitting strings.
  306. *
  307. * @return array
  308. */
  309. public function tokenizeString($string, $eolChar='\n')
  310. {
  311. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  312. echo "\t*** START PHP TOKENIZING ***".PHP_EOL;
  313. $isWin = false;
  314. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  315. $isWin = true;
  316. }
  317. }
  318. $tokens = @token_get_all($string);
  319. $finalTokens = array();
  320. $newStackPtr = 0;
  321. $numTokens = count($tokens);
  322. $lastNotEmptyToken = 0;
  323. $insideInlineIf = array();
  324. $insideUseGroup = false;
  325. $commentTokenizer = new PHP_CodeSniffer_Tokenizers_Comment();
  326. for ($stackPtr = 0; $stackPtr < $numTokens; $stackPtr++) {
  327. $token = (array) $tokens[$stackPtr];
  328. $tokenIsArray = isset($token[1]);
  329. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  330. if ($tokenIsArray === true) {
  331. $type = token_name($token[0]);
  332. $content = PHP_CodeSniffer::prepareForOutput($token[1]);
  333. } else {
  334. $newToken = self::resolveSimpleToken($token[0]);
  335. $type = $newToken['type'];
  336. $content = PHP_CodeSniffer::prepareForOutput($token[0]);
  337. }
  338. echo "\tProcess token ";
  339. if ($tokenIsArray === true) {
  340. echo "[$stackPtr]";
  341. } else {
  342. echo " $stackPtr ";
  343. }
  344. echo ": $type => $content";
  345. }//end if
  346. if ($newStackPtr > 0 && $finalTokens[($newStackPtr - 1)]['code'] !== T_WHITESPACE) {
  347. $lastNotEmptyToken = ($newStackPtr - 1);
  348. }
  349. /*
  350. If we are using \r\n newline characters, the \r and \n are sometimes
  351. split over two tokens. This normally occurs after comments. We need
  352. to merge these two characters together so that our line endings are
  353. consistent for all lines.
  354. */
  355. if ($tokenIsArray === true && substr($token[1], -1) === "\r") {
  356. if (isset($tokens[($stackPtr + 1)]) === true
  357. && is_array($tokens[($stackPtr + 1)]) === true
  358. && $tokens[($stackPtr + 1)][1][0] === "\n"
  359. ) {
  360. $token[1] .= "\n";
  361. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  362. if ($isWin === true) {
  363. echo '\n';
  364. } else {
  365. echo "\033[30;1m\\n\033[0m";
  366. }
  367. }
  368. if ($tokens[($stackPtr + 1)][1] === "\n") {
  369. // This token's content has been merged into the previous,
  370. // so we can skip it.
  371. $tokens[($stackPtr + 1)] = '';
  372. } else {
  373. $tokens[($stackPtr + 1)][1] = substr($tokens[($stackPtr + 1)][1], 1);
  374. }
  375. }
  376. }//end if
  377. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  378. echo PHP_EOL;
  379. }
  380. /*
  381. Parse doc blocks into something that can be easily iterated over.
  382. */
  383. if ($tokenIsArray === true && $token[0] === T_DOC_COMMENT) {
  384. $commentTokens = $commentTokenizer->tokenizeString($token[1], $eolChar, $newStackPtr);
  385. foreach ($commentTokens as $commentToken) {
  386. $finalTokens[$newStackPtr] = $commentToken;
  387. $newStackPtr++;
  388. }
  389. continue;
  390. }
  391. /*
  392. If this is a double quoted string, PHP will tokenize the whole
  393. thing which causes problems with the scope map when braces are
  394. within the string. So we need to merge the tokens together to
  395. provide a single string.
  396. */
  397. if ($tokenIsArray === false && ($token[0] === '"' || $token[0] === 'b"')) {
  398. // Binary casts need a special token.
  399. if ($token[0] === 'b"') {
  400. $finalTokens[$newStackPtr] = array(
  401. 'code' => T_BINARY_CAST,
  402. 'type' => 'T_BINARY_CAST',
  403. 'content' => 'b',
  404. );
  405. $newStackPtr++;
  406. }
  407. $tokenContent = '"';
  408. $nestedVars = array();
  409. for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
  410. $subToken = (array) $tokens[$i];
  411. $subTokenIsArray = isset($subToken[1]);
  412. if ($subTokenIsArray === true) {
  413. $tokenContent .= $subToken[1];
  414. if ($subToken[1] === '{'
  415. && $subToken[0] !== T_ENCAPSED_AND_WHITESPACE
  416. ) {
  417. $nestedVars[] = $i;
  418. }
  419. } else {
  420. $tokenContent .= $subToken[0];
  421. if ($subToken[0] === '}') {
  422. array_pop($nestedVars);
  423. }
  424. }
  425. if ($subTokenIsArray === false
  426. && $subToken[0] === '"'
  427. && empty($nestedVars) === true
  428. ) {
  429. // We found the other end of the double quoted string.
  430. break;
  431. }
  432. }//end for
  433. $stackPtr = $i;
  434. // Convert each line within the double quoted string to a
  435. // new token, so it conforms with other multiple line tokens.
  436. $tokenLines = explode($eolChar, $tokenContent);
  437. $numLines = count($tokenLines);
  438. $newToken = array();
  439. for ($j = 0; $j < $numLines; $j++) {
  440. $newToken['content'] = $tokenLines[$j];
  441. if ($j === ($numLines - 1)) {
  442. if ($tokenLines[$j] === '') {
  443. break;
  444. }
  445. } else {
  446. $newToken['content'] .= $eolChar;
  447. }
  448. $newToken['code'] = T_DOUBLE_QUOTED_STRING;
  449. $newToken['type'] = 'T_DOUBLE_QUOTED_STRING';
  450. $finalTokens[$newStackPtr] = $newToken;
  451. $newStackPtr++;
  452. }
  453. // Continue, as we're done with this token.
  454. continue;
  455. }//end if
  456. /*
  457. If this is a heredoc, PHP will tokenize the whole
  458. thing which causes problems when heredocs don't
  459. contain real PHP code, which is almost never.
  460. We want to leave the start and end heredoc tokens
  461. alone though.
  462. */
  463. if ($tokenIsArray === true && $token[0] === T_START_HEREDOC) {
  464. // Add the start heredoc token to the final array.
  465. $finalTokens[$newStackPtr] = self::standardiseToken($token);
  466. // Check if this is actually a nowdoc and use a different token
  467. // to help the sniffs.
  468. $nowdoc = false;
  469. if ($token[1][3] === "'") {
  470. $finalTokens[$newStackPtr]['code'] = T_START_NOWDOC;
  471. $finalTokens[$newStackPtr]['type'] = 'T_START_NOWDOC';
  472. $nowdoc = true;
  473. }
  474. $tokenContent = '';
  475. for ($i = ($stackPtr + 1); $i < $numTokens; $i++) {
  476. $subTokenIsArray = is_array($tokens[$i]);
  477. if ($subTokenIsArray === true
  478. && $tokens[$i][0] === T_END_HEREDOC
  479. ) {
  480. // We found the other end of the heredoc.
  481. break;
  482. }
  483. if ($subTokenIsArray === true) {
  484. $tokenContent .= $tokens[$i][1];
  485. } else {
  486. $tokenContent .= $tokens[$i];
  487. }
  488. }
  489. if ($i === $numTokens) {
  490. // We got to the end of the file and never
  491. // found the closing token, so this probably wasn't
  492. // a heredoc.
  493. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  494. $type = $finalTokens[$newStackPtr]['type'];
  495. echo "\t\t* failed to find the end of the here/nowdoc".PHP_EOL;
  496. echo "\t\t* token $stackPtr changed from $type to T_STRING".PHP_EOL;
  497. }
  498. $finalTokens[$newStackPtr]['code'] = T_STRING;
  499. $finalTokens[$newStackPtr]['type'] = 'T_STRING';
  500. $newStackPtr++;
  501. continue;
  502. }
  503. $stackPtr = $i;
  504. $newStackPtr++;
  505. // Convert each line within the heredoc to a
  506. // new token, so it conforms with other multiple line tokens.
  507. $tokenLines = explode($eolChar, $tokenContent);
  508. $numLines = count($tokenLines);
  509. $newToken = array();
  510. for ($j = 0; $j < $numLines; $j++) {
  511. $newToken['content'] = $tokenLines[$j];
  512. if ($j === ($numLines - 1)) {
  513. if ($tokenLines[$j] === '') {
  514. break;
  515. }
  516. } else {
  517. $newToken['content'] .= $eolChar;
  518. }
  519. if ($nowdoc === true) {
  520. $newToken['code'] = T_NOWDOC;
  521. $newToken['type'] = 'T_NOWDOC';
  522. } else {
  523. $newToken['code'] = T_HEREDOC;
  524. $newToken['type'] = 'T_HEREDOC';
  525. }
  526. $finalTokens[$newStackPtr] = $newToken;
  527. $newStackPtr++;
  528. }//end for
  529. // Add the end heredoc token to the final array.
  530. $finalTokens[$newStackPtr] = self::standardiseToken($tokens[$stackPtr]);
  531. if ($nowdoc === true) {
  532. $finalTokens[$newStackPtr]['code'] = T_END_NOWDOC;
  533. $finalTokens[$newStackPtr]['type'] = 'T_END_NOWDOC';
  534. $nowdoc = true;
  535. }
  536. $newStackPtr++;
  537. // Continue, as we're done with this token.
  538. continue;
  539. }//end if
  540. /*
  541. Before PHP 5.6, the ... operator was tokenized as three
  542. T_STRING_CONCAT tokens in a row. So look for and combine
  543. these tokens in earlier versions.
  544. */
  545. if ($tokenIsArray === false
  546. && $token[0] === '.'
  547. && isset($tokens[($stackPtr + 1)]) === true
  548. && isset($tokens[($stackPtr + 2)]) === true
  549. && $tokens[($stackPtr + 1)] === '.'
  550. && $tokens[($stackPtr + 2)] === '.'
  551. ) {
  552. $newToken = array();
  553. $newToken['code'] = T_ELLIPSIS;
  554. $newToken['type'] = 'T_ELLIPSIS';
  555. $newToken['content'] = '...';
  556. $finalTokens[$newStackPtr] = $newToken;
  557. $newStackPtr++;
  558. $stackPtr += 2;
  559. continue;
  560. }
  561. /*
  562. Before PHP 5.6, the ** operator was tokenized as two
  563. T_MULTIPLY tokens in a row. So look for and combine
  564. these tokens in earlier versions.
  565. */
  566. if ($tokenIsArray === false
  567. && $token[0] === '*'
  568. && isset($tokens[($stackPtr + 1)]) === true
  569. && $tokens[($stackPtr + 1)] === '*'
  570. ) {
  571. $newToken = array();
  572. $newToken['code'] = T_POW;
  573. $newToken['type'] = 'T_POW';
  574. $newToken['content'] = '**';
  575. $finalTokens[$newStackPtr] = $newToken;
  576. $newStackPtr++;
  577. $stackPtr++;
  578. continue;
  579. }
  580. /*
  581. Before PHP 5.6, the **= operator was tokenized as
  582. T_MULTIPLY followed by T_MUL_EQUAL. So look for and combine
  583. these tokens in earlier versions.
  584. */
  585. if ($tokenIsArray === false
  586. && $token[0] === '*'
  587. && isset($tokens[($stackPtr + 1)]) === true
  588. && is_array($tokens[($stackPtr + 1)]) === true
  589. && $tokens[($stackPtr + 1)][1] === '*='
  590. ) {
  591. $newToken = array();
  592. $newToken['code'] = T_POW_EQUAL;
  593. $newToken['type'] = 'T_POW_EQUAL';
  594. $newToken['content'] = '**=';
  595. $finalTokens[$newStackPtr] = $newToken;
  596. $newStackPtr++;
  597. $stackPtr++;
  598. continue;
  599. }
  600. /*
  601. Before PHP 7, the ??= operator was tokenized as
  602. T_INLINE_THEN, T_INLINE_THEN, T_EQUAL.
  603. Between PHP 7.0 and 7.2, the ??= operator was tokenized as
  604. T_COALESCE, T_EQUAL.
  605. So look for and combine these tokens in earlier versions.
  606. */
  607. if (($tokenIsArray === false
  608. && $token[0] === '?'
  609. && isset($tokens[($stackPtr + 1)]) === true
  610. && $tokens[($stackPtr + 1)][0] === '?'
  611. && isset($tokens[($stackPtr + 2)]) === true
  612. && $tokens[($stackPtr + 2)][0] === '=')
  613. || ($tokenIsArray === true
  614. && $token[0] === T_COALESCE
  615. && isset($tokens[($stackPtr + 1)]) === true
  616. && $tokens[($stackPtr + 1)][0] === '=')
  617. ) {
  618. $newToken = array();
  619. $newToken['code'] = T_COALESCE_EQUAL;
  620. $newToken['type'] = 'T_COALESCE_EQUAL';
  621. $newToken['content'] = '??=';
  622. $finalTokens[$newStackPtr] = $newToken;
  623. $newStackPtr++;
  624. $stackPtr++;
  625. if ($tokenIsArray === false) {
  626. // Pre PHP 7.
  627. $stackPtr++;
  628. }
  629. continue;
  630. }
  631. /*
  632. Before PHP 7, the ?? operator was tokenized as
  633. T_INLINE_THEN followed by T_INLINE_THEN.
  634. So look for and combine these tokens in earlier versions.
  635. */
  636. if ($tokenIsArray === false
  637. && $token[0] === '?'
  638. && isset($tokens[($stackPtr + 1)]) === true
  639. && $tokens[($stackPtr + 1)][0] === '?'
  640. ) {
  641. $newToken = array();
  642. $newToken['code'] = T_COALESCE;
  643. $newToken['type'] = 'T_COALESCE';
  644. $newToken['content'] = '??';
  645. $finalTokens[$newStackPtr] = $newToken;
  646. $newStackPtr++;
  647. $stackPtr++;
  648. continue;
  649. }
  650. /*
  651. Convert ? to T_NULLABLE OR T_INLINE_THEN
  652. */
  653. if ($tokenIsArray === false && $token[0] === '?') {
  654. $newToken = array();
  655. $newToken['content'] = '?';
  656. for ($i = ($stackPtr - 1); $i >= 0; $i--) {
  657. if (is_array($tokens[$i]) === true) {
  658. $tokenType = $tokens[$i][0];
  659. } else {
  660. $tokenType = $tokens[$i];
  661. }
  662. if ($tokenType === T_FUNCTION) {
  663. $newToken['code'] = T_NULLABLE;
  664. $newToken['type'] = 'T_NULLABLE';
  665. break;
  666. } else if (in_array($tokenType, array(T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, '{', ';')) === true) {
  667. $newToken['code'] = T_INLINE_THEN;
  668. $newToken['type'] = 'T_INLINE_THEN';
  669. $insideInlineIf[] = $stackPtr;
  670. break;
  671. }
  672. }
  673. $finalTokens[$newStackPtr] = $newToken;
  674. $newStackPtr++;
  675. continue;
  676. }//end if
  677. /*
  678. Tokens after a double colon may be look like scope openers,
  679. such as when writing code like Foo::NAMESPACE, but they are
  680. only ever variables or strings.
  681. */
  682. if ($stackPtr > 1
  683. && (is_array($tokens[($stackPtr - 1)]) === true
  684. && $tokens[($stackPtr - 1)][0] === T_PAAMAYIM_NEKUDOTAYIM)
  685. && $tokenIsArray === true
  686. && $token[0] !== T_STRING
  687. && $token[0] !== T_VARIABLE
  688. && $token[0] !== T_DOLLAR
  689. && isset(PHP_CodeSniffer_Tokens::$emptyTokens[$token[0]]) === false
  690. ) {
  691. $newToken = array();
  692. $newToken['code'] = T_STRING;
  693. $newToken['type'] = 'T_STRING';
  694. $newToken['content'] = $token[1];
  695. $finalTokens[$newStackPtr] = $newToken;
  696. $newStackPtr++;
  697. continue;
  698. }
  699. /*
  700. The string-like token after a function keyword should always be
  701. tokenized as T_STRING even if it appears to be a different token,
  702. such as when writing code like: function default(): foo
  703. so go forward and change the token type before it is processed.
  704. */
  705. if ($tokenIsArray === true && $token[0] === T_FUNCTION) {
  706. for ($x = ($stackPtr + 1); $x < $numTokens; $x++) {
  707. if (is_array($tokens[$x]) === false
  708. || isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x][0]]) === false
  709. ) {
  710. // Non-empty content.
  711. break;
  712. }
  713. }
  714. if ($x < $numTokens && is_array($tokens[$x]) === true) {
  715. $tokens[$x][0] = T_STRING;
  716. }
  717. }
  718. /*
  719. Before PHP 7, the <=> operator was tokenized as
  720. T_IS_SMALLER_OR_EQUAL followed by T_GREATER_THAN.
  721. So look for and combine these tokens in earlier versions.
  722. */
  723. if ($tokenIsArray === true
  724. && $token[0] === T_IS_SMALLER_OR_EQUAL
  725. && isset($tokens[($stackPtr + 1)]) === true
  726. && $tokens[($stackPtr + 1)][0] === '>'
  727. ) {
  728. $newToken = array();
  729. $newToken['code'] = T_SPACESHIP;
  730. $newToken['type'] = 'T_SPACESHIP';
  731. $newToken['content'] = '<=>';
  732. $finalTokens[$newStackPtr] = $newToken;
  733. $newStackPtr++;
  734. $stackPtr++;
  735. continue;
  736. }
  737. /*
  738. Emulate traits in PHP versions less than 5.4.
  739. */
  740. if ($tokenIsArray === true
  741. && $token[0] === T_STRING
  742. && strtolower($token[1]) === 'trait'
  743. && $tokens[($stackPtr - 1)][0] !== T_OBJECT_OPERATOR
  744. && $tokens[($stackPtr - 1)][0] !== T_PAAMAYIM_NEKUDOTAYIM
  745. ) {
  746. $finalTokens[$newStackPtr] = array(
  747. 'content' => $token[1],
  748. 'code' => T_TRAIT,
  749. 'type' => 'T_TRAIT',
  750. );
  751. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  752. echo "\t\t* token $stackPtr changed from T_STRING to T_TRAIT".PHP_EOL;
  753. }
  754. $newStackPtr++;
  755. continue;
  756. }
  757. /*
  758. PHP doesn't assign a token to goto labels, so we have to.
  759. These are just string tokens with a single colon after them. Double
  760. colons are already tokenized and so don't interfere with this check.
  761. But we do have to account for CASE statements, that look just like
  762. goto labels.
  763. */
  764. if ($tokenIsArray === true
  765. && $token[0] === T_STRING
  766. && isset($tokens[($stackPtr + 1)]) === true
  767. && $tokens[($stackPtr + 1)] === ':'
  768. && $tokens[($stackPtr - 1)][0] !== T_PAAMAYIM_NEKUDOTAYIM
  769. ) {
  770. $stopTokens = array(
  771. T_CASE => true,
  772. T_SEMICOLON => true,
  773. T_OPEN_CURLY_BRACKET => true,
  774. T_INLINE_THEN => true,
  775. );
  776. for ($x = ($newStackPtr - 1); $x > 0; $x--) {
  777. if (isset($stopTokens[$finalTokens[$x]['code']]) === true) {
  778. break;
  779. }
  780. }
  781. if ($finalTokens[$x]['code'] !== T_CASE
  782. && $finalTokens[$x]['code'] !== T_INLINE_THEN
  783. ) {
  784. $finalTokens[$newStackPtr] = array(
  785. 'content' => $token[1].':',
  786. 'code' => T_GOTO_LABEL,
  787. 'type' => 'T_GOTO_LABEL',
  788. );
  789. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  790. echo "\t\t* token $stackPtr changed from T_STRING to T_GOTO_LABEL".PHP_EOL;
  791. echo "\t\t* skipping T_COLON token ".($stackPtr + 1).PHP_EOL;
  792. }
  793. $newStackPtr++;
  794. $stackPtr++;
  795. continue;
  796. }
  797. }//end if
  798. /*
  799. HHVM 3.5 tokenizes "else[\s]+if" as a T_ELSEIF token while PHP
  800. proper only tokenizes "elseif" as a T_ELSEIF token. So split
  801. up the HHVM token to make it looks like proper PHP.
  802. */
  803. if ($tokenIsArray === true
  804. && $token[0] === T_ELSEIF
  805. && strtolower($token[1]) !== 'elseif'
  806. ) {
  807. $finalTokens[$newStackPtr] = array(
  808. 'content' => substr($token[1], 0, 4),
  809. 'code' => T_ELSE,
  810. 'type' => 'T_ELSE',
  811. );
  812. $newStackPtr++;
  813. $finalTokens[$newStackPtr] = array(
  814. 'content' => substr($token[1], 4, -2),
  815. 'code' => T_WHITESPACE,
  816. 'type' => 'T_WHITESPACE',
  817. );
  818. $newStackPtr++;
  819. $finalTokens[$newStackPtr] = array(
  820. 'content' => substr($token[1], -2),
  821. 'code' => T_IF,
  822. 'type' => 'T_IF',
  823. );
  824. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  825. echo "\t\t* token $stackPtr changed from T_ELSEIF to T_ELSE/T_WHITESPACE/T_IF".PHP_EOL;
  826. }
  827. $newStackPtr++;
  828. continue;
  829. }//end if
  830. /*
  831. HHVM 3.5 and 3.6 tokenizes a hashbang line such as #!/usr/bin/php
  832. as T_HASHANG while PHP proper uses T_INLINE_HTML.
  833. */
  834. if ($tokenIsArray === true && token_name($token[0]) === 'T_HASHBANG') {
  835. $finalTokens[$newStackPtr] = array(
  836. 'content' => $token[1],
  837. 'code' => T_INLINE_HTML,
  838. 'type' => 'T_INLINE_HTML',
  839. );
  840. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  841. echo "\t\t* token $stackPtr changed from T_HASHBANG to T_INLINE_HTML".PHP_EOL;
  842. }
  843. $newStackPtr++;
  844. continue;
  845. }//end if
  846. /*
  847. If this token has newlines in its content, split each line up
  848. and create a new token for each line. We do this so it's easier
  849. to ascertain where errors occur on a line.
  850. Note that $token[1] is the token's content.
  851. */
  852. if ($tokenIsArray === true && strpos($token[1], $eolChar) !== false) {
  853. $tokenLines = explode($eolChar, $token[1]);
  854. $numLines = count($tokenLines);
  855. $newToken = array(
  856. 'type' => token_name($token[0]),
  857. 'code' => $token[0],
  858. 'content' => '',
  859. );
  860. for ($i = 0; $i < $numLines; $i++) {
  861. $newToken['content'] = $tokenLines[$i];
  862. if ($i === ($numLines - 1)) {
  863. if ($tokenLines[$i] === '') {
  864. break;
  865. }
  866. } else {
  867. $newToken['content'] .= $eolChar;
  868. }
  869. $finalTokens[$newStackPtr] = $newToken;
  870. $newStackPtr++;
  871. }
  872. } else {
  873. if ($tokenIsArray === true && $token[0] === T_STRING) {
  874. // Some T_STRING tokens should remain that way
  875. // due to their context.
  876. $context = array(
  877. T_OBJECT_OPERATOR => true,
  878. T_FUNCTION => true,
  879. T_CLASS => true,
  880. T_EXTENDS => true,
  881. T_IMPLEMENTS => true,
  882. T_NEW => true,
  883. T_CONST => true,
  884. T_NS_SEPARATOR => true,
  885. T_USE => true,
  886. T_NAMESPACE => true,
  887. T_PAAMAYIM_NEKUDOTAYIM => true,
  888. );
  889. if (isset($context[$finalTokens[$lastNotEmptyToken]['code']]) === true) {
  890. // Special case for syntax like: return new self
  891. // where self should not be a string.
  892. if ($finalTokens[$lastNotEmptyToken]['code'] === T_NEW
  893. && strtolower($token[1]) === 'self'
  894. ) {
  895. $finalTokens[$newStackPtr] = array(
  896. 'content' => $token[1],
  897. 'code' => T_SELF,
  898. 'type' => 'T_SELF',
  899. );
  900. } else {
  901. $finalTokens[$newStackPtr] = array(
  902. 'content' => $token[1],
  903. 'code' => T_STRING,
  904. 'type' => 'T_STRING',
  905. );
  906. }
  907. $newStackPtr++;
  908. continue;
  909. }//end if
  910. }//end if
  911. $newToken = null;
  912. if ($tokenIsArray === false) {
  913. if (isset(self::$_resolveTokenCache[$token[0]]) === true) {
  914. $newToken = self::$_resolveTokenCache[$token[0]];
  915. }
  916. } else {
  917. $cacheKey = null;
  918. if ($token[0] === T_STRING) {
  919. $cacheKey = strtolower($token[1]);
  920. } else if ($token[0] !== T_CURLY_OPEN) {
  921. $cacheKey = $token[0];
  922. }
  923. if ($cacheKey !== null && isset(self::$_resolveTokenCache[$cacheKey]) === true) {
  924. $newToken = self::$_resolveTokenCache[$cacheKey];
  925. $newToken['content'] = $token[1];
  926. }
  927. }
  928. if ($newToken === null) {
  929. $newToken = self::standardiseToken($token);
  930. }
  931. // Convert colons that are actually the ELSE component of an
  932. // inline IF statement.
  933. if (empty($insideInlineIf) === false && $newToken['code'] === T_COLON) {
  934. array_pop($insideInlineIf);
  935. $newToken['code'] = T_INLINE_ELSE;
  936. $newToken['type'] = 'T_INLINE_ELSE';
  937. }
  938. // This is a special condition for T_ARRAY tokens used for
  939. // type hinting function arguments as being arrays. We want to keep
  940. // the parenthesis map clean, so let's tag these tokens as
  941. // T_ARRAY_HINT.
  942. if ($newToken['code'] === T_ARRAY) {
  943. for ($i = $stackPtr; $i < $numTokens; $i++) {
  944. if ($tokens[$i] === '(') {
  945. break;
  946. } else if ($tokens[$i][0] === T_VARIABLE) {
  947. $newToken['code'] = T_ARRAY_HINT;
  948. $newToken['type'] = 'T_ARRAY_HINT';
  949. break;
  950. }
  951. }
  952. }
  953. // This is a special case when checking PHP 5.5+ code in PHP < 5.5
  954. // where "finally" should be T_FINALLY instead of T_STRING.
  955. if ($newToken['code'] === T_STRING
  956. && strtolower($newToken['content']) === 'finally'
  957. ) {
  958. $newToken['code'] = T_FINALLY;
  959. $newToken['type'] = 'T_FINALLY';
  960. }
  961. // This is a special case for the PHP 5.5 classname::class syntax
  962. // where "class" should be T_STRING instead of T_CLASS.
  963. if (($newToken['code'] === T_CLASS
  964. || $newToken['code'] === T_FUNCTION)
  965. && $finalTokens[($newStackPtr - 1)]['code'] === T_DOUBLE_COLON
  966. ) {
  967. $newToken['code'] = T_STRING;
  968. $newToken['type'] = 'T_STRING';
  969. }
  970. // This is a special case for PHP 5.6 use function and use const
  971. // where "function" and "const" should be T_STRING instead of T_FUNCTION
  972. // and T_CONST.
  973. if (($newToken['code'] === T_FUNCTION
  974. || $newToken['code'] === T_CONST)
  975. && $finalTokens[$lastNotEmptyToken]['code'] === T_USE
  976. ) {
  977. $newToken['code'] = T_STRING;
  978. $newToken['type'] = 'T_STRING';
  979. }
  980. // This is a special case for use groups in PHP 7+ where leaving
  981. // the curly braces as their normal tokens would confuse
  982. // the scope map and sniffs.
  983. if ($newToken['code'] === T_OPEN_CURLY_BRACKET
  984. && $finalTokens[$lastNotEmptyToken]['code'] === T_NS_SEPARATOR
  985. ) {
  986. $newToken['code'] = T_OPEN_USE_GROUP;
  987. $newToken['type'] = 'T_OPEN_USE_GROUP';
  988. $insideUseGroup = true;
  989. }
  990. if ($insideUseGroup === true && $newToken['code'] === T_CLOSE_CURLY_BRACKET) {
  991. $newToken['code'] = T_CLOSE_USE_GROUP;
  992. $newToken['type'] = 'T_CLOSE_USE_GROUP';
  993. $insideUseGroup = false;
  994. }
  995. $finalTokens[$newStackPtr] = $newToken;
  996. $newStackPtr++;
  997. }//end if
  998. }//end for
  999. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1000. echo "\t*** END PHP TOKENIZING ***".PHP_EOL;
  1001. }
  1002. return $finalTokens;
  1003. }//end tokenizeString()
  1004. /**
  1005. * Performs additional processing after main tokenizing.
  1006. *
  1007. * This additional processing checks for CASE statements that are using curly
  1008. * braces for scope openers and closers. It also turns some T_FUNCTION tokens
  1009. * into T_CLOSURE when they are not standard function definitions. It also
  1010. * detects short array syntax and converts those square brackets into new tokens.
  1011. * It also corrects some usage of the static and class keywords. It also
  1012. * assigns tokens to function return types.
  1013. *
  1014. * @param array $tokens The array of tokens to process.
  1015. * @param string $eolChar The EOL character to use for splitting strings.
  1016. *
  1017. * @return void
  1018. */
  1019. public function processAdditional(&$tokens, $eolChar)
  1020. {
  1021. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1022. echo "\t*** START ADDITIONAL PHP PROCESSING ***".PHP_EOL;
  1023. }
  1024. $numTokens = count($tokens);
  1025. for ($i = ($numTokens - 1); $i >= 0; $i--) {
  1026. // Check for any unset scope conditions due to alternate IF/ENDIF syntax.
  1027. if (isset($tokens[$i]['scope_opener']) === true
  1028. && isset($tokens[$i]['scope_condition']) === false
  1029. ) {
  1030. $tokens[$i]['scope_condition'] = $tokens[$tokens[$i]['scope_opener']]['scope_condition'];
  1031. }
  1032. if ($tokens[$i]['code'] === T_FUNCTION) {
  1033. /*
  1034. Detect functions that are actually closures and
  1035. assign them a different token.
  1036. */
  1037. if (isset($tokens[$i]['scope_opener']) === true) {
  1038. for ($x = ($i + 1); $x < $numTokens; $x++) {
  1039. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false
  1040. && $tokens[$x]['code'] !== T_BITWISE_AND
  1041. ) {
  1042. break;
  1043. }
  1044. }
  1045. if ($tokens[$x]['code'] === T_OPEN_PARENTHESIS) {
  1046. $tokens[$i]['code'] = T_CLOSURE;
  1047. $tokens[$i]['type'] = 'T_CLOSURE';
  1048. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1049. $line = $tokens[$i]['line'];
  1050. echo "\t* token $i on line $line changed from T_FUNCTION to T_CLOSURE".PHP_EOL;
  1051. }
  1052. for ($x = ($tokens[$i]['scope_opener'] + 1); $x < $tokens[$i]['scope_closer']; $x++) {
  1053. if (isset($tokens[$x]['conditions'][$i]) === false) {
  1054. continue;
  1055. }
  1056. $tokens[$x]['conditions'][$i] = T_CLOSURE;
  1057. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1058. $type = $tokens[$x]['type'];
  1059. echo "\t\t* cleaned $x ($type) *".PHP_EOL;
  1060. }
  1061. }
  1062. }
  1063. $tokenAfterReturnTypeHint = $tokens[$i]['scope_opener'];
  1064. } else if (isset($tokens[$i]['parenthesis_closer']) === true) {
  1065. $tokenAfterReturnTypeHint = null;
  1066. for ($x = ($tokens[$i]['parenthesis_closer'] + 1); $x < $numTokens; $x++) {
  1067. if ($tokens[$x]['code'] === T_SEMICOLON) {
  1068. $tokenAfterReturnTypeHint = $x;
  1069. break;
  1070. }
  1071. }
  1072. if ($tokenAfterReturnTypeHint === null) {
  1073. // Probably a syntax error.
  1074. continue;
  1075. }
  1076. } else {
  1077. // Probably a syntax error.
  1078. continue;
  1079. }//end if
  1080. /*
  1081. Detect function return values and assign them
  1082. a special token, because PHP doesn't.
  1083. */
  1084. for ($x = ($tokenAfterReturnTypeHint - 1); $x > $i; $x--) {
  1085. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1086. if (in_array($tokens[$x]['code'], array(T_STRING, T_ARRAY, T_ARRAY_HINT, T_CALLABLE, T_SELF, T_PARENT), true) === true) {
  1087. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1088. $line = $tokens[$x]['line'];
  1089. $type = $tokens[$x]['type'];
  1090. echo "\t* token $x on line $line changed from $type to T_RETURN_TYPE".PHP_EOL;
  1091. }
  1092. $tokens[$x]['code'] = T_RETURN_TYPE;
  1093. $tokens[$x]['type'] = 'T_RETURN_TYPE';
  1094. }
  1095. break;
  1096. }
  1097. }
  1098. continue;
  1099. } else if ($tokens[$i]['code'] === T_CLASS && isset($tokens[$i]['scope_opener']) === true) {
  1100. /*
  1101. Detect anonymous classes and assign them a different token.
  1102. */
  1103. for ($x = ($i + 1); $x < $numTokens; $x++) {
  1104. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1105. break;
  1106. }
  1107. }
  1108. if ($tokens[$x]['code'] === T_OPEN_PARENTHESIS
  1109. || $tokens[$x]['code'] === T_OPEN_CURLY_BRACKET
  1110. || $tokens[$x]['code'] === T_EXTENDS
  1111. || $tokens[$x]['code'] === T_IMPLEMENTS
  1112. ) {
  1113. $tokens[$i]['code'] = T_ANON_CLASS;
  1114. $tokens[$i]['type'] = 'T_ANON_CLASS';
  1115. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1116. $line = $tokens[$i]['line'];
  1117. echo "\t* token $i on line $line changed from T_CLASS to T_ANON_CLASS".PHP_EOL;
  1118. }
  1119. for ($x = ($tokens[$i]['scope_opener'] + 1); $x < $tokens[$i]['scope_closer']; $x++) {
  1120. if (isset($tokens[$x]['conditions'][$i]) === false) {
  1121. continue;
  1122. }
  1123. $tokens[$x]['conditions'][$i] = T_ANON_CLASS;
  1124. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1125. $type = $tokens[$x]['type'];
  1126. echo "\t\t* cleaned $x ($type) *".PHP_EOL;
  1127. }
  1128. }
  1129. }
  1130. continue;
  1131. } else if ($tokens[$i]['code'] === T_OPEN_SQUARE_BRACKET) {
  1132. if (isset($tokens[$i]['bracket_closer']) === false) {
  1133. continue;
  1134. }
  1135. // Unless there is a variable or a bracket before this token,
  1136. // it is the start of an array being defined using the short syntax.
  1137. $isShortArray = false;
  1138. $allowed = array(
  1139. T_CLOSE_SQUARE_BRACKET => T_CLOSE_SQUARE_BRACKET,
  1140. T_CLOSE_PARENTHESIS => T_CLOSE_PARENTHESIS,
  1141. T_VARIABLE => T_VARIABLE,
  1142. T_OBJECT_OPERATOR => T_OBJECT_OPERATOR,
  1143. T_STRING => T_STRING,
  1144. );
  1145. for ($x = ($i - 1); $x > 0; $x--) {
  1146. // If we hit a scope opener, the statement has ended
  1147. // without finding anything, so it's probably an array
  1148. // using PHP 7.1 short list syntax.
  1149. if (isset($tokens[$x]['scope_opener']) === true) {
  1150. $isShortArray = true;
  1151. break;
  1152. }
  1153. if (isset($tokens[$x]['bracket_opener']) === true
  1154. && $x > $tokens[$x]['bracket_opener']
  1155. ) {
  1156. $x = $tokens[$x]['bracket_opener'];
  1157. continue;
  1158. }
  1159. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1160. if (isset($allowed[$tokens[$x]['code']]) === false) {
  1161. $isShortArray = true;
  1162. }
  1163. break;
  1164. }
  1165. }//end for
  1166. if ($isShortArray === true) {
  1167. $tokens[$i]['code'] = T_OPEN_SHORT_ARRAY;
  1168. $tokens[$i]['type'] = 'T_OPEN_SHORT_ARRAY';
  1169. $closer = $tokens[$i]['bracket_closer'];
  1170. $tokens[$closer]['code'] = T_CLOSE_SHORT_ARRAY;
  1171. $tokens[$closer]['type'] = 'T_CLOSE_SHORT_ARRAY';
  1172. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1173. $line = $tokens[$i]['line'];
  1174. echo "\t* token $i on line $line changed from T_OPEN_SQUARE_BRACKET to T_OPEN_SHORT_ARRAY".PHP_EOL;
  1175. $line = $tokens[$closer]['line'];
  1176. echo "\t* token $closer on line $line changed from T_CLOSE_SQUARE_BRACKET to T_CLOSE_SHORT_ARRAY".PHP_EOL;
  1177. }
  1178. }
  1179. continue;
  1180. } else if ($tokens[$i]['code'] === T_STATIC) {
  1181. for ($x = ($i - 1); $x > 0; $x--) {
  1182. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1183. break;
  1184. }
  1185. }
  1186. if ($tokens[$x]['code'] === T_INSTANCEOF) {
  1187. $tokens[$i]['code'] = T_STRING;
  1188. $tokens[$i]['type'] = 'T_STRING';
  1189. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1190. $line = $tokens[$i]['line'];
  1191. echo "\t* token $i on line $line changed from T_STATIC to T_STRING".PHP_EOL;
  1192. }
  1193. }
  1194. continue;
  1195. } else if ($tokens[$i]['code'] === T_ECHO && $tokens[$i]['content'] === '<?=') {
  1196. // HHVM tokenizes <?= as T_ECHO but it should be T_OPEN_TAG_WITH_ECHO.
  1197. $tokens[$i]['code'] = T_OPEN_TAG_WITH_ECHO;
  1198. $tokens[$i]['type'] = 'T_OPEN_TAG_WITH_ECHO';
  1199. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1200. $line = $tokens[$i]['line'];
  1201. echo "\t* token $i on line $line changed from T_ECHO to T_OPEN_TAG_WITH_ECHO".PHP_EOL;
  1202. }
  1203. } else if ($tokens[$i]['code'] === T_TRUE
  1204. || $tokens[$i]['code'] === T_FALSE
  1205. || $tokens[$i]['code'] === T_NULL
  1206. ) {
  1207. for ($x = ($i + 1); $i < $numTokens; $x++) {
  1208. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1209. // Non-whitespace content.
  1210. break;
  1211. }
  1212. }
  1213. $context = array(
  1214. T_OBJECT_OPERATOR => true,
  1215. T_NS_SEPARATOR => true,
  1216. T_PAAMAYIM_NEKUDOTAYIM => true,
  1217. );
  1218. if (isset($context[$tokens[$x]['code']]) === true) {
  1219. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1220. $line = $tokens[$i]['line'];
  1221. $type = $tokens[$i]['type'];
  1222. echo "\t* token $i on line $line changed from $type to T_STRING".PHP_EOL;
  1223. }
  1224. $tokens[$i]['code'] = T_STRING;
  1225. $tokens[$i]['type'] = 'T_STRING';
  1226. }
  1227. } else if ($tokens[$i]['code'] === T_CONST) {
  1228. // Context sensitive keywords support.
  1229. for ($x = ($i + 1); $i < $numTokens; $x++) {
  1230. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1231. // Non-whitespace content.
  1232. break;
  1233. }
  1234. }
  1235. if ($tokens[$x]['code'] !== T_STRING) {
  1236. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1237. $line = $tokens[$x]['line'];
  1238. $type = $tokens[$x]['type'];
  1239. echo "\t* token $x on line $line changed from $type to T_STRING".PHP_EOL;
  1240. }
  1241. $tokens[$x]['code'] = T_STRING;
  1242. $tokens[$x]['type'] = 'T_STRING';
  1243. }
  1244. }//end if
  1245. if (($tokens[$i]['code'] !== T_CASE
  1246. && $tokens[$i]['code'] !== T_DEFAULT)
  1247. || isset($tokens[$i]['scope_opener']) === false
  1248. ) {
  1249. // Only interested in CASE and DEFAULT statements from here on in.
  1250. continue;
  1251. }
  1252. $scopeOpener = $tokens[$i]['scope_opener'];
  1253. $scopeCloser = $tokens[$i]['scope_closer'];
  1254. // If the first char after the opener is a curly brace
  1255. // and that brace has been ignored, it is actually
  1256. // opening this case statement and the opener and closer are
  1257. // probably set incorrectly.
  1258. for ($x = ($scopeOpener + 1); $x < $numTokens; $x++) {
  1259. if (isset(PHP_CodeSniffer_Tokens::$emptyTokens[$tokens[$x]['code']]) === false) {
  1260. // Non-whitespace content.
  1261. break;
  1262. }
  1263. }
  1264. if ($tokens[$x]['code'] === T_CASE || $tokens[$x]['code'] === T_DEFAULT) {
  1265. // Special case for multiple CASE statements that share the same
  1266. // closer. Because we are going backwards through the file, this next
  1267. // CASE/DEFAULT statement is already fixed, so just use its closer
  1268. // and don't worry about fixing anything.
  1269. $newCloser = $tokens[$x]['scope_closer'];
  1270. $tokens[$i]['scope_closer'] = $newCloser;
  1271. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1272. $oldType = $tokens[$scopeCloser]['type'];
  1273. $newType = $tokens[$newCloser]['type'];
  1274. $line = $tokens[$i]['line'];
  1275. echo "\t* token $i (T_CASE) on line $line closer changed from $scopeCloser ($oldType) to $newCloser ($newType)".PHP_EOL;
  1276. }
  1277. continue;
  1278. }
  1279. if ($tokens[$x]['code'] !== T_OPEN_CURLY_BRACKET
  1280. || isset($tokens[$x]['scope_condition']) === true
  1281. ) {
  1282. // Not a CASE/DEFAULT with a curly brace opener.
  1283. continue;
  1284. }
  1285. // The closer for this CASE/DEFAULT should be the closing curly brace and
  1286. // not whatever it already is. The opener needs to be the opening curly
  1287. // brace so everything matches up.
  1288. $newCloser = $tokens[$x]['bracket_closer'];
  1289. foreach (array($i, $x, $newCloser) as $index) {
  1290. $tokens[$index]['scope_condition'] = $i;
  1291. $tokens[$index]['scope_opener'] = $x;
  1292. $tokens[$index]['scope_closer'] = $newCloser;
  1293. }
  1294. unset($tokens[$scopeOpener]['scope_condition']);
  1295. unset($tokens[$scopeOpener]['scope_opener']);
  1296. unset($tokens[$scopeOpener]['scope_closer']);
  1297. unset($tokens[$scopeCloser]['scope_condition']);
  1298. unset($tokens[$scopeCloser]['scope_opener']);
  1299. unset($tokens[$scopeCloser]['scope_closer']);
  1300. unset($tokens[$x]['bracket_opener']);
  1301. unset($tokens[$x]['bracket_closer']);
  1302. unset($tokens[$newCloser]['bracket_opener']);
  1303. unset($tokens[$newCloser]['bracket_closer']);
  1304. $tokens[$scopeCloser]['conditions'][] = $i;
  1305. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1306. $line = $tokens[$i]['line'];
  1307. $tokenType = $tokens[$i]['type'];
  1308. $oldType = $tokens[$scopeOpener]['type'];
  1309. $newType = $tokens[$x]['type'];
  1310. echo "\t* token $i ($tokenType) on line $line opener changed from $scopeOpener ($oldType) to $x ($newType)".PHP_EOL;
  1311. $oldType = $tokens[$scopeCloser]['type'];
  1312. $newType = $tokens[$newCloser]['type'];
  1313. echo "\t* token $i ($tokenType) on line $line closer changed from $scopeCloser ($oldType) to $newCloser ($newType)".PHP_EOL;
  1314. }
  1315. // Now fix up all the tokens that think they are
  1316. // inside the CASE/DEFAULT statement when they are really outside.
  1317. for ($x = $newCloser; $x < $scopeCloser; $x++) {
  1318. foreach ($tokens[$x]['conditions'] as $num => $oldCond) {
  1319. if ($oldCond === $tokens[$i]['code']) {
  1320. $oldConditions = $tokens[$x]['conditions'];
  1321. unset($tokens[$x]['conditions'][$num]);
  1322. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1323. $type = $tokens[$x]['type'];
  1324. $oldConds = '';
  1325. foreach ($oldConditions as $condition) {
  1326. $oldConds .= token_name($condition).',';
  1327. }
  1328. $oldConds = rtrim($oldConds, ',');
  1329. $newConds = '';
  1330. foreach ($tokens[$x]['conditions'] as $condition) {
  1331. $newConds .= token_name($condition).',';
  1332. }
  1333. $newConds = rtrim($newConds, ',');
  1334. echo "\t\t* cleaned $x ($type) *".PHP_EOL;
  1335. echo "\t\t\t=> conditions changed from $oldConds to $newConds".PHP_EOL;
  1336. }
  1337. break;
  1338. }//end if
  1339. }//end foreach
  1340. }//end for
  1341. }//end for
  1342. if (PHP_CODESNIFFER_VERBOSITY > 1) {
  1343. echo "\t*** END ADDITIONAL PHP PROCESSING ***".PHP_EOL;
  1344. }
  1345. }//end processAdditional()
  1346. /**
  1347. * Takes a token produced from <code>token_get_all()</code> and produces a
  1348. * more uniform token.
  1349. *
  1350. * @param string|array $token The token to convert.
  1351. *
  1352. * @return array The new token.
  1353. */
  1354. public static function standardiseToken($token)
  1355. {
  1356. if (isset($token[1]) === false) {
  1357. if (isset(self::$_resolveTokenCache[$token[0]]) === true) {
  1358. return self::$_resolveTokenCache[$token[0]];
  1359. }
  1360. } else {
  1361. $cacheKey = null;
  1362. if ($token[0] === T_STRING) {
  1363. $cacheKey = strtolower($token[1]);
  1364. } else if ($token[0] !== T_CURLY_OPEN) {
  1365. $cacheKey = $token[0];
  1366. }
  1367. if ($cacheKey !== null && isset(self::$_resolveTokenCache[$cacheKey]) === true) {
  1368. $newToken = self::$_resolveTokenCache[$cacheKey];
  1369. $newToken['content'] = $token[1];
  1370. return $newToken;
  1371. }
  1372. }
  1373. if (isset($token[1]) === false) {
  1374. return self::resolveSimpleToken($token[0]);
  1375. }
  1376. if ($token[0] === T_STRING) {
  1377. switch ($cacheKey) {
  1378. case 'false':
  1379. $newToken['type'] = 'T_FALSE';
  1380. break;
  1381. case 'true':
  1382. $newToken['type'] = 'T_TRUE';
  1383. break;
  1384. case 'null':
  1385. $newToken['type'] = 'T_NULL';
  1386. break;
  1387. case 'self':
  1388. $newToken['type'] = 'T_SELF';
  1389. break;
  1390. case 'parent':
  1391. $newToken['type'] = 'T_PARENT';
  1392. break;
  1393. default:
  1394. $newToken['type'] = 'T_STRING';
  1395. break;
  1396. }
  1397. $newToken['code'] = constant($newToken['type']);
  1398. self::$_resolveTokenCache[$cacheKey] = $newToken;
  1399. } else if ($token[0] === T_CURLY_OPEN) {
  1400. $newToken = array(
  1401. 'code' => T_OPEN_CURLY_BRACKET,
  1402. 'type' => 'T_OPEN_CURLY_BRACKET',
  1403. );
  1404. } else {
  1405. $newToken = array(
  1406. 'code' => $token[0],
  1407. 'type' => token_name($token[0]),
  1408. );
  1409. self::$_resolveTokenCache[$token[0]] = $newToken;
  1410. }//end if
  1411. $newToken['content'] = $token[1];
  1412. return $newToken;
  1413. }//end standardiseToken()
  1414. /**
  1415. * Converts simple tokens into a format that conforms to complex tokens
  1416. * produced by token_get_all().
  1417. *
  1418. * Simple tokens are tokens that are not in array form when produced from
  1419. * token_get_all().
  1420. *
  1421. * @param string $token The simple token to convert.
  1422. *
  1423. * @return array The new token in array format.
  1424. */
  1425. public static function resolveSimpleToken($token)
  1426. {
  1427. $newToken = array();
  1428. switch ($token) {
  1429. case '{':
  1430. $newToken['type'] = 'T_OPEN_CURLY_BRACKET';
  1431. break;
  1432. case '}':
  1433. $newToken['type'] = 'T_CLOSE_CURLY_BRACKET';
  1434. break;
  1435. case '[':
  1436. $newToken['type'] = 'T_OPEN_SQUARE_BRACKET';
  1437. break;
  1438. case ']':
  1439. $newToken['type'] = 'T_CLOSE_SQUARE_BRACKET';
  1440. break;
  1441. case '(':
  1442. $newToken['type'] = 'T_OPEN_PARENTHESIS';
  1443. break;
  1444. case ')':
  1445. $newToken['type'] = 'T_CLOSE_PARENTHESIS';
  1446. break;
  1447. case ':':
  1448. $newToken['type'] = 'T_COLON';
  1449. break;
  1450. case '.':
  1451. $newToken['type'] = 'T_STRING_CONCAT';
  1452. break;
  1453. case ';':
  1454. $newToken['type'] = 'T_SEMICOLON';
  1455. break;
  1456. case '=':
  1457. $newToken['type'] = 'T_EQUAL';
  1458. break;
  1459. case '*':
  1460. $newToken['type'] = 'T_MULTIPLY';
  1461. break;
  1462. case '/':
  1463. $newToken['type'] = 'T_DIVIDE';
  1464. break;
  1465. case '+':
  1466. $newToken['type'] = 'T_PLUS';
  1467. break;
  1468. case '-':
  1469. $newToken['type'] = 'T_MINUS';
  1470. break;
  1471. case '%':
  1472. $newToken['type'] = 'T_MODULUS';
  1473. break;
  1474. case '^':
  1475. $newToken['type'] = 'T_BITWISE_XOR';
  1476. break;
  1477. case '&':
  1478. $newToken['type'] = 'T_BITWISE_AND';
  1479. break;
  1480. case '|':
  1481. $newToken['type'] = 'T_BITWISE_OR';
  1482. break;
  1483. case '<':
  1484. $newToken['type'] = 'T_LESS_THAN';
  1485. break;
  1486. case '>':
  1487. $newToken['type'] = 'T_GREATER_THAN';
  1488. break;
  1489. case '!':
  1490. $newToken['type'] = 'T_BOOLEAN_NOT';
  1491. break;
  1492. case ',':
  1493. $newToken['type'] = 'T_COMMA';
  1494. break;
  1495. case '@':
  1496. $newToken['type'] = 'T_ASPERAND';
  1497. break;
  1498. case '$':
  1499. $newToken['type'] = 'T_DOLLAR';
  1500. break;
  1501. case '`':
  1502. $newToken['type'] = 'T_BACKTICK';
  1503. break;
  1504. default:
  1505. $newToken['type'] = 'T_NONE';
  1506. break;
  1507. }//end switch
  1508. $newToken['code'] = constant($newToken['type']);
  1509. $newToken['content'] = $token;
  1510. self::$_resolveTokenCache[$token] = $newToken;
  1511. return $newToken;
  1512. }//end resolveSimpleToken()
  1513. }//end class