TokensAnalyzerTest.php 60 KB

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