TokensAnalyzerTest.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Tokenizer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Tokens;
  15. use PhpCsFixer\Tokenizer\TokensAnalyzer;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. * @author Max Voloshin <voloshin.dp@gmail.com>
  19. * @author Gregor Harlan <gharlan@web.de>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Tokenizer\TokensAnalyzer
  24. */
  25. final class TokensAnalyzerTest extends TestCase
  26. {
  27. /**
  28. * @param array<int, array{classIndex: int, type: string}> $expectedElements
  29. *
  30. * @dataProvider provideGetClassyElementsCases
  31. */
  32. public function testGetClassyElements(array $expectedElements, string $source): void
  33. {
  34. $tokens = Tokens::fromCode($source);
  35. array_walk(
  36. $expectedElements,
  37. static function (array &$element, $index) use ($tokens): void {
  38. $element['token'] = $tokens[$index];
  39. ksort($element);
  40. }
  41. );
  42. $tokensAnalyzer = new TokensAnalyzer($tokens);
  43. static::assertSame(
  44. $expectedElements,
  45. $tokensAnalyzer->getClassyElements()
  46. );
  47. }
  48. public static function provideGetClassyElementsCases(): iterable
  49. {
  50. yield 'trait import' => [
  51. [
  52. 10 => [
  53. 'classIndex' => 4,
  54. 'type' => 'trait_import',
  55. ],
  56. 19 => [
  57. 'classIndex' => 4,
  58. 'type' => 'trait_import',
  59. ],
  60. 24 => [
  61. 'classIndex' => 4,
  62. 'type' => 'const',
  63. ],
  64. 35 => [
  65. 'classIndex' => 4,
  66. 'type' => 'method',
  67. ],
  68. 55 => [
  69. 'classIndex' => 49,
  70. 'type' => 'trait_import',
  71. ],
  72. 64 => [
  73. 'classIndex' => 49,
  74. 'type' => 'method',
  75. ],
  76. ],
  77. '<?php
  78. /** */
  79. class Foo
  80. {
  81. use A\B;
  82. //
  83. use Foo;
  84. const A = 1;
  85. public function foo()
  86. {
  87. $a = new class()
  88. {
  89. use Z; // nested trait import
  90. public function bar()
  91. {
  92. echo 123;
  93. }
  94. };
  95. $a->bar();
  96. }
  97. }',
  98. ];
  99. yield [
  100. [
  101. 9 => [
  102. 'classIndex' => 1,
  103. 'type' => 'property',
  104. ],
  105. 14 => [
  106. 'classIndex' => 1,
  107. 'type' => 'property',
  108. ],
  109. 19 => [
  110. 'classIndex' => 1,
  111. 'type' => 'property',
  112. ],
  113. 28 => [
  114. 'classIndex' => 1,
  115. 'type' => 'property',
  116. ],
  117. 42 => [
  118. 'classIndex' => 1,
  119. 'type' => 'const',
  120. ],
  121. 53 => [
  122. 'classIndex' => 1,
  123. 'type' => 'method',
  124. ],
  125. 83 => [
  126. 'classIndex' => 1,
  127. 'type' => 'method',
  128. ],
  129. 140 => [
  130. 'classIndex' => 1,
  131. 'type' => 'method',
  132. ],
  133. 164 => [
  134. 'classIndex' => 158,
  135. 'type' => 'const',
  136. ],
  137. 173 => [
  138. 'classIndex' => 158,
  139. 'type' => 'trait_import',
  140. ],
  141. ],
  142. <<<'PHP'
  143. <?php
  144. class Foo
  145. {
  146. public $prop0;
  147. protected $prop1;
  148. private $prop2 = 1;
  149. var $prop3 = array(1,2,3);
  150. const CONSTANT = 'constant value';
  151. public function bar4()
  152. {
  153. $a = 5;
  154. return " ({$a})";
  155. }
  156. public function bar5($data)
  157. {
  158. $message = $data;
  159. $example = function ($arg) use ($message) {
  160. echo $arg . ' ' . $message;
  161. };
  162. $example('hello');
  163. }function A(){}
  164. }
  165. function test(){}
  166. class Foo2
  167. {
  168. const CONSTANT = 'constant value';
  169. use Foo\Bar; // expected in the return value
  170. }
  171. PHP
  172. ,
  173. ];
  174. }
  175. public function testGetClassyElementsWithNullableProperties(): void
  176. {
  177. $source = <<<'PHP'
  178. <?php
  179. class Foo
  180. {
  181. public int $prop0;
  182. protected ?array $prop1;
  183. private string $prop2 = 1;
  184. var ? Foo\Bar $prop3 = array(1,2,3);
  185. }
  186. PHP;
  187. $tokens = Tokens::fromCode($source);
  188. $tokensAnalyzer = new TokensAnalyzer($tokens);
  189. $elements = $tokensAnalyzer->getClassyElements();
  190. static::assertSame(
  191. [
  192. 11 => [
  193. 'classIndex' => 1,
  194. 'token' => $tokens[11],
  195. 'type' => 'property',
  196. ],
  197. 19 => [
  198. 'classIndex' => 1,
  199. 'token' => $tokens[19],
  200. 'type' => 'property',
  201. ],
  202. 26 => [
  203. 'classIndex' => 1,
  204. 'token' => $tokens[26],
  205. 'type' => 'property',
  206. ],
  207. 41 => [
  208. 'classIndex' => 1,
  209. 'token' => $tokens[41],
  210. 'type' => 'property',
  211. ],
  212. ],
  213. $elements
  214. );
  215. }
  216. public function testGetClassyElementsWithAnonymousClass(): void
  217. {
  218. $source = <<<'PHP'
  219. <?php
  220. class A {
  221. public $A;
  222. private function B()
  223. {
  224. return new class(){
  225. protected $level1;
  226. private function XYZ() {
  227. return new class(){private $level2 = 1;};
  228. }
  229. };
  230. }
  231. private function C() {
  232. }
  233. }
  234. function B() {} // do not count this
  235. PHP;
  236. $tokens = Tokens::fromCode($source);
  237. $tokensAnalyzer = new TokensAnalyzer($tokens);
  238. $elements = $tokensAnalyzer->getClassyElements();
  239. static::assertSame(
  240. [
  241. 9 => [
  242. 'classIndex' => 1,
  243. 'token' => $tokens[9],
  244. 'type' => 'property', // $A
  245. ],
  246. 14 => [
  247. 'classIndex' => 1,
  248. 'token' => $tokens[14],
  249. 'type' => 'method', // B
  250. ],
  251. 33 => [
  252. 'classIndex' => 26,
  253. 'token' => $tokens[33],
  254. 'type' => 'property', // $level1
  255. ],
  256. 38 => [
  257. 'classIndex' => 26,
  258. 'token' => $tokens[38],
  259. 'type' => 'method', // XYZ
  260. ],
  261. 56 => [
  262. 'classIndex' => 50,
  263. 'token' => $tokens[56],
  264. 'type' => 'property', // $level2
  265. ],
  266. 74 => [
  267. 'classIndex' => 1,
  268. 'token' => $tokens[74],
  269. 'type' => 'method', // C
  270. ],
  271. ],
  272. $elements
  273. );
  274. }
  275. public function testGetClassyElementsWithMultipleAnonymousClass(): void
  276. {
  277. $source = <<<'PHP'
  278. <?php class A0
  279. {
  280. public function AA0()
  281. {
  282. return new class
  283. {
  284. public function BB0()
  285. {
  286. }
  287. };
  288. }
  289. public function otherFunction0()
  290. {
  291. }
  292. }
  293. class A1
  294. {
  295. public function AA1()
  296. {
  297. return new class
  298. {
  299. public function BB1()
  300. {
  301. return new class
  302. {
  303. public function CC1()
  304. {
  305. return new class
  306. {
  307. public function DD1()
  308. {
  309. return new class{};
  310. }
  311. public function DD2()
  312. {
  313. return new class{};
  314. }
  315. };
  316. }
  317. };
  318. }
  319. public function BB2()
  320. {
  321. return new class
  322. {
  323. public function CC2()
  324. {
  325. return new class
  326. {
  327. public function DD2()
  328. {
  329. return new class{};
  330. }
  331. };
  332. }
  333. };
  334. }
  335. };
  336. }
  337. public function otherFunction1()
  338. {
  339. }
  340. }
  341. PHP;
  342. $tokens = Tokens::fromCode($source);
  343. $tokensAnalyzer = new TokensAnalyzer($tokens);
  344. $elements = $tokensAnalyzer->getClassyElements();
  345. static::assertSame(
  346. [
  347. 9 => [
  348. 'classIndex' => 1,
  349. 'token' => $tokens[9],
  350. 'type' => 'method',
  351. ],
  352. 27 => [
  353. 'classIndex' => 21,
  354. 'token' => $tokens[27],
  355. 'type' => 'method',
  356. ],
  357. 44 => [
  358. 'classIndex' => 1,
  359. 'token' => $tokens[44],
  360. 'type' => 'method',
  361. ],
  362. 64 => [
  363. 'classIndex' => 56,
  364. 'token' => $tokens[64],
  365. 'type' => 'method',
  366. ],
  367. 82 => [
  368. 'classIndex' => 76,
  369. 'token' => $tokens[82],
  370. 'type' => 'method',
  371. ],
  372. 100 => [
  373. 'classIndex' => 94,
  374. 'token' => $tokens[100],
  375. 'type' => 'method',
  376. ],
  377. 118 => [
  378. 'classIndex' => 112,
  379. 'token' => $tokens[118],
  380. 'type' => 'method',
  381. ],
  382. 139 => [
  383. 'classIndex' => 112,
  384. 'token' => $tokens[139],
  385. 'type' => 'method',
  386. ],
  387. 170 => [
  388. 'classIndex' => 76,
  389. 'token' => $tokens[170],
  390. 'type' => 'method',
  391. ],
  392. 188 => [
  393. 'classIndex' => 182,
  394. 'token' => $tokens[188],
  395. 'type' => 'method',
  396. ],
  397. 206 => [
  398. 'classIndex' => 200,
  399. 'token' => $tokens[206],
  400. 'type' => 'method',
  401. ],
  402. 242 => [
  403. 'classIndex' => 56,
  404. 'token' => $tokens[242],
  405. 'type' => 'method',
  406. ],
  407. ],
  408. $elements
  409. );
  410. }
  411. public function testGetClassyElements74(): void
  412. {
  413. $source = <<<'PHP'
  414. <?php
  415. class Foo
  416. {
  417. public int $bar = 3;
  418. protected ?string $baz;
  419. private ?string $bazNull = null;
  420. public static iterable $staticProp;
  421. public float $x, $y;
  422. var bool $flag1;
  423. var ?bool $flag2;
  424. }
  425. PHP;
  426. $tokens = Tokens::fromCode($source);
  427. $tokensAnalyzer = new TokensAnalyzer($tokens);
  428. $elements = $tokensAnalyzer->getClassyElements();
  429. $expected = [];
  430. foreach ([11, 23, 31, 44, 51, 54, 61, 69] as $index) {
  431. $expected[$index] = [
  432. 'classIndex' => 1,
  433. 'token' => $tokens[$index],
  434. 'type' => 'property',
  435. ];
  436. }
  437. static::assertSame($expected, $elements);
  438. }
  439. /**
  440. * @param array<int, array{classIndex: int, type: string}> $expected
  441. *
  442. * @dataProvider provideGetClassyElements81Cases
  443. *
  444. * @requires PHP 8.1
  445. */
  446. public function testGetClassyElements81(array $expected, string $source): void
  447. {
  448. $tokens = Tokens::fromCode($source);
  449. $tokensAnalyzer = new TokensAnalyzer($tokens);
  450. $elements = $tokensAnalyzer->getClassyElements();
  451. array_walk(
  452. $expected,
  453. static function (array &$element, $index) use ($tokens): void {
  454. $element['token'] = $tokens[$index];
  455. ksort($element);
  456. }
  457. );
  458. static::assertSame($expected, $elements);
  459. }
  460. public static function provideGetClassyElements81Cases(): iterable
  461. {
  462. yield [
  463. [
  464. 11 => [
  465. 'classIndex' => 1,
  466. 'type' => 'property', // $prop1
  467. ],
  468. 20 => [
  469. 'classIndex' => 1,
  470. 'type' => 'property', // $prop2
  471. ],
  472. 29 => [
  473. 'classIndex' => 1,
  474. 'type' => 'property', // $prop13
  475. ],
  476. ],
  477. '<?php
  478. class Foo
  479. {
  480. readonly string $prop1;
  481. readonly public string $prop2;
  482. public readonly string $prop3;
  483. }
  484. ',
  485. ];
  486. yield 'final const' => [
  487. [
  488. 11 => [
  489. 'classIndex' => 1,
  490. 'type' => 'const', // A
  491. ],
  492. 24 => [
  493. 'classIndex' => 1,
  494. 'type' => 'const', // B
  495. ],
  496. ],
  497. '<?php
  498. class Foo
  499. {
  500. final public const A = "1";
  501. public final const B = "2";
  502. }
  503. ',
  504. ];
  505. yield 'enum final const' => [
  506. [
  507. 11 => [
  508. 'classIndex' => 1,
  509. 'type' => 'const', // A
  510. ],
  511. 24 => [
  512. 'classIndex' => 1,
  513. 'type' => 'const', // B
  514. ],
  515. ],
  516. '<?php
  517. enum Foo
  518. {
  519. final public const A = "1";
  520. public final const B = "2";
  521. }
  522. ',
  523. ];
  524. yield 'enum case' => [
  525. [
  526. 12 => [
  527. 'classIndex' => 1,
  528. 'type' => 'const', // Spades
  529. ],
  530. 21 => [
  531. 'classIndex' => 1,
  532. 'type' => 'case', // Hearts
  533. ],
  534. 32 => [
  535. 'classIndex' => 1,
  536. 'type' => 'method', // function tests
  537. ],
  538. 81 => [
  539. 'classIndex' => 75,
  540. 'type' => 'method', // function bar123
  541. ],
  542. 135 => [
  543. 'classIndex' => 127,
  544. 'type' => 'method', // function bar7
  545. ],
  546. ],
  547. '<?php
  548. enum Foo: string
  549. {
  550. private const Spades = 123;
  551. case Hearts = "H";
  552. private function test($foo) {
  553. switch ($foo) {
  554. case 1:
  555. // case 2
  556. case 3:
  557. echo 123;
  558. break;
  559. }
  560. return new class {
  561. public function bar123($foo) {
  562. switch ($foo) {
  563. case 1:
  564. // case 2
  565. case 3:
  566. echo 123;
  567. };
  568. }
  569. };
  570. }
  571. }
  572. class Bar {
  573. public function bar7($foo) {
  574. switch ($foo) {
  575. case 1:
  576. // case 2
  577. case 3:
  578. echo 123;
  579. };
  580. }
  581. }
  582. ',
  583. ];
  584. yield 'enum' => [
  585. [
  586. 10 => [
  587. 'classIndex' => 1,
  588. 'type' => 'case',
  589. ],
  590. 19 => [
  591. 'classIndex' => 1,
  592. 'type' => 'case',
  593. ],
  594. 28 => [
  595. 'classIndex' => 1,
  596. 'type' => 'method',
  597. ],
  598. ],
  599. '<?php
  600. enum Foo: string
  601. {
  602. case Bar = "bar";
  603. case Baz = "baz";
  604. function qux() {
  605. switch (true) {
  606. case "x": break;
  607. }
  608. }
  609. }
  610. ',
  611. ];
  612. }
  613. /**
  614. * @param array<int, array{classIndex: int, type: string}> $expected
  615. *
  616. * @dataProvider provideGetClassyElements82Cases
  617. *
  618. * @requires PHP 8.2
  619. */
  620. public function testGetClassyElements82(array $expected, string $source): void
  621. {
  622. $tokens = Tokens::fromCode($source);
  623. $tokensAnalyzer = new TokensAnalyzer($tokens);
  624. $elements = $tokensAnalyzer->getClassyElements();
  625. array_walk(
  626. $expected,
  627. static function (array &$element, $index) use ($tokens): void {
  628. $element['token'] = $tokens[$index];
  629. ksort($element);
  630. },
  631. );
  632. static::assertSame($expected, $elements);
  633. }
  634. public static function provideGetClassyElements82Cases(): iterable
  635. {
  636. yield 'constant in trait' => [
  637. [
  638. 7 => [
  639. 'classIndex' => 1,
  640. 'type' => 'const',
  641. ],
  642. 18 => [
  643. 'classIndex' => 1,
  644. 'type' => 'const',
  645. ],
  646. ],
  647. <<<'PHP'
  648. <?php
  649. trait Foo
  650. {
  651. const BAR = 0;
  652. final const BAZ = 1;
  653. }
  654. PHP,
  655. ];
  656. }
  657. /**
  658. * @param array<int, bool> $expected
  659. *
  660. * @dataProvider provideIsAnonymousClassCases
  661. */
  662. public function testIsAnonymousClass(array $expected, string $source): void
  663. {
  664. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  665. foreach ($expected as $index => $expectedValue) {
  666. static::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  667. }
  668. }
  669. public static function provideIsAnonymousClassCases(): iterable
  670. {
  671. yield [
  672. [1 => false],
  673. '<?php class foo {}',
  674. ];
  675. yield [
  676. [7 => true],
  677. '<?php $foo = new class() {};',
  678. ];
  679. yield [
  680. [7 => true],
  681. '<?php $foo = new class() extends Foo implements Bar, Baz {};',
  682. ];
  683. yield [
  684. [1 => false, 19 => true],
  685. '<?php class Foo { function bar() { return new class() {}; } }',
  686. ];
  687. yield [
  688. [7 => true, 11 => true],
  689. '<?php $a = new class(new class($d->a) implements B{}) extends C{};',
  690. ];
  691. yield [
  692. [1 => false],
  693. '<?php interface foo {}',
  694. ];
  695. if (\PHP_VERSION_ID >= 8_00_00) {
  696. yield [
  697. [11 => true],
  698. '<?php $object = new #[ExampleAttribute] class(){};',
  699. ];
  700. yield [
  701. [27 => true],
  702. '<?php $object = new #[A] #[B] #[C]#[D]/* */ /** */#[E]class(){};',
  703. ];
  704. }
  705. if (\PHP_VERSION_ID >= 8_01_00) {
  706. yield [
  707. [1 => false],
  708. '<?php enum foo {}',
  709. ];
  710. }
  711. }
  712. /**
  713. * @param array<int, bool> $expected
  714. *
  715. * @dataProvider provideIsLambdaCases
  716. */
  717. public function testIsLambda(array $expected, string $source): void
  718. {
  719. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  720. foreach ($expected as $index => $isLambda) {
  721. static::assertSame($isLambda, $tokensAnalyzer->isLambda($index));
  722. }
  723. }
  724. public static function provideIsLambdaCases(): array
  725. {
  726. return [
  727. [
  728. [1 => false],
  729. '<?php function foo () {};',
  730. ],
  731. [
  732. [1 => false],
  733. '<?php function /** foo */ foo () {};',
  734. ],
  735. [
  736. [5 => true],
  737. '<?php $foo = function () {};',
  738. ],
  739. [
  740. [5 => true],
  741. '<?php $foo = function /** foo */ () {};',
  742. ],
  743. [
  744. [7 => true],
  745. '<?php
  746. preg_replace_callback(
  747. "/(^|[a-z])/",
  748. function (array $matches) {
  749. return "a";
  750. },
  751. $string
  752. );',
  753. ],
  754. [
  755. [5 => true],
  756. '<?php $foo = function &() {};',
  757. ],
  758. [
  759. [6 => true],
  760. '<?php
  761. $a = function (): array {
  762. return [];
  763. };',
  764. ],
  765. [
  766. [2 => false],
  767. '<?php
  768. function foo (): array {
  769. return [];
  770. };',
  771. ],
  772. [
  773. [6 => true],
  774. '<?php
  775. $a = function (): void {
  776. return [];
  777. };',
  778. ],
  779. [
  780. [2 => false],
  781. '<?php
  782. function foo (): void {
  783. return [];
  784. };',
  785. ],
  786. [
  787. [6 => true],
  788. '<?php
  789. $a = function (): ?int {
  790. return [];
  791. };',
  792. ],
  793. [
  794. [6 => true],
  795. '<?php
  796. $a = function (): int {
  797. return [];
  798. };',
  799. ],
  800. [
  801. [2 => false],
  802. '<?php
  803. function foo (): ?int {
  804. return [];
  805. };',
  806. ],
  807. ];
  808. }
  809. /**
  810. * @param array<int, bool> $expected
  811. *
  812. * @dataProvider provideIsLambda74Cases
  813. */
  814. public function testIsLambda74(array $expected, string $source): void
  815. {
  816. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  817. foreach ($expected as $index => $expectedValue) {
  818. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  819. }
  820. }
  821. public static function provideIsLambda74Cases(): iterable
  822. {
  823. yield [
  824. [5 => true],
  825. '<?php $fn = fn() => [];',
  826. ];
  827. yield [
  828. [5 => true],
  829. '<?php $fn = fn () => [];',
  830. ];
  831. }
  832. /**
  833. * @param array<int, bool> $expected
  834. *
  835. * @dataProvider provideIsLambda80Cases
  836. *
  837. * @requires PHP 8.0
  838. */
  839. public function testIsLambda80(array $expected, string $source): void
  840. {
  841. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  842. foreach ($expected as $index => $expectedValue) {
  843. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  844. }
  845. }
  846. public static function provideIsLambda80Cases(): array
  847. {
  848. return [
  849. [
  850. [6 => true],
  851. '<?php
  852. $a = function (): ?static {
  853. return [];
  854. };',
  855. ],
  856. [
  857. [6 => true],
  858. '<?php
  859. $a = function (): static {
  860. return [];
  861. };',
  862. ],
  863. [
  864. [14 => true],
  865. '<?php
  866. $c = 4; //
  867. $a = function(
  868. $a,
  869. $b,
  870. ) use (
  871. $c,
  872. ) {
  873. echo $a + $b + $c;
  874. };
  875. $a(1,2);',
  876. ],
  877. ];
  878. }
  879. public function testIsLambdaInvalid(): void
  880. {
  881. $this->expectException(\LogicException::class);
  882. $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
  883. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  884. $tokensAnalyzer->isLambda(0);
  885. }
  886. /**
  887. * @param array<int, bool> $expected
  888. *
  889. * @dataProvider provideIsConstantInvocationCases
  890. */
  891. public function testIsConstantInvocation(array $expected, string $source): void
  892. {
  893. $this->doIsConstantInvocationTest($expected, $source);
  894. }
  895. public static function provideIsConstantInvocationCases(): array
  896. {
  897. return [
  898. [
  899. [3 => true],
  900. '<?php echo FOO;',
  901. ],
  902. [
  903. [4 => true],
  904. '<?php echo \FOO;',
  905. ],
  906. [
  907. [3 => false, 5 => false, 7 => true],
  908. '<?php echo Foo\Bar\BAR;',
  909. ],
  910. [
  911. [3 => true, 7 => true, 11 => true],
  912. '<?php echo FOO ? BAR : BAZ;',
  913. ],
  914. 'Bitwise & and bitwise |' => [
  915. [3 => true, 7 => true, 11 => true],
  916. '<?php echo FOO & BAR | BAZ;',
  917. ],
  918. 'Bitwise &' => [
  919. [3 => true],
  920. '<?php echo FOO & $bar;',
  921. ],
  922. [
  923. [5 => true],
  924. '<?php echo $foo[BAR];',
  925. ],
  926. [
  927. [3 => true, 5 => true],
  928. '<?php echo FOO[BAR];',
  929. ],
  930. [
  931. [1 => false, 3 => true, 6 => false, 8 => true],
  932. '<?php func(FOO, Bar\BAZ);',
  933. ],
  934. [
  935. [4 => true, 8 => true],
  936. '<?php if (FOO && BAR) {}',
  937. ],
  938. [
  939. [3 => true, 7 => false, 9 => false, 11 => true],
  940. '<?php return FOO * X\Y\BAR;',
  941. ],
  942. [
  943. [3 => false, 11 => true, 16 => true, 20 => true],
  944. '<?php function x() { yield FOO; yield FOO => BAR; }',
  945. ],
  946. [
  947. [11 => true],
  948. '<?php switch ($a) { case FOO: break; }',
  949. ],
  950. [
  951. [3 => false],
  952. '<?php namespace FOO;',
  953. ],
  954. [
  955. [3 => false],
  956. '<?php use FOO;',
  957. ],
  958. [
  959. [5 => false, 7 => false, 9 => false],
  960. '<?php use function FOO\BAR\BAZ;',
  961. ],
  962. [
  963. [3 => false, 8 => false],
  964. '<?php namespace X; const FOO = 1;',
  965. ],
  966. [
  967. [3 => false],
  968. '<?php class FOO {}',
  969. ],
  970. [
  971. [3 => false],
  972. '<?php interface FOO {}',
  973. ],
  974. [
  975. [3 => false],
  976. '<?php trait FOO {}',
  977. ],
  978. [
  979. [3 => false, 7 => false],
  980. '<?php class x extends FOO {}',
  981. ],
  982. [
  983. [3 => false, 7 => false],
  984. '<?php class x implements FOO {}',
  985. ],
  986. [
  987. [3 => false, 7 => false, 10 => false, 13 => false],
  988. '<?php class x implements FOO, BAR, BAZ {}',
  989. ],
  990. [
  991. [3 => false, 9 => false],
  992. '<?php class x { const FOO = 1; }',
  993. ],
  994. [
  995. [3 => false, 9 => false],
  996. '<?php class x { use FOO; }',
  997. ],
  998. [
  999. [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
  1000. '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
  1001. ],
  1002. [
  1003. [3 => false, 6 => false, 11 => false, 17 => false],
  1004. '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
  1005. ],
  1006. [
  1007. [1 => false],
  1008. '<?php FOO();',
  1009. ],
  1010. [
  1011. [1 => false, 3 => false],
  1012. '<?php FOO::x();',
  1013. ],
  1014. [
  1015. [1 => false, 3 => false],
  1016. '<?php x::FOO();',
  1017. ],
  1018. [
  1019. [5 => false],
  1020. '<?php $foo instanceof FOO;',
  1021. ],
  1022. [
  1023. [9 => false],
  1024. '<?php try {} catch (FOO $e) {}',
  1025. ],
  1026. [
  1027. [4 => false],
  1028. '<?php "$foo[BAR]";',
  1029. ],
  1030. [
  1031. [5 => true],
  1032. '<?php "{$foo[BAR]}";',
  1033. ],
  1034. [
  1035. [1 => false, 6 => false],
  1036. '<?php FOO: goto FOO;',
  1037. ],
  1038. [
  1039. [1 => false, 3 => true, 7 => true],
  1040. '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
  1041. ],
  1042. [
  1043. [3 => false, 7 => false, 10 => false, 13 => false],
  1044. '<?php interface Foo extends Bar, Baz, Qux {}',
  1045. ],
  1046. [
  1047. [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
  1048. '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
  1049. ],
  1050. [
  1051. [3 => false, 8 => false],
  1052. '<?php function x(): FOO {}',
  1053. ],
  1054. [
  1055. [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
  1056. '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
  1057. ],
  1058. [
  1059. [6 => false, 16 => false, 21 => false],
  1060. '<?php
  1061. abstract class Baz
  1062. {
  1063. abstract public function test(): Foo;
  1064. }
  1065. ',
  1066. ],
  1067. [
  1068. [3 => false, 6 => false],
  1069. '<?php function x(?FOO $foo) {}',
  1070. ],
  1071. [
  1072. [3 => false, 9 => false],
  1073. '<?php function x(): ?FOO {}',
  1074. ],
  1075. [
  1076. [9 => false, 11 => false, 13 => false],
  1077. '<?php try {} catch (FOO|BAR|BAZ $e) {}',
  1078. ],
  1079. [
  1080. [3 => false, 11 => false, 16 => false],
  1081. '<?php interface Foo { public function bar(): Baz; }',
  1082. ],
  1083. [
  1084. [3 => false, 11 => false, 17 => false],
  1085. '<?php interface Foo { public function bar(): \Baz; }',
  1086. ],
  1087. [
  1088. [3 => false, 11 => false, 17 => false],
  1089. '<?php interface Foo { public function bar(): ?Baz; }',
  1090. ],
  1091. [
  1092. [3 => false, 11 => false, 18 => false],
  1093. '<?php interface Foo { public function bar(): ?\Baz; }',
  1094. ],
  1095. ];
  1096. }
  1097. /**
  1098. * @param array<int, bool> $expected
  1099. *
  1100. * @dataProvider provideIsConstantInvocationPhp80Cases
  1101. *
  1102. * @requires PHP 8.0
  1103. */
  1104. public function testIsConstantInvocationPhp80(array $expected, string $source): void
  1105. {
  1106. $this->doIsConstantInvocationTest($expected, $source);
  1107. }
  1108. public static function provideIsConstantInvocationPhp80Cases(): iterable
  1109. {
  1110. yield 'abstract method return alternation' => [
  1111. [6 => false, 16 => false, 21 => false, 23 => false],
  1112. '<?php
  1113. abstract class Baz
  1114. {
  1115. abstract public function test(): Foo|Bar;
  1116. }
  1117. ',
  1118. ];
  1119. yield 'function return alternation' => [
  1120. [3 => false, 8 => false, 10 => false],
  1121. '<?php function test(): Foo|Bar {}',
  1122. ];
  1123. yield 'nullsafe operator' => [
  1124. [3 => false, 5 => false],
  1125. '<?php $a?->b?->c;',
  1126. ];
  1127. yield 'non-capturing catch' => [
  1128. [9 => false],
  1129. '<?php try {} catch (Exception) {}',
  1130. ];
  1131. yield 'non-capturing catch 2' => [
  1132. [10 => false],
  1133. '<?php try {} catch (\Exception) {}',
  1134. ];
  1135. yield 'non-capturing multiple catch' => [
  1136. [9 => false, 13 => false],
  1137. '<?php try {} catch (Foo | Bar) {}',
  1138. ];
  1139. yield 'attribute 1' => [
  1140. [2 => false, 5 => false, 10 => false],
  1141. '<?php #[Foo, Bar] function foo() {}',
  1142. ];
  1143. yield 'attribute 2' => [
  1144. [2 => false, 7 => false, 14 => false],
  1145. '<?php #[Foo(), Bar()] function foo() {}',
  1146. ];
  1147. yield [
  1148. [2 => false, 9 => false],
  1149. '<?php #[Foo()] function foo() {}',
  1150. ];
  1151. yield [
  1152. [3 => false, 10 => false],
  1153. '<?php #[\Foo()] function foo() {}',
  1154. ];
  1155. yield [
  1156. [2 => false, 4 => false, 11 => false],
  1157. '<?php #[A\Foo()] function foo() {}',
  1158. ];
  1159. yield [
  1160. [3 => false, 5 => false, 12 => false],
  1161. '<?php #[\A\Foo()] function foo() {}',
  1162. ];
  1163. yield 'multiple type catch with variable' => [
  1164. [5 => false, 15 => false, 18 => false],
  1165. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  1166. ];
  1167. yield 'multiple type catch without variable 1' => [
  1168. [5 => false, 15 => false, 18 => false],
  1169. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  1170. ];
  1171. yield 'multiple type catch without variable 2' => [
  1172. [5 => false, 15 => false, 17 => false, 19 => false, 21 => false, 24 => false, 27 => false],
  1173. '<?php try { foo(); } catch(\D|Z|A\B|\InvalidArgumentException|\LogicException) {}',
  1174. ];
  1175. yield 'multiple type catch without variable 3' => [
  1176. [5 => false, 14 => false, 16 => false, 19 => false, 22 => false],
  1177. '<?php try { foo(); } catch(A\B|\InvalidArgumentException|\LogicException) {}',
  1178. ];
  1179. yield 'attribute before' => [
  1180. [4 => false, 6 => false, 8 => false, 13 => false, 17 => false, 23 => false, 25 => false],
  1181. '<?php
  1182. use Psr\Log\LoggerInterface;
  1183. function f( #[Target(\'xxx\')] LoggerInterface|null $logger) {}
  1184. ',
  1185. ];
  1186. }
  1187. /**
  1188. * @param array<int, bool> $expected
  1189. *
  1190. * @dataProvider provideIsConstantInvocationPhp81Cases
  1191. *
  1192. * @requires PHP 8.1
  1193. */
  1194. public function testIsConstantInvocationPhp81(array $expected, string $source): void
  1195. {
  1196. $this->doIsConstantInvocationTest($expected, $source);
  1197. }
  1198. public static function provideIsConstantInvocationPhp81Cases(): iterable
  1199. {
  1200. yield [
  1201. [5 => false, 15 => false],
  1202. '<?php
  1203. abstract class Baz
  1204. {
  1205. final public const Y = "i";
  1206. }
  1207. ',
  1208. ];
  1209. yield [
  1210. [4 => false, 12 => false, 23 => false],
  1211. '<?php
  1212. class Test {
  1213. public function __construct(
  1214. public $prop = new Foo,
  1215. ) {}
  1216. }
  1217. ',
  1218. ];
  1219. yield 'intersection' => [
  1220. [3 => false, 9 => false, 11 => false],
  1221. '<?php function foo(): \Foo&Bar {}',
  1222. ];
  1223. yield 'abstract method return intersection' => [
  1224. [6 => false, 16 => false, 21 => false, 23 => false, 25 => false, 27 => false, 29 => false],
  1225. '<?php
  1226. abstract class Baz
  1227. {
  1228. abstract public function foo(): Foo&Bar1&Bar2&Bar3&Bar4;
  1229. }
  1230. ',
  1231. ];
  1232. }
  1233. public function testIsConstantInvocationInvalid(): void
  1234. {
  1235. $this->expectException(\LogicException::class);
  1236. $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
  1237. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  1238. $tokensAnalyzer->isConstantInvocation(0);
  1239. }
  1240. /**
  1241. * @requires PHP 8.0
  1242. */
  1243. public function testIsConstantInvocationForNullSafeObjectOperator(): void
  1244. {
  1245. $tokens = Tokens::fromCode('<?php $a?->b?->c;');
  1246. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1247. foreach ($tokens as $index => $token) {
  1248. if (!$token->isGivenKind(T_STRING)) {
  1249. continue;
  1250. }
  1251. static::assertFalse($tokensAnalyzer->isConstantInvocation($index));
  1252. }
  1253. }
  1254. /**
  1255. * @param array<int, bool> $expected
  1256. *
  1257. * @dataProvider provideIsUnarySuccessorOperatorCases
  1258. */
  1259. public function testIsUnarySuccessorOperator(array $expected, string $source): void
  1260. {
  1261. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1262. foreach ($expected as $index => $isUnary) {
  1263. static::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  1264. if ($isUnary) {
  1265. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1266. static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1267. }
  1268. }
  1269. }
  1270. public static function provideIsUnarySuccessorOperatorCases(): array
  1271. {
  1272. return [
  1273. [
  1274. [2 => true],
  1275. '<?php $a++;',
  1276. ],
  1277. [
  1278. [2 => true],
  1279. '<?php $a--;',
  1280. ],
  1281. [
  1282. [3 => true],
  1283. '<?php $a ++;',
  1284. ],
  1285. [
  1286. [2 => true, 4 => false],
  1287. '<?php $a++ + 1;',
  1288. ],
  1289. [
  1290. [5 => true],
  1291. '<?php ${"a"}++;',
  1292. ],
  1293. [
  1294. [4 => true],
  1295. '<?php $foo->bar++;',
  1296. ],
  1297. [
  1298. [6 => true],
  1299. '<?php $foo->{"bar"}++;',
  1300. ],
  1301. 'array access' => [
  1302. [5 => true],
  1303. '<?php $a["foo"]++;',
  1304. ],
  1305. 'array curly access' => [
  1306. [5 => true],
  1307. '<?php $a{"foo"}++;',
  1308. ],
  1309. ];
  1310. }
  1311. /**
  1312. * @param array<int, bool> $expected
  1313. *
  1314. * @dataProvider provideIsUnaryPredecessorOperatorCases
  1315. */
  1316. public function testIsUnaryPredecessorOperator(array $expected, string $source): void
  1317. {
  1318. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1319. foreach ($expected as $index => $isUnary) {
  1320. static::assertSame($isUnary, $tokensAnalyzer->isUnaryPredecessorOperator($index));
  1321. if ($isUnary) {
  1322. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1323. static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1324. }
  1325. }
  1326. }
  1327. public static function provideIsUnaryPredecessorOperatorCases(): array
  1328. {
  1329. return [
  1330. [
  1331. [1 => true],
  1332. '<?php ++$a;',
  1333. ],
  1334. [
  1335. [1 => true],
  1336. '<?php --$a;',
  1337. ],
  1338. [
  1339. [1 => true],
  1340. '<?php -- $a;',
  1341. ],
  1342. [
  1343. [3 => false, 5 => true],
  1344. '<?php $a + ++$b;',
  1345. ],
  1346. [
  1347. [1 => true, 2 => true],
  1348. '<?php !!$a;',
  1349. ],
  1350. [
  1351. [5 => true],
  1352. '<?php $a = &$b;',
  1353. ],
  1354. [
  1355. [3 => true],
  1356. '<?php function &foo() {}',
  1357. ],
  1358. [
  1359. [1 => true],
  1360. '<?php @foo();',
  1361. ],
  1362. [
  1363. [3 => true, 8 => true],
  1364. '<?php foo(+ $a, -$b);',
  1365. ],
  1366. [
  1367. [5 => true, 11 => true, 17 => true],
  1368. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  1369. ],
  1370. [
  1371. [8 => true],
  1372. '<?php function foo($a, ...$b) {}',
  1373. ],
  1374. [
  1375. [5 => true, 6 => true],
  1376. '<?php function foo(&...$b) {}',
  1377. ],
  1378. [
  1379. [7 => true],
  1380. '<?php function foo(array ...$b) {}',
  1381. ],
  1382. [
  1383. [7 => true],
  1384. '<?php $foo = function(...$a) {};',
  1385. ],
  1386. [
  1387. [10 => true],
  1388. '<?php $foo = function($a, ...$b) {};',
  1389. ],
  1390. ];
  1391. }
  1392. /**
  1393. * @param array<int, bool> $expected
  1394. *
  1395. * @dataProvider provideIsBinaryOperatorCases
  1396. */
  1397. public function testIsBinaryOperator(array $expected, string $source): void
  1398. {
  1399. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1400. foreach ($expected as $index => $isBinary) {
  1401. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1402. if ($isBinary) {
  1403. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1404. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1405. }
  1406. }
  1407. }
  1408. public static function provideIsBinaryOperatorCases(): iterable
  1409. {
  1410. yield from [
  1411. [
  1412. [8 => true],
  1413. '<?php echo $a[1] + 1;',
  1414. ],
  1415. [
  1416. [8 => true],
  1417. '<?php echo $a{1} + 1;',
  1418. ],
  1419. [
  1420. [3 => true],
  1421. '<?php $a .= $b; ?>',
  1422. ],
  1423. [
  1424. [3 => true],
  1425. '<?php $a . \'a\' ?>',
  1426. ],
  1427. [
  1428. [3 => true],
  1429. '<?php $a &+ $b;',
  1430. ],
  1431. [
  1432. [3 => true],
  1433. '<?php $a && $b;',
  1434. ],
  1435. [
  1436. [3 => true],
  1437. '<?php $a & $b;',
  1438. ],
  1439. [
  1440. [4 => true],
  1441. '<?php [] + [];',
  1442. ],
  1443. [
  1444. [3 => true],
  1445. '<?php $a + $b;',
  1446. ],
  1447. [
  1448. [3 => true],
  1449. '<?php 1 + $b;',
  1450. ],
  1451. [
  1452. [3 => true],
  1453. '<?php 0.2 + $b;',
  1454. ],
  1455. [
  1456. [6 => true],
  1457. '<?php $a[1] + $b;',
  1458. ],
  1459. [
  1460. [3 => true],
  1461. '<?php FOO + $b;',
  1462. ],
  1463. [
  1464. [5 => true],
  1465. '<?php foo() + $b;',
  1466. ],
  1467. [
  1468. [6 => true],
  1469. '<?php ${"foo"} + $b;',
  1470. ],
  1471. [
  1472. [2 => true],
  1473. '<?php $a+$b;',
  1474. ],
  1475. [
  1476. [5 => true],
  1477. '<?php $a /* foo */ + /* bar */ $b;',
  1478. ],
  1479. [
  1480. [3 => true],
  1481. '<?php $a =
  1482. $b;',
  1483. ],
  1484. [
  1485. [3 => true],
  1486. '<?php $a
  1487. = $b;',
  1488. ],
  1489. [
  1490. [3 => true, 9 => true, 12 => false],
  1491. '<?php $a = array("b" => "c", );',
  1492. ],
  1493. [
  1494. [3 => true, 5 => false],
  1495. '<?php $a * -$b;',
  1496. ],
  1497. [
  1498. [3 => true, 5 => false, 8 => true, 10 => false],
  1499. '<?php $a = -2 / +5;',
  1500. ],
  1501. [
  1502. [3 => true, 5 => false],
  1503. '<?php $a = &$b;',
  1504. ],
  1505. [
  1506. [2 => false, 4 => true],
  1507. '<?php $a++ + $b;',
  1508. ],
  1509. [
  1510. [7 => true],
  1511. '<?php $a = FOO & $bar;',
  1512. ],
  1513. [
  1514. [3 => true],
  1515. '<?php __LINE__ - 1;',
  1516. ],
  1517. [
  1518. [5 => true],
  1519. '<?php `echo 1` + 1;',
  1520. ],
  1521. [
  1522. [3 => true],
  1523. '<?php $a ** $b;',
  1524. ],
  1525. [
  1526. [3 => true],
  1527. '<?php $a **= $b;',
  1528. ],
  1529. [
  1530. [9 => false],
  1531. '<?php $a = "{$value}-{$theSwitch}";',
  1532. ],
  1533. [
  1534. [3 => false],
  1535. '<?=$path?>-<?=$id?>',
  1536. ],
  1537. ];
  1538. $operators = [
  1539. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  1540. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
  1541. ];
  1542. foreach ($operators as $operator) {
  1543. yield [
  1544. [3 => true],
  1545. '<?php $a '.$operator.' $b;',
  1546. ];
  1547. }
  1548. yield [
  1549. [3 => true],
  1550. '<?php $a <=> $b;',
  1551. ];
  1552. yield [
  1553. [3 => true],
  1554. '<?php $a ?? $b;',
  1555. ];
  1556. }
  1557. /**
  1558. * @dataProvider provideIsArrayCases
  1559. */
  1560. public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
  1561. {
  1562. $tokens = Tokens::fromCode($source);
  1563. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1564. static::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  1565. static::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  1566. }
  1567. public static function provideIsArrayCases(): array
  1568. {
  1569. return [
  1570. [
  1571. '<?php
  1572. array("a" => 1);
  1573. ',
  1574. 2,
  1575. ],
  1576. [
  1577. '<?php
  1578. ["a" => 2];
  1579. ',
  1580. 2, false,
  1581. ],
  1582. [
  1583. '<?php
  1584. array(
  1585. "a" => 3
  1586. );
  1587. ',
  1588. 2, true,
  1589. ],
  1590. [
  1591. '<?php
  1592. [
  1593. "a" => 4
  1594. ];
  1595. ',
  1596. 2, true,
  1597. ],
  1598. [
  1599. '<?php
  1600. array(
  1601. "a" => array(5, 6, 7),
  1602. 8 => new \Exception(\'Hello\')
  1603. );
  1604. ',
  1605. 2, true,
  1606. ],
  1607. [
  1608. // mix short array syntax
  1609. '<?php
  1610. array(
  1611. "a" => [9, 10, 11],
  1612. 12 => new \Exception(\'Hello\')
  1613. );
  1614. ',
  1615. 2, true,
  1616. ],
  1617. // Windows/Max EOL testing
  1618. [
  1619. "<?php\r\narray('a' => 13);\r\n",
  1620. 1,
  1621. ],
  1622. [
  1623. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  1624. 2, true,
  1625. ],
  1626. ];
  1627. }
  1628. /**
  1629. * @param list<int> $tokenIndexes
  1630. *
  1631. * @dataProvider provideIsArray71Cases
  1632. */
  1633. public function testIsArray71(string $source, array $tokenIndexes): void
  1634. {
  1635. $tokens = Tokens::fromCode($source);
  1636. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1637. foreach ($tokens as $index => $token) {
  1638. $expect = \in_array($index, $tokenIndexes, true);
  1639. static::assertSame(
  1640. $expect,
  1641. $tokensAnalyzer->isArray($index),
  1642. sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1643. );
  1644. }
  1645. }
  1646. public static function provideIsArray71Cases(): array
  1647. {
  1648. return [
  1649. [
  1650. '<?php
  1651. [$a] = $z;
  1652. ["a" => $a, "b" => $b] = $array;
  1653. $c = [$d, $e] = $array[$a];
  1654. [[$a, $b], [$c, $d]] = $d;
  1655. $array = []; $d = array();
  1656. ',
  1657. [76, 84],
  1658. ],
  1659. ];
  1660. }
  1661. /**
  1662. * @param array<int, bool> $expected
  1663. *
  1664. * @dataProvider provideIsBinaryOperator71Cases
  1665. */
  1666. public function testIsBinaryOperator71(array $expected, string $source): void
  1667. {
  1668. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1669. foreach ($expected as $index => $isBinary) {
  1670. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1671. if ($isBinary) {
  1672. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1673. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1674. }
  1675. }
  1676. }
  1677. public static function provideIsBinaryOperator71Cases(): iterable
  1678. {
  1679. yield [
  1680. [11 => false],
  1681. '<?php try {} catch (A | B $e) {}',
  1682. ];
  1683. yield [
  1684. [3 => true],
  1685. '<?php $a ??= $b;',
  1686. ];
  1687. }
  1688. /**
  1689. * @param array<int, bool> $expected
  1690. *
  1691. * @dataProvider provideIsBinaryOperator80Cases
  1692. *
  1693. * @requires PHP 8.0
  1694. */
  1695. public function testIsBinaryOperator80(array $expected, string $source): void
  1696. {
  1697. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1698. foreach ($expected as $index => $isBinary) {
  1699. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1700. if ($isBinary) {
  1701. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1702. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1703. }
  1704. }
  1705. }
  1706. public static function provideIsBinaryOperator80Cases(): iterable
  1707. {
  1708. yield [
  1709. [6 => false],
  1710. '<?php function foo(array|string $x) {}',
  1711. ];
  1712. yield [
  1713. [6 => false],
  1714. '<?php function foo(string|array $x) {}',
  1715. ];
  1716. yield [
  1717. [6 => false],
  1718. '<?php function foo(int|callable $x) {}',
  1719. ];
  1720. yield [
  1721. [6 => false],
  1722. '<?php function foo(callable|int $x) {}',
  1723. ];
  1724. }
  1725. /**
  1726. * @param array<int, bool> $expected
  1727. *
  1728. * @dataProvider provideIsBinaryOperator81Cases
  1729. *
  1730. * @requires PHP 8.1
  1731. */
  1732. public function testIsBinaryOperator81(array $expected, string $source): void
  1733. {
  1734. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1735. foreach ($expected as $index => $isBinary) {
  1736. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1737. if ($isBinary) {
  1738. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1739. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1740. }
  1741. }
  1742. }
  1743. public static function provideIsBinaryOperator81Cases(): iterable
  1744. {
  1745. yield 'type intersection' => [
  1746. [6 => false],
  1747. '<?php function foo(array&string $x) {}',
  1748. ];
  1749. }
  1750. /**
  1751. * @dataProvider provideArrayExceptionsCases
  1752. */
  1753. public function testIsNotArray(string $source, int $tokenIndex): void
  1754. {
  1755. $tokens = Tokens::fromCode($source);
  1756. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1757. static::assertFalse($tokensAnalyzer->isArray($tokenIndex));
  1758. }
  1759. /**
  1760. * @dataProvider provideArrayExceptionsCases
  1761. */
  1762. public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
  1763. {
  1764. $this->expectException(\InvalidArgumentException::class);
  1765. $tokens = Tokens::fromCode($source);
  1766. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1767. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  1768. }
  1769. public static function provideArrayExceptionsCases(): array
  1770. {
  1771. return [
  1772. ['<?php $a;', 1],
  1773. ["<?php\n \$a = (0+1); // [0,1]", 4],
  1774. ['<?php $text = "foo $bbb[0] bar";', 8],
  1775. ['<?php $text = "foo ${aaa[123]} bar";', 9],
  1776. ];
  1777. }
  1778. public function testIsBlockMultilineException(): void
  1779. {
  1780. $this->expectException(\LogicException::class);
  1781. $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
  1782. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1783. $tokensAnalyzer->isBlockMultiline($tokens, 1);
  1784. }
  1785. /**
  1786. * @dataProvider provideIsBlockMultilineCases
  1787. */
  1788. public function testIsBlockMultiline(bool $isBlockMultiline, string $source, int $tokenIndex): void
  1789. {
  1790. $tokens = Tokens::fromCode($source);
  1791. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1792. static::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
  1793. }
  1794. public static function provideIsBlockMultilineCases(): iterable
  1795. {
  1796. yield [
  1797. false,
  1798. '<?php foo(1, 2, 3);',
  1799. 2,
  1800. ];
  1801. yield [
  1802. true,
  1803. '<?php foo(1,
  1804. 2,
  1805. 3
  1806. );',
  1807. 2,
  1808. ];
  1809. yield [
  1810. false,
  1811. '<?php foo(1, "Multi
  1812. string", 2, 3);',
  1813. 2,
  1814. ];
  1815. yield [
  1816. false,
  1817. '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
  1818. "a",
  1819. "b"
  1820. ), 2, 3);',
  1821. 2,
  1822. ];
  1823. }
  1824. /**
  1825. * @param array{visibility: ?int, static: bool, abstract: bool, final: bool} $expected
  1826. *
  1827. * @dataProvider provideGetFunctionPropertiesCases
  1828. */
  1829. public function testGetFunctionProperties(string $source, int $index, array $expected): void
  1830. {
  1831. $tokens = Tokens::fromCode($source);
  1832. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1833. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  1834. static::assertSame($expected, $attributes);
  1835. }
  1836. public static function provideGetFunctionPropertiesCases(): array
  1837. {
  1838. $defaultAttributes = [
  1839. 'visibility' => null,
  1840. 'static' => false,
  1841. 'abstract' => false,
  1842. 'final' => false,
  1843. ];
  1844. $template = '
  1845. <?php
  1846. class TestClass {
  1847. %s function a() {
  1848. //
  1849. }
  1850. }
  1851. ';
  1852. $cases = [];
  1853. $attributes = $defaultAttributes;
  1854. $attributes['visibility'] = T_PRIVATE;
  1855. $cases[] = [sprintf($template, 'private'), 10, $attributes];
  1856. $attributes = $defaultAttributes;
  1857. $attributes['visibility'] = T_PUBLIC;
  1858. $cases[] = [sprintf($template, 'public'), 10, $attributes];
  1859. $attributes = $defaultAttributes;
  1860. $attributes['visibility'] = T_PROTECTED;
  1861. $cases[] = [sprintf($template, 'protected'), 10, $attributes];
  1862. $attributes = $defaultAttributes;
  1863. $attributes['visibility'] = null;
  1864. $attributes['static'] = true;
  1865. $cases[] = [sprintf($template, 'static'), 10, $attributes];
  1866. $attributes = $defaultAttributes;
  1867. $attributes['visibility'] = T_PUBLIC;
  1868. $attributes['static'] = true;
  1869. $attributes['final'] = true;
  1870. $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
  1871. $attributes = $defaultAttributes;
  1872. $attributes['visibility'] = null;
  1873. $attributes['abstract'] = true;
  1874. $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
  1875. $attributes = $defaultAttributes;
  1876. $attributes['visibility'] = T_PUBLIC;
  1877. $attributes['abstract'] = true;
  1878. $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
  1879. $attributes = $defaultAttributes;
  1880. $cases[] = [sprintf($template, ''), 8, $attributes];
  1881. return $cases;
  1882. }
  1883. public function testIsWhilePartOfDoWhile(): void
  1884. {
  1885. $source =
  1886. <<<'SRC'
  1887. <?php
  1888. // `not do`
  1889. while(false) {
  1890. }
  1891. while (false);
  1892. while (false)?>
  1893. <?php
  1894. if(false){
  1895. }while(false);
  1896. if(false){
  1897. }while(false)?><?php
  1898. while(false){}while(false){}
  1899. while ($i <= 10):
  1900. echo $i;
  1901. $i++;
  1902. endwhile;
  1903. ?>
  1904. <?php while(false): ?>
  1905. <?php endwhile ?>
  1906. <?php
  1907. // `do`
  1908. do{
  1909. } while(false);
  1910. do{
  1911. } while(false)?>
  1912. <?php
  1913. if (false){}do{}while(false);
  1914. // `not do`, `do`
  1915. if(false){}while(false){}do{}while(false);
  1916. SRC;
  1917. $expected = [
  1918. 3 => false,
  1919. 12 => false,
  1920. 19 => false,
  1921. 34 => false,
  1922. 47 => false,
  1923. 53 => false,
  1924. 59 => false,
  1925. 66 => false,
  1926. 91 => false,
  1927. 112 => true,
  1928. 123 => true,
  1929. 139 => true,
  1930. 153 => false,
  1931. 162 => true,
  1932. ];
  1933. $tokens = Tokens::fromCode($source);
  1934. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1935. foreach ($tokens as $index => $token) {
  1936. if (!$token->isGivenKind(T_WHILE)) {
  1937. continue;
  1938. }
  1939. static::assertSame(
  1940. $expected[$index],
  1941. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  1942. sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not ')
  1943. );
  1944. }
  1945. }
  1946. /**
  1947. * @param array<int, list<int>>|list<int> $expected
  1948. *
  1949. * @dataProvider provideGetImportUseIndexesCases
  1950. */
  1951. public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
  1952. {
  1953. $tokens = Tokens::fromCode($input);
  1954. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1955. static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1956. }
  1957. public static function provideGetImportUseIndexesCases(): array
  1958. {
  1959. return [
  1960. [
  1961. [1, 8],
  1962. '<?php use E\F?><?php use A\B;',
  1963. ],
  1964. [
  1965. [[1], [14], [29]],
  1966. '<?php
  1967. use T\A;
  1968. namespace A { use D\C; }
  1969. namespace b { use D\C; }
  1970. ',
  1971. true,
  1972. ],
  1973. [
  1974. [[1, 8]],
  1975. '<?php use D\B; use A\C?>',
  1976. true,
  1977. ],
  1978. [
  1979. [1, 8],
  1980. '<?php use D\B; use A\C?>',
  1981. ],
  1982. [
  1983. [7, 22],
  1984. '<?php
  1985. namespace A { use D\C; }
  1986. namespace b { use D\C; }
  1987. ',
  1988. ],
  1989. [
  1990. [3, 10, 34, 45, 54, 59, 77, 95],
  1991. <<<'EOF'
  1992. use Zoo\Bar;
  1993. use Foo\Bar;
  1994. use Foo\Zar\Baz;
  1995. <?php
  1996. use Foo\Bar;
  1997. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  1998. use Foo\Bir as FBB;
  1999. use Foo\Zar\Baz;
  2000. use SomeClass;
  2001. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  2002. use Zoo\Bar;
  2003. $a = new someclass();
  2004. use Zoo\Tar;
  2005. class AnnotatedClass
  2006. {
  2007. }
  2008. EOF
  2009. ,
  2010. ],
  2011. [
  2012. [1, 22, 41],
  2013. '<?php
  2014. use some\a\{ClassA, ClassB, ClassC as C};
  2015. use function some\a\{fn_a, fn_b, fn_c};
  2016. use const some\a\{ConstA, ConstB, ConstC};
  2017. ',
  2018. ],
  2019. [
  2020. [[1, 22, 41]],
  2021. '<?php
  2022. use some\a\{ClassA, ClassB, ClassC as C};
  2023. use function some\a\{fn_a, fn_b, fn_c};
  2024. use const some\a\{ConstA, ConstB, ConstC};
  2025. ',
  2026. true,
  2027. ],
  2028. [
  2029. [1, 23, 43],
  2030. '<?php
  2031. use some\a\{ClassA, ClassB, ClassC as C,};
  2032. use function some\a\{fn_a, fn_b, fn_c,};
  2033. use const some\a\{ConstA, ConstB, ConstC,};
  2034. ',
  2035. ],
  2036. [
  2037. [[1, 23, 43]],
  2038. '<?php
  2039. use some\a\{ClassA, ClassB, ClassC as C,};
  2040. use function some\a\{fn_a, fn_b, fn_c,};
  2041. use const some\a\{ConstA, ConstB, ConstC,};
  2042. ',
  2043. true,
  2044. ],
  2045. ];
  2046. }
  2047. public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
  2048. {
  2049. $source = '<?php
  2050. class MyTestWithAnonymousClass extends TestCase
  2051. {
  2052. public function setUp()
  2053. {
  2054. $provider = new class(function () {}) {};
  2055. }
  2056. public function testSomethingWithMoney(
  2057. Money $amount
  2058. ) {
  2059. $a = new class(function () {
  2060. new class(function () {
  2061. new class(function () {})
  2062. {
  2063. const A=1;
  2064. };
  2065. })
  2066. {
  2067. const B=1;
  2068. public function foo() {
  2069. $c = new class() {const AA=3;};
  2070. $d = new class {const AB=3;};
  2071. }
  2072. };
  2073. })
  2074. {
  2075. const C=1;
  2076. };
  2077. }
  2078. }';
  2079. $tokens = Tokens::fromCode($source);
  2080. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2081. $elements = $tokensAnalyzer->getClassyElements();
  2082. static::assertSame([
  2083. 13 => [
  2084. 'classIndex' => 1,
  2085. 'token' => $tokens[13],
  2086. 'type' => 'method', // setUp
  2087. ],
  2088. 46 => [
  2089. 'classIndex' => 1,
  2090. 'token' => $tokens[46],
  2091. 'type' => 'method', // testSomethingWithMoney
  2092. ],
  2093. 100 => [
  2094. 'classIndex' => 87,
  2095. 'token' => $tokens[100], // const A
  2096. 'type' => 'const',
  2097. ],
  2098. 115 => [
  2099. 'classIndex' => 65,
  2100. 'token' => $tokens[115], // const B
  2101. 'type' => 'const',
  2102. ],
  2103. 124 => [
  2104. 'classIndex' => 65, // $a
  2105. 'token' => $tokens[124],
  2106. 'type' => 'method', // foo
  2107. ],
  2108. 143 => [
  2109. 'classIndex' => 138,
  2110. 'token' => $tokens[143], // const AA
  2111. 'type' => 'const',
  2112. ],
  2113. 161 => [
  2114. 'classIndex' => 158,
  2115. 'token' => $tokens[161], // const AB
  2116. 'type' => 'const',
  2117. ],
  2118. ], $elements);
  2119. }
  2120. /**
  2121. * @dataProvider provideIsSuperGlobalCases
  2122. */
  2123. public function testIsSuperGlobal(bool $expected, string $source, int $index): void
  2124. {
  2125. $tokens = Tokens::fromCode($source);
  2126. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2127. static::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
  2128. }
  2129. public static function provideIsSuperGlobalCases(): array
  2130. {
  2131. $superNames = [
  2132. '$_COOKIE',
  2133. '$_ENV',
  2134. '$_FILES',
  2135. '$_GET',
  2136. '$_POST',
  2137. '$_REQUEST',
  2138. '$_SERVER',
  2139. '$_SESSION',
  2140. '$GLOBALS',
  2141. ];
  2142. $cases = [];
  2143. foreach ($superNames as $superName) {
  2144. $cases[] = [
  2145. true,
  2146. sprintf('<?php echo %s[0];', $superName),
  2147. 3,
  2148. ];
  2149. }
  2150. $notGlobalCodeCases = [
  2151. '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
  2152. '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
  2153. ];
  2154. foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
  2155. $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
  2156. for ($i = 0; $i < $tokensCount; ++$i) {
  2157. $cases[] = [
  2158. false,
  2159. $notGlobalCodeCase,
  2160. $i,
  2161. ];
  2162. }
  2163. }
  2164. return $cases;
  2165. }
  2166. /**
  2167. * @param array<int, bool> $expected
  2168. */
  2169. private function doIsConstantInvocationTest(array $expected, string $source): void
  2170. {
  2171. $tokens = Tokens::fromCode($source);
  2172. static::assertCount(
  2173. $tokens->countTokenKind(T_STRING),
  2174. $expected,
  2175. 'All T_STRING tokens must be tested'
  2176. );
  2177. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2178. foreach ($expected as $index => $expectedValue) {
  2179. static::assertSame(
  2180. $expectedValue,
  2181. $tokensAnalyzer->isConstantInvocation($index),
  2182. sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
  2183. );
  2184. }
  2185. }
  2186. }