TokensAnalyzerTest.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828
  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. self::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. self::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. self::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. self::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. self::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. self::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. self::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. self::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. if (\PHP_VERSION_ID >= 8_03_00) {
  712. yield 'simple readonly anonymous class' => [
  713. [9 => true],
  714. '<?php $instance = new readonly class {};',
  715. ];
  716. yield 'readonly anonymous class with attribute' => [
  717. [13 => true],
  718. '<?php $instance = new #[Foo] readonly class {};',
  719. ];
  720. yield 'readonly anonymous class with multiple attributes' => [
  721. [17 => true],
  722. '<?php $instance = new #[Foo] #[BAR] readonly class {};',
  723. ];
  724. }
  725. }
  726. /**
  727. * @param array<int, bool> $expected
  728. *
  729. * @dataProvider provideIsLambdaCases
  730. */
  731. public function testIsLambda(array $expected, string $source): void
  732. {
  733. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  734. foreach ($expected as $index => $isLambda) {
  735. self::assertSame($isLambda, $tokensAnalyzer->isLambda($index));
  736. }
  737. }
  738. public static function provideIsLambdaCases(): iterable
  739. {
  740. yield [
  741. [1 => false],
  742. '<?php function foo () {};',
  743. ];
  744. yield [
  745. [1 => false],
  746. '<?php function /** foo */ foo () {};',
  747. ];
  748. yield [
  749. [5 => true],
  750. '<?php $foo = function () {};',
  751. ];
  752. yield [
  753. [5 => true],
  754. '<?php $foo = function /** foo */ () {};',
  755. ];
  756. yield [
  757. [7 => true],
  758. '<?php
  759. preg_replace_callback(
  760. "/(^|[a-z])/",
  761. function (array $matches) {
  762. return "a";
  763. },
  764. $string
  765. );',
  766. ];
  767. yield [
  768. [5 => true],
  769. '<?php $foo = function &() {};',
  770. ];
  771. yield [
  772. [6 => true],
  773. '<?php
  774. $a = function (): array {
  775. return [];
  776. };',
  777. ];
  778. yield [
  779. [2 => false],
  780. '<?php
  781. function foo (): array {
  782. return [];
  783. };',
  784. ];
  785. yield [
  786. [6 => true],
  787. '<?php
  788. $a = function (): void {
  789. return [];
  790. };',
  791. ];
  792. yield [
  793. [2 => false],
  794. '<?php
  795. function foo (): void {
  796. return [];
  797. };',
  798. ];
  799. yield [
  800. [6 => true],
  801. '<?php
  802. $a = function (): ?int {
  803. return [];
  804. };',
  805. ];
  806. yield [
  807. [6 => true],
  808. '<?php
  809. $a = function (): int {
  810. return [];
  811. };',
  812. ];
  813. yield [
  814. [2 => false],
  815. '<?php
  816. function foo (): ?int {
  817. return [];
  818. };',
  819. ];
  820. }
  821. /**
  822. * @param array<int, bool> $expected
  823. *
  824. * @dataProvider provideIsLambda74Cases
  825. */
  826. public function testIsLambda74(array $expected, string $source): void
  827. {
  828. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  829. foreach ($expected as $index => $expectedValue) {
  830. self::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  831. }
  832. }
  833. public static function provideIsLambda74Cases(): iterable
  834. {
  835. yield [
  836. [5 => true],
  837. '<?php $fn = fn() => [];',
  838. ];
  839. yield [
  840. [5 => true],
  841. '<?php $fn = fn () => [];',
  842. ];
  843. }
  844. /**
  845. * @param array<int, bool> $expected
  846. *
  847. * @dataProvider provideIsLambda80Cases
  848. *
  849. * @requires PHP 8.0
  850. */
  851. public function testIsLambda80(array $expected, string $source): void
  852. {
  853. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  854. foreach ($expected as $index => $expectedValue) {
  855. self::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  856. }
  857. }
  858. public static function provideIsLambda80Cases(): iterable
  859. {
  860. yield [
  861. [6 => true],
  862. '<?php
  863. $a = function (): ?static {
  864. return [];
  865. };',
  866. ];
  867. yield [
  868. [6 => true],
  869. '<?php
  870. $a = function (): static {
  871. return [];
  872. };',
  873. ];
  874. yield [
  875. [14 => true],
  876. '<?php
  877. $c = 4; //
  878. $a = function(
  879. $a,
  880. $b,
  881. ) use (
  882. $c,
  883. ) {
  884. echo $a + $b + $c;
  885. };
  886. $a(1,2);',
  887. ];
  888. }
  889. public function testIsLambdaInvalid(): void
  890. {
  891. $this->expectException(\LogicException::class);
  892. $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
  893. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  894. $tokensAnalyzer->isLambda(0);
  895. }
  896. /**
  897. * @param array<int, bool> $expected
  898. *
  899. * @dataProvider provideIsConstantInvocationCases
  900. */
  901. public function testIsConstantInvocation(array $expected, string $source): void
  902. {
  903. $this->doIsConstantInvocationTest($expected, $source);
  904. }
  905. public static function provideIsConstantInvocationCases(): iterable
  906. {
  907. yield [
  908. [3 => true],
  909. '<?php echo FOO;',
  910. ];
  911. yield [
  912. [4 => true],
  913. '<?php echo \FOO;',
  914. ];
  915. yield [
  916. [3 => false, 5 => false, 7 => true],
  917. '<?php echo Foo\Bar\BAR;',
  918. ];
  919. yield [
  920. [3 => true, 7 => true, 11 => true],
  921. '<?php echo FOO ? BAR : BAZ;',
  922. ];
  923. yield 'Bitwise & and bitwise |' => [
  924. [3 => true, 7 => true, 11 => true],
  925. '<?php echo FOO & BAR | BAZ;',
  926. ];
  927. yield 'Bitwise &' => [
  928. [3 => true],
  929. '<?php echo FOO & $bar;',
  930. ];
  931. yield [
  932. [5 => true],
  933. '<?php echo $foo[BAR];',
  934. ];
  935. yield [
  936. [3 => true, 5 => true],
  937. '<?php echo FOO[BAR];',
  938. ];
  939. yield [
  940. [1 => false, 3 => true, 6 => false, 8 => true],
  941. '<?php func(FOO, Bar\BAZ);',
  942. ];
  943. yield [
  944. [4 => true, 8 => true],
  945. '<?php if (FOO && BAR) {}',
  946. ];
  947. yield [
  948. [3 => true, 7 => false, 9 => false, 11 => true],
  949. '<?php return FOO * X\Y\BAR;',
  950. ];
  951. yield [
  952. [3 => false, 11 => true, 16 => true, 20 => true],
  953. '<?php function x() { yield FOO; yield FOO => BAR; }',
  954. ];
  955. yield [
  956. [11 => true],
  957. '<?php switch ($a) { case FOO: break; }',
  958. ];
  959. yield [
  960. [3 => false],
  961. '<?php namespace FOO;',
  962. ];
  963. yield [
  964. [3 => false],
  965. '<?php use FOO;',
  966. ];
  967. yield [
  968. [5 => false, 7 => false, 9 => false],
  969. '<?php use function FOO\BAR\BAZ;',
  970. ];
  971. yield [
  972. [3 => false, 8 => false],
  973. '<?php namespace X; const FOO = 1;',
  974. ];
  975. yield [
  976. [3 => false],
  977. '<?php class FOO {}',
  978. ];
  979. yield [
  980. [3 => false],
  981. '<?php interface FOO {}',
  982. ];
  983. yield [
  984. [3 => false],
  985. '<?php trait FOO {}',
  986. ];
  987. yield [
  988. [3 => false, 7 => false],
  989. '<?php class x extends FOO {}',
  990. ];
  991. yield [
  992. [3 => false, 7 => false],
  993. '<?php class x implements FOO {}',
  994. ];
  995. yield [
  996. [3 => false, 7 => false, 10 => false, 13 => false],
  997. '<?php class x implements FOO, BAR, BAZ {}',
  998. ];
  999. yield [
  1000. [3 => false, 9 => false],
  1001. '<?php class x { const FOO = 1; }',
  1002. ];
  1003. yield [
  1004. [3 => false, 9 => false],
  1005. '<?php class x { use FOO; }',
  1006. ];
  1007. yield [
  1008. [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
  1009. '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
  1010. ];
  1011. yield [
  1012. [3 => false, 6 => false, 11 => false, 17 => false],
  1013. '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
  1014. ];
  1015. yield [
  1016. [1 => false],
  1017. '<?php FOO();',
  1018. ];
  1019. yield [
  1020. [1 => false, 3 => false],
  1021. '<?php FOO::x();',
  1022. ];
  1023. yield [
  1024. [1 => false, 3 => false],
  1025. '<?php x::FOO();',
  1026. ];
  1027. yield [
  1028. [5 => false],
  1029. '<?php $foo instanceof FOO;',
  1030. ];
  1031. yield [
  1032. [9 => false],
  1033. '<?php try {} catch (FOO $e) {}',
  1034. ];
  1035. yield [
  1036. [4 => false],
  1037. '<?php "$foo[BAR]";',
  1038. ];
  1039. yield [
  1040. [5 => true],
  1041. '<?php "{$foo[BAR]}";',
  1042. ];
  1043. yield [
  1044. [1 => false, 6 => false],
  1045. '<?php FOO: goto FOO;',
  1046. ];
  1047. yield [
  1048. [1 => false, 3 => true, 7 => true],
  1049. '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
  1050. ];
  1051. yield [
  1052. [3 => false, 7 => false, 10 => false, 13 => false],
  1053. '<?php interface Foo extends Bar, Baz, Qux {}',
  1054. ];
  1055. yield [
  1056. [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
  1057. '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
  1058. ];
  1059. yield [
  1060. [3 => false, 8 => false],
  1061. '<?php function x(): FOO {}',
  1062. ];
  1063. yield [
  1064. [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
  1065. '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
  1066. ];
  1067. yield [
  1068. [6 => false, 16 => false, 21 => false],
  1069. '<?php
  1070. abstract class Baz
  1071. {
  1072. abstract public function test(): Foo;
  1073. }
  1074. ',
  1075. ];
  1076. yield [
  1077. [3 => false, 6 => false],
  1078. '<?php function x(?FOO $foo) {}',
  1079. ];
  1080. yield [
  1081. [3 => false, 9 => false],
  1082. '<?php function x(): ?FOO {}',
  1083. ];
  1084. yield [
  1085. [9 => false, 11 => false, 13 => false],
  1086. '<?php try {} catch (FOO|BAR|BAZ $e) {}',
  1087. ];
  1088. yield [
  1089. [3 => false, 11 => false, 16 => false],
  1090. '<?php interface Foo { public function bar(): Baz; }',
  1091. ];
  1092. yield [
  1093. [3 => false, 11 => false, 17 => false],
  1094. '<?php interface Foo { public function bar(): \Baz; }',
  1095. ];
  1096. yield [
  1097. [3 => false, 11 => false, 17 => false],
  1098. '<?php interface Foo { public function bar(): ?Baz; }',
  1099. ];
  1100. yield [
  1101. [3 => false, 11 => false, 18 => false],
  1102. '<?php interface Foo { public function bar(): ?\Baz; }',
  1103. ];
  1104. }
  1105. /**
  1106. * @param array<int, bool> $expected
  1107. *
  1108. * @dataProvider provideIsConstantInvocationPhp80Cases
  1109. *
  1110. * @requires PHP 8.0
  1111. */
  1112. public function testIsConstantInvocationPhp80(array $expected, string $source): void
  1113. {
  1114. $this->doIsConstantInvocationTest($expected, $source);
  1115. }
  1116. public static function provideIsConstantInvocationPhp80Cases(): iterable
  1117. {
  1118. yield 'abstract method return alternation' => [
  1119. [6 => false, 16 => false, 21 => false, 23 => false],
  1120. '<?php
  1121. abstract class Baz
  1122. {
  1123. abstract public function test(): Foo|Bar;
  1124. }
  1125. ',
  1126. ];
  1127. yield 'function return alternation' => [
  1128. [3 => false, 8 => false, 10 => false],
  1129. '<?php function test(): Foo|Bar {}',
  1130. ];
  1131. yield 'nullsafe operator' => [
  1132. [3 => false, 5 => false],
  1133. '<?php $a?->b?->c;',
  1134. ];
  1135. yield 'non-capturing catch' => [
  1136. [9 => false],
  1137. '<?php try {} catch (Exception) {}',
  1138. ];
  1139. yield 'non-capturing catch 2' => [
  1140. [10 => false],
  1141. '<?php try {} catch (\Exception) {}',
  1142. ];
  1143. yield 'non-capturing multiple catch' => [
  1144. [9 => false, 13 => false],
  1145. '<?php try {} catch (Foo | Bar) {}',
  1146. ];
  1147. yield 'attribute 1' => [
  1148. [2 => false, 5 => false, 10 => false],
  1149. '<?php #[Foo, Bar] function foo() {}',
  1150. ];
  1151. yield 'attribute 2' => [
  1152. [2 => false, 7 => false, 14 => false],
  1153. '<?php #[Foo(), Bar()] function foo() {}',
  1154. ];
  1155. yield [
  1156. [2 => false, 9 => false],
  1157. '<?php #[Foo()] function foo() {}',
  1158. ];
  1159. yield [
  1160. [3 => false, 10 => false],
  1161. '<?php #[\Foo()] function foo() {}',
  1162. ];
  1163. yield [
  1164. [2 => false, 4 => false, 11 => false],
  1165. '<?php #[A\Foo()] function foo() {}',
  1166. ];
  1167. yield [
  1168. [3 => false, 5 => false, 12 => false],
  1169. '<?php #[\A\Foo()] function foo() {}',
  1170. ];
  1171. yield 'multiple type catch with variable' => [
  1172. [5 => false, 15 => false, 18 => false],
  1173. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  1174. ];
  1175. yield 'multiple type catch without variable 1' => [
  1176. [5 => false, 15 => false, 18 => false],
  1177. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  1178. ];
  1179. yield 'multiple type catch without variable 2' => [
  1180. [5 => false, 15 => false, 17 => false, 19 => false, 21 => false, 24 => false, 27 => false],
  1181. '<?php try { foo(); } catch(\D|Z|A\B|\InvalidArgumentException|\LogicException) {}',
  1182. ];
  1183. yield 'multiple type catch without variable 3' => [
  1184. [5 => false, 14 => false, 16 => false, 19 => false, 22 => false],
  1185. '<?php try { foo(); } catch(A\B|\InvalidArgumentException|\LogicException) {}',
  1186. ];
  1187. yield 'attribute before' => [
  1188. [4 => false, 6 => false, 8 => false, 13 => false, 17 => false, 23 => false, 25 => false],
  1189. '<?php
  1190. use Psr\Log\LoggerInterface;
  1191. function f( #[Target(\'xxx\')] LoggerInterface|null $logger) {}
  1192. ',
  1193. ];
  1194. }
  1195. /**
  1196. * @param array<int, bool> $expected
  1197. *
  1198. * @dataProvider provideIsConstantInvocationPhp81Cases
  1199. *
  1200. * @requires PHP 8.1
  1201. */
  1202. public function testIsConstantInvocationPhp81(array $expected, string $source): void
  1203. {
  1204. $this->doIsConstantInvocationTest($expected, $source);
  1205. }
  1206. public static function provideIsConstantInvocationPhp81Cases(): iterable
  1207. {
  1208. yield [
  1209. [5 => false, 15 => false],
  1210. '<?php
  1211. abstract class Baz
  1212. {
  1213. final public const Y = "i";
  1214. }
  1215. ',
  1216. ];
  1217. yield [
  1218. [4 => false, 12 => false, 23 => false],
  1219. '<?php
  1220. class Test {
  1221. public function __construct(
  1222. public $prop = new Foo,
  1223. ) {}
  1224. }
  1225. ',
  1226. ];
  1227. yield 'intersection' => [
  1228. [3 => false, 9 => false, 11 => false],
  1229. '<?php function foo(): \Foo&Bar {}',
  1230. ];
  1231. yield 'abstract method return intersection' => [
  1232. [6 => false, 16 => false, 21 => false, 23 => false, 25 => false, 27 => false, 29 => false],
  1233. '<?php
  1234. abstract class Baz
  1235. {
  1236. abstract public function foo(): Foo&Bar1&Bar2&Bar3&Bar4;
  1237. }
  1238. ',
  1239. ];
  1240. }
  1241. public function testIsConstantInvocationInvalid(): void
  1242. {
  1243. $this->expectException(\LogicException::class);
  1244. $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
  1245. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  1246. $tokensAnalyzer->isConstantInvocation(0);
  1247. }
  1248. /**
  1249. * @requires PHP 8.0
  1250. */
  1251. public function testIsConstantInvocationForNullSafeObjectOperator(): void
  1252. {
  1253. $tokens = Tokens::fromCode('<?php $a?->b?->c;');
  1254. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1255. foreach ($tokens as $index => $token) {
  1256. if (!$token->isGivenKind(T_STRING)) {
  1257. continue;
  1258. }
  1259. self::assertFalse($tokensAnalyzer->isConstantInvocation($index));
  1260. }
  1261. }
  1262. /**
  1263. * @param array<int, bool> $expected
  1264. *
  1265. * @dataProvider provideIsUnarySuccessorOperatorCases
  1266. */
  1267. public function testIsUnarySuccessorOperator(array $expected, string $source): void
  1268. {
  1269. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1270. foreach ($expected as $index => $isUnary) {
  1271. self::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  1272. if ($isUnary) {
  1273. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1274. self::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1275. }
  1276. }
  1277. }
  1278. public static function provideIsUnarySuccessorOperatorCases(): iterable
  1279. {
  1280. yield [
  1281. [2 => true],
  1282. '<?php $a++;',
  1283. ];
  1284. yield [
  1285. [2 => true],
  1286. '<?php $a--;',
  1287. ];
  1288. yield [
  1289. [3 => true],
  1290. '<?php $a ++;',
  1291. ];
  1292. yield [
  1293. [2 => true, 4 => false],
  1294. '<?php $a++ + 1;',
  1295. ];
  1296. yield [
  1297. [5 => true],
  1298. '<?php ${"a"}++;',
  1299. ];
  1300. yield [
  1301. [4 => true],
  1302. '<?php $foo->bar++;',
  1303. ];
  1304. yield [
  1305. [6 => true],
  1306. '<?php $foo->{"bar"}++;',
  1307. ];
  1308. yield 'array access' => [
  1309. [5 => true],
  1310. '<?php $a["foo"]++;',
  1311. ];
  1312. yield 'array curly access' => [
  1313. [5 => true],
  1314. '<?php $a{"foo"}++;',
  1315. ];
  1316. }
  1317. /**
  1318. * @param array<int, bool> $expected
  1319. *
  1320. * @dataProvider provideIsUnaryPredecessorOperatorCases
  1321. */
  1322. public function testIsUnaryPredecessorOperator(array $expected, string $source): void
  1323. {
  1324. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1325. foreach ($expected as $index => $isUnary) {
  1326. self::assertSame($isUnary, $tokensAnalyzer->isUnaryPredecessorOperator($index));
  1327. if ($isUnary) {
  1328. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1329. self::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1330. }
  1331. }
  1332. }
  1333. public static function provideIsUnaryPredecessorOperatorCases(): iterable
  1334. {
  1335. yield [
  1336. [1 => true],
  1337. '<?php ++$a;',
  1338. ];
  1339. yield [
  1340. [1 => true],
  1341. '<?php --$a;',
  1342. ];
  1343. yield [
  1344. [1 => true],
  1345. '<?php -- $a;',
  1346. ];
  1347. yield [
  1348. [3 => false, 5 => true],
  1349. '<?php $a + ++$b;',
  1350. ];
  1351. yield [
  1352. [1 => true, 2 => true],
  1353. '<?php !!$a;',
  1354. ];
  1355. yield [
  1356. [5 => true],
  1357. '<?php $a = &$b;',
  1358. ];
  1359. yield [
  1360. [3 => true],
  1361. '<?php function &foo() {}',
  1362. ];
  1363. yield [
  1364. [1 => true],
  1365. '<?php @foo();',
  1366. ];
  1367. yield [
  1368. [3 => true, 8 => true],
  1369. '<?php foo(+ $a, -$b);',
  1370. ];
  1371. yield [
  1372. [5 => true, 11 => true, 17 => true],
  1373. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  1374. ];
  1375. yield [
  1376. [8 => true],
  1377. '<?php function foo($a, ...$b) {}',
  1378. ];
  1379. yield [
  1380. [5 => true, 6 => true],
  1381. '<?php function foo(&...$b) {}',
  1382. ];
  1383. yield [
  1384. [7 => true],
  1385. '<?php function foo(array ...$b) {}',
  1386. ];
  1387. yield [
  1388. [7 => true],
  1389. '<?php $foo = function(...$a) {};',
  1390. ];
  1391. yield [
  1392. [10 => true],
  1393. '<?php $foo = function($a, ...$b) {};',
  1394. ];
  1395. }
  1396. /**
  1397. * @param array<int, bool> $expected
  1398. *
  1399. * @dataProvider provideIsBinaryOperatorCases
  1400. */
  1401. public function testIsBinaryOperator(array $expected, string $source): void
  1402. {
  1403. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1404. foreach ($expected as $index => $isBinary) {
  1405. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1406. if ($isBinary) {
  1407. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1408. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1409. }
  1410. }
  1411. }
  1412. public static function provideIsBinaryOperatorCases(): iterable
  1413. {
  1414. yield [
  1415. [8 => true],
  1416. '<?php echo $a[1] + 1;',
  1417. ];
  1418. yield [
  1419. [8 => true],
  1420. '<?php echo $a{1} + 1;',
  1421. ];
  1422. yield [
  1423. [3 => true],
  1424. '<?php $a .= $b; ?>',
  1425. ];
  1426. yield [
  1427. [3 => true],
  1428. '<?php $a . \'a\' ?>',
  1429. ];
  1430. yield [
  1431. [3 => true],
  1432. '<?php $a &+ $b;',
  1433. ];
  1434. yield [
  1435. [3 => true],
  1436. '<?php $a && $b;',
  1437. ];
  1438. yield [
  1439. [3 => true],
  1440. '<?php $a & $b;',
  1441. ];
  1442. yield [
  1443. [4 => true],
  1444. '<?php [] + [];',
  1445. ];
  1446. yield [
  1447. [3 => true],
  1448. '<?php $a + $b;',
  1449. ];
  1450. yield [
  1451. [3 => true],
  1452. '<?php 1 + $b;',
  1453. ];
  1454. yield [
  1455. [3 => true],
  1456. '<?php 0.2 + $b;',
  1457. ];
  1458. yield [
  1459. [6 => true],
  1460. '<?php $a[1] + $b;',
  1461. ];
  1462. yield [
  1463. [3 => true],
  1464. '<?php FOO + $b;',
  1465. ];
  1466. yield [
  1467. [5 => true],
  1468. '<?php foo() + $b;',
  1469. ];
  1470. yield [
  1471. [6 => true],
  1472. '<?php ${"foo"} + $b;',
  1473. ];
  1474. yield [
  1475. [2 => true],
  1476. '<?php $a+$b;',
  1477. ];
  1478. yield [
  1479. [5 => true],
  1480. '<?php $a /* foo */ + /* bar */ $b;',
  1481. ];
  1482. yield [
  1483. [3 => true],
  1484. '<?php $a =
  1485. $b;',
  1486. ];
  1487. yield [
  1488. [3 => true],
  1489. '<?php $a
  1490. = $b;',
  1491. ];
  1492. yield [
  1493. [3 => true, 9 => true, 12 => false],
  1494. '<?php $a = array("b" => "c", );',
  1495. ];
  1496. yield [
  1497. [3 => true, 5 => false],
  1498. '<?php $a * -$b;',
  1499. ];
  1500. yield [
  1501. [3 => true, 5 => false, 8 => true, 10 => false],
  1502. '<?php $a = -2 / +5;',
  1503. ];
  1504. yield [
  1505. [3 => true, 5 => false],
  1506. '<?php $a = &$b;',
  1507. ];
  1508. yield [
  1509. [2 => false, 4 => true],
  1510. '<?php $a++ + $b;',
  1511. ];
  1512. yield [
  1513. [7 => true],
  1514. '<?php $a = FOO & $bar;',
  1515. ];
  1516. yield [
  1517. [3 => true],
  1518. '<?php __LINE__ - 1;',
  1519. ];
  1520. yield [
  1521. [5 => true],
  1522. '<?php `echo 1` + 1;',
  1523. ];
  1524. yield [
  1525. [3 => true],
  1526. '<?php $a ** $b;',
  1527. ];
  1528. yield [
  1529. [3 => true],
  1530. '<?php $a **= $b;',
  1531. ];
  1532. yield [
  1533. [9 => false],
  1534. '<?php $a = "{$value}-{$theSwitch}";',
  1535. ];
  1536. yield [
  1537. [3 => false],
  1538. '<?=$path?>-<?=$id?>',
  1539. ];
  1540. $operators = [
  1541. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  1542. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
  1543. ];
  1544. foreach ($operators as $operator) {
  1545. yield [
  1546. [3 => true],
  1547. '<?php $a '.$operator.' $b;',
  1548. ];
  1549. }
  1550. yield [
  1551. [3 => true],
  1552. '<?php $a <=> $b;',
  1553. ];
  1554. yield [
  1555. [3 => true],
  1556. '<?php $a ?? $b;',
  1557. ];
  1558. }
  1559. /**
  1560. * @dataProvider provideIsArrayCases
  1561. */
  1562. public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
  1563. {
  1564. $tokens = Tokens::fromCode($source);
  1565. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1566. self::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  1567. self::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  1568. }
  1569. public static function provideIsArrayCases(): iterable
  1570. {
  1571. yield [
  1572. '<?php
  1573. array("a" => 1);
  1574. ',
  1575. 2,
  1576. ];
  1577. yield [
  1578. '<?php
  1579. ["a" => 2];
  1580. ',
  1581. 2, false,
  1582. ];
  1583. yield [
  1584. '<?php
  1585. array(
  1586. "a" => 3
  1587. );
  1588. ',
  1589. 2, true,
  1590. ];
  1591. yield [
  1592. '<?php
  1593. [
  1594. "a" => 4
  1595. ];
  1596. ',
  1597. 2, true,
  1598. ];
  1599. yield [
  1600. '<?php
  1601. array(
  1602. "a" => array(5, 6, 7),
  1603. 8 => new \Exception(\'Hello\')
  1604. );
  1605. ',
  1606. 2, true,
  1607. ];
  1608. yield [
  1609. // mix short array syntax
  1610. '<?php
  1611. array(
  1612. "a" => [9, 10, 11],
  1613. 12 => new \Exception(\'Hello\')
  1614. );
  1615. ',
  1616. 2, true,
  1617. ];
  1618. // Windows/Max EOL testing
  1619. yield [
  1620. "<?php\r\narray('a' => 13);\r\n",
  1621. 1,
  1622. ];
  1623. yield [
  1624. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  1625. 2, true,
  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. self::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(): iterable
  1647. {
  1648. yield [
  1649. '<?php
  1650. [$a] = $z;
  1651. ["a" => $a, "b" => $b] = $array;
  1652. $c = [$d, $e] = $array[$a];
  1653. [[$a, $b], [$c, $d]] = $d;
  1654. $array = []; $d = array();
  1655. ',
  1656. [76, 84],
  1657. ];
  1658. }
  1659. /**
  1660. * @param array<int, bool> $expected
  1661. *
  1662. * @dataProvider provideIsBinaryOperator71Cases
  1663. */
  1664. public function testIsBinaryOperator71(array $expected, string $source): void
  1665. {
  1666. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1667. foreach ($expected as $index => $isBinary) {
  1668. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1669. if ($isBinary) {
  1670. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1671. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1672. }
  1673. }
  1674. }
  1675. public static function provideIsBinaryOperator71Cases(): iterable
  1676. {
  1677. yield [
  1678. [11 => false],
  1679. '<?php try {} catch (A | B $e) {}',
  1680. ];
  1681. yield [
  1682. [3 => true],
  1683. '<?php $a ??= $b;',
  1684. ];
  1685. }
  1686. /**
  1687. * @param array<int, bool> $expected
  1688. *
  1689. * @dataProvider provideIsBinaryOperator80Cases
  1690. *
  1691. * @requires PHP 8.0
  1692. */
  1693. public function testIsBinaryOperator80(array $expected, string $source): void
  1694. {
  1695. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1696. foreach ($expected as $index => $isBinary) {
  1697. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1698. if ($isBinary) {
  1699. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1700. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1701. }
  1702. }
  1703. }
  1704. public static function provideIsBinaryOperator80Cases(): iterable
  1705. {
  1706. yield [
  1707. [6 => false],
  1708. '<?php function foo(array|string $x) {}',
  1709. ];
  1710. yield [
  1711. [6 => false],
  1712. '<?php function foo(string|array $x) {}',
  1713. ];
  1714. yield [
  1715. [6 => false],
  1716. '<?php function foo(int|callable $x) {}',
  1717. ];
  1718. yield [
  1719. [6 => false],
  1720. '<?php function foo(callable|int $x) {}',
  1721. ];
  1722. yield [
  1723. [6 => false],
  1724. '<?php function foo(bool|int &$x) {}',
  1725. ];
  1726. }
  1727. /**
  1728. * @param array<int, bool> $expected
  1729. *
  1730. * @dataProvider provideIsBinaryOperator81Cases
  1731. *
  1732. * @requires PHP 8.1
  1733. */
  1734. public function testIsBinaryOperator81(array $expected, string $source): void
  1735. {
  1736. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1737. foreach ($expected as $index => $isBinary) {
  1738. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1739. if ($isBinary) {
  1740. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1741. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1742. }
  1743. }
  1744. }
  1745. public static function provideIsBinaryOperator81Cases(): iterable
  1746. {
  1747. yield 'type intersection' => [
  1748. [6 => false],
  1749. '<?php function foo(array&string $x) {}',
  1750. ];
  1751. }
  1752. /**
  1753. * @param array<int, bool> $expected
  1754. *
  1755. * @dataProvider provideIsBinaryOperator82Cases
  1756. *
  1757. * @requires PHP 8.2
  1758. */
  1759. public function testIsBinaryOperator82(array $expected, string $source): void
  1760. {
  1761. $tokens = Tokens::fromCode($source);
  1762. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1763. foreach ($tokens as $index => $token) {
  1764. $isBinary = isset($expected[$index]);
  1765. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1766. if ($isBinary) {
  1767. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1768. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1769. }
  1770. }
  1771. }
  1772. public static function provideIsBinaryOperator82Cases(): iterable
  1773. {
  1774. yield [
  1775. [],
  1776. '<?php class Dnf { public static I|(P&S11) $f2;}',
  1777. ];
  1778. yield [
  1779. [],
  1780. '<?php function Foo((A&B)|I $x): (X&Z)|(p\f\G&Y\Z)|z { return foo();}',
  1781. ];
  1782. $particularEndOfFile = 'A|(B&C); }';
  1783. yield sprintf('block "%s" at the end of file that is a type', $particularEndOfFile) => [
  1784. [],
  1785. '<?php abstract class A { abstract function foo(): '.$particularEndOfFile,
  1786. ];
  1787. yield sprintf('block "%s" at the end of file that is not a type', $particularEndOfFile) => [
  1788. [12 => true, 15 => true],
  1789. '<?php function foo() { return '.$particularEndOfFile,
  1790. ];
  1791. }
  1792. /**
  1793. * @dataProvider provideArrayExceptionsCases
  1794. */
  1795. public function testIsNotArray(string $source, int $tokenIndex): void
  1796. {
  1797. $tokens = Tokens::fromCode($source);
  1798. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1799. self::assertFalse($tokensAnalyzer->isArray($tokenIndex));
  1800. }
  1801. /**
  1802. * @dataProvider provideArrayExceptionsCases
  1803. */
  1804. public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
  1805. {
  1806. $this->expectException(\InvalidArgumentException::class);
  1807. $tokens = Tokens::fromCode($source);
  1808. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1809. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  1810. }
  1811. public static function provideArrayExceptionsCases(): iterable
  1812. {
  1813. yield ['<?php $a;', 1];
  1814. yield ["<?php\n \$a = (0+1); // [0,1]", 4];
  1815. yield ['<?php $text = "foo $bbb[0] bar";', 8];
  1816. yield ['<?php $text = "foo ${aaa[123]} bar";', 9];
  1817. }
  1818. public function testIsBlockMultilineException(): void
  1819. {
  1820. $this->expectException(\LogicException::class);
  1821. $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
  1822. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1823. $tokensAnalyzer->isBlockMultiline($tokens, 1);
  1824. }
  1825. /**
  1826. * @dataProvider provideIsBlockMultilineCases
  1827. */
  1828. public function testIsBlockMultiline(bool $isBlockMultiline, string $source, int $tokenIndex): void
  1829. {
  1830. $tokens = Tokens::fromCode($source);
  1831. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1832. self::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
  1833. }
  1834. public static function provideIsBlockMultilineCases(): iterable
  1835. {
  1836. yield [
  1837. false,
  1838. '<?php foo(1, 2, 3);',
  1839. 2,
  1840. ];
  1841. yield [
  1842. true,
  1843. '<?php foo(1,
  1844. 2,
  1845. 3
  1846. );',
  1847. 2,
  1848. ];
  1849. yield [
  1850. false,
  1851. '<?php foo(1, "Multi
  1852. string", 2, 3);',
  1853. 2,
  1854. ];
  1855. yield [
  1856. false,
  1857. '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
  1858. "a",
  1859. "b"
  1860. ), 2, 3);',
  1861. 2,
  1862. ];
  1863. }
  1864. /**
  1865. * @param array{visibility: ?int, static: bool, abstract: bool, final: bool} $expected
  1866. *
  1867. * @dataProvider provideGetFunctionPropertiesCases
  1868. */
  1869. public function testGetFunctionProperties(string $source, int $index, array $expected): void
  1870. {
  1871. $tokens = Tokens::fromCode($source);
  1872. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1873. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  1874. self::assertSame($expected, $attributes);
  1875. }
  1876. public static function provideGetFunctionPropertiesCases(): iterable
  1877. {
  1878. $defaultAttributes = [
  1879. 'visibility' => null,
  1880. 'static' => false,
  1881. 'abstract' => false,
  1882. 'final' => false,
  1883. ];
  1884. $template = '
  1885. <?php
  1886. class TestClass {
  1887. %s function a() {
  1888. //
  1889. }
  1890. }
  1891. ';
  1892. $cases = [];
  1893. $attributes = $defaultAttributes;
  1894. $attributes['visibility'] = T_PRIVATE;
  1895. $cases[] = [sprintf($template, 'private'), 10, $attributes];
  1896. $attributes = $defaultAttributes;
  1897. $attributes['visibility'] = T_PUBLIC;
  1898. $cases[] = [sprintf($template, 'public'), 10, $attributes];
  1899. $attributes = $defaultAttributes;
  1900. $attributes['visibility'] = T_PROTECTED;
  1901. $cases[] = [sprintf($template, 'protected'), 10, $attributes];
  1902. $attributes = $defaultAttributes;
  1903. $attributes['visibility'] = null;
  1904. $attributes['static'] = true;
  1905. $cases[] = [sprintf($template, 'static'), 10, $attributes];
  1906. $attributes = $defaultAttributes;
  1907. $attributes['visibility'] = T_PUBLIC;
  1908. $attributes['static'] = true;
  1909. $attributes['final'] = true;
  1910. $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
  1911. $attributes = $defaultAttributes;
  1912. $attributes['visibility'] = null;
  1913. $attributes['abstract'] = true;
  1914. $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
  1915. $attributes = $defaultAttributes;
  1916. $attributes['visibility'] = T_PUBLIC;
  1917. $attributes['abstract'] = true;
  1918. $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
  1919. $attributes = $defaultAttributes;
  1920. $cases[] = [sprintf($template, ''), 8, $attributes];
  1921. return $cases;
  1922. }
  1923. public function testIsWhilePartOfDoWhile(): void
  1924. {
  1925. $source =
  1926. <<<'SRC'
  1927. <?php
  1928. // `not do`
  1929. while(false) {
  1930. }
  1931. while (false);
  1932. while (false)?>
  1933. <?php
  1934. if(false){
  1935. }while(false);
  1936. if(false){
  1937. }while(false)?><?php
  1938. while(false){}while(false){}
  1939. while ($i <= 10):
  1940. echo $i;
  1941. $i++;
  1942. endwhile;
  1943. ?>
  1944. <?php while(false): ?>
  1945. <?php endwhile ?>
  1946. <?php
  1947. // `do`
  1948. do{
  1949. } while(false);
  1950. do{
  1951. } while(false)?>
  1952. <?php
  1953. if (false){}do{}while(false);
  1954. // `not do`, `do`
  1955. if(false){}while(false){}do{}while(false);
  1956. SRC;
  1957. $expected = [
  1958. 3 => false,
  1959. 12 => false,
  1960. 19 => false,
  1961. 34 => false,
  1962. 47 => false,
  1963. 53 => false,
  1964. 59 => false,
  1965. 66 => false,
  1966. 91 => false,
  1967. 112 => true,
  1968. 123 => true,
  1969. 139 => true,
  1970. 153 => false,
  1971. 162 => true,
  1972. ];
  1973. $tokens = Tokens::fromCode($source);
  1974. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1975. foreach ($tokens as $index => $token) {
  1976. if (!$token->isGivenKind(T_WHILE)) {
  1977. continue;
  1978. }
  1979. self::assertSame(
  1980. $expected[$index],
  1981. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  1982. sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not ')
  1983. );
  1984. }
  1985. }
  1986. /**
  1987. * @dataProvider provideIsEnumCaseCases
  1988. *
  1989. * @param array<int, bool> $expected
  1990. *
  1991. * @requires PHP 8.1
  1992. */
  1993. public function testIsEnumCase(string $source, array $expected): void
  1994. {
  1995. $tokens = Tokens::fromCode($source);
  1996. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1997. foreach ($tokens as $index => $token) {
  1998. if (!$token->isGivenKind(T_CASE)) {
  1999. try {
  2000. $tokensAnalyzer->isEnumCase($index);
  2001. self::fail('TokensAnalyzer::isEnumCase() did not throw LogicException.');
  2002. } catch (\Throwable $e) {
  2003. self::assertInstanceOf(\LogicException::class, $e);
  2004. self::assertMatchesRegularExpression('/^No T_CASE given at index \d+, got \S+ instead\.$/', $e->getMessage());
  2005. }
  2006. continue;
  2007. }
  2008. self::assertSame($expected[$index], $tokensAnalyzer->isEnumCase($index));
  2009. }
  2010. }
  2011. public static function provideIsEnumCaseCases(): iterable
  2012. {
  2013. yield 'switch only' => [
  2014. '<?php
  2015. function bar(string $a): string
  2016. {
  2017. switch ($a) {
  2018. case \'one\':
  2019. return $a;
  2020. case \'two\':
  2021. default:
  2022. return strtoupper($a);
  2023. }
  2024. }
  2025. ',
  2026. [
  2027. 23 => false,
  2028. 33 => false,
  2029. ],
  2030. ];
  2031. yield 'pure enum' => [
  2032. '<?php
  2033. enum Foo
  2034. {
  2035. case One;
  2036. case Two;
  2037. }
  2038. ',
  2039. [
  2040. 7 => true,
  2041. 12 => true,
  2042. ],
  2043. ];
  2044. yield 'pure enum with switch' => [
  2045. '<?php
  2046. enum Foo
  2047. {
  2048. case One;
  2049. case Two;
  2050. public static function getLowerName(self $instance): string
  2051. {
  2052. switch ($instance->name) {
  2053. case \'One\':
  2054. case \'Two\':
  2055. return strtolower($instance->name);
  2056. }
  2057. }
  2058. }
  2059. ',
  2060. [
  2061. 7 => true,
  2062. 12 => true,
  2063. 45 => false,
  2064. 50 => false,
  2065. ],
  2066. ];
  2067. yield 'backed enum' => [
  2068. '<?php
  2069. enum Suit: string
  2070. {
  2071. case Hearts = \'hearts\';
  2072. case Spades = \'spades\';
  2073. case Clubs = \'clubs\';
  2074. case Diamonds = \'diamonds\';
  2075. }
  2076. ',
  2077. [
  2078. 10 => true,
  2079. 19 => true,
  2080. 28 => true,
  2081. 37 => true,
  2082. ],
  2083. ];
  2084. yield 'backed enum with switch' => [
  2085. '<?php
  2086. enum Suit: string
  2087. {
  2088. case Hearts = \'hearts\';
  2089. case Spades = \'spades\';
  2090. case Clubs = \'clubs\';
  2091. case Diamonds = \'diamonds\';
  2092. public static function getUppercasedValue(self $instance): string
  2093. {
  2094. switch ($instance->value) {
  2095. case \'hearts\':
  2096. case \'spades\':
  2097. return strtoupper($instance->value);
  2098. default:
  2099. return $instance->value;
  2100. }
  2101. }
  2102. }
  2103. ',
  2104. [
  2105. 10 => true,
  2106. 19 => true,
  2107. 28 => true,
  2108. 37 => true,
  2109. 74 => false,
  2110. 79 => false,
  2111. ],
  2112. ];
  2113. }
  2114. /**
  2115. * @param array<int, list<int>>|list<int> $expected
  2116. *
  2117. * @dataProvider provideGetImportUseIndexesCases
  2118. */
  2119. public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
  2120. {
  2121. $tokens = Tokens::fromCode($input);
  2122. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2123. self::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  2124. }
  2125. public static function provideGetImportUseIndexesCases(): iterable
  2126. {
  2127. yield [
  2128. [1, 8],
  2129. '<?php use E\F?><?php use A\B;',
  2130. ];
  2131. yield [
  2132. [[1], [14], [29]],
  2133. '<?php
  2134. use T\A;
  2135. namespace A { use D\C; }
  2136. namespace b { use D\C; }
  2137. ',
  2138. true,
  2139. ];
  2140. yield [
  2141. [[1, 8]],
  2142. '<?php use D\B; use A\C?>',
  2143. true,
  2144. ];
  2145. yield [
  2146. [1, 8],
  2147. '<?php use D\B; use A\C?>',
  2148. ];
  2149. yield [
  2150. [7, 22],
  2151. '<?php
  2152. namespace A { use D\C; }
  2153. namespace b { use D\C; }
  2154. ',
  2155. ];
  2156. yield [
  2157. [3, 10, 34, 45, 54, 59, 77, 95],
  2158. <<<'EOF'
  2159. use Zoo\Bar;
  2160. use Foo\Bar;
  2161. use Foo\Zar\Baz;
  2162. <?php
  2163. use Foo\Bar;
  2164. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  2165. use Foo\Bir as FBB;
  2166. use Foo\Zar\Baz;
  2167. use SomeClass;
  2168. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  2169. use Zoo\Bar;
  2170. $a = new someclass();
  2171. use Zoo\Tar;
  2172. class AnnotatedClass
  2173. {
  2174. }
  2175. EOF
  2176. ,
  2177. ];
  2178. yield [
  2179. [1, 22, 41],
  2180. '<?php
  2181. use some\a\{ClassA, ClassB, ClassC as C};
  2182. use function some\a\{fn_a, fn_b, fn_c};
  2183. use const some\a\{ConstA, ConstB, ConstC};
  2184. ',
  2185. ];
  2186. yield [
  2187. [[1, 22, 41]],
  2188. '<?php
  2189. use some\a\{ClassA, ClassB, ClassC as C};
  2190. use function some\a\{fn_a, fn_b, fn_c};
  2191. use const some\a\{ConstA, ConstB, ConstC};
  2192. ',
  2193. true,
  2194. ];
  2195. yield [
  2196. [1, 23, 43],
  2197. '<?php
  2198. use some\a\{ClassA, ClassB, ClassC as C,};
  2199. use function some\a\{fn_a, fn_b, fn_c,};
  2200. use const some\a\{ConstA, ConstB, ConstC,};
  2201. ',
  2202. ];
  2203. yield [
  2204. [[1, 23, 43]],
  2205. '<?php
  2206. use some\a\{ClassA, ClassB, ClassC as C,};
  2207. use function some\a\{fn_a, fn_b, fn_c,};
  2208. use const some\a\{ConstA, ConstB, ConstC,};
  2209. ',
  2210. true,
  2211. ];
  2212. }
  2213. public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
  2214. {
  2215. $source = '<?php
  2216. class MyTestWithAnonymousClass extends TestCase
  2217. {
  2218. public function setUp()
  2219. {
  2220. $provider = new class(function () {}) {};
  2221. }
  2222. public function testSomethingWithMoney(
  2223. Money $amount
  2224. ) {
  2225. $a = new class(function () {
  2226. new class(function () {
  2227. new class(function () {})
  2228. {
  2229. const A=1;
  2230. };
  2231. })
  2232. {
  2233. const B=1;
  2234. public function foo() {
  2235. $c = new class() {const AA=3;};
  2236. $d = new class {const AB=3;};
  2237. }
  2238. };
  2239. })
  2240. {
  2241. const C=1;
  2242. };
  2243. }
  2244. }';
  2245. $tokens = Tokens::fromCode($source);
  2246. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2247. $elements = $tokensAnalyzer->getClassyElements();
  2248. self::assertSame([
  2249. 13 => [
  2250. 'classIndex' => 1,
  2251. 'token' => $tokens[13],
  2252. 'type' => 'method', // setUp
  2253. ],
  2254. 46 => [
  2255. 'classIndex' => 1,
  2256. 'token' => $tokens[46],
  2257. 'type' => 'method', // testSomethingWithMoney
  2258. ],
  2259. 100 => [
  2260. 'classIndex' => 87,
  2261. 'token' => $tokens[100], // const A
  2262. 'type' => 'const',
  2263. ],
  2264. 115 => [
  2265. 'classIndex' => 65,
  2266. 'token' => $tokens[115], // const B
  2267. 'type' => 'const',
  2268. ],
  2269. 124 => [
  2270. 'classIndex' => 65, // $a
  2271. 'token' => $tokens[124],
  2272. 'type' => 'method', // foo
  2273. ],
  2274. 143 => [
  2275. 'classIndex' => 138,
  2276. 'token' => $tokens[143], // const AA
  2277. 'type' => 'const',
  2278. ],
  2279. 161 => [
  2280. 'classIndex' => 158,
  2281. 'token' => $tokens[161], // const AB
  2282. 'type' => 'const',
  2283. ],
  2284. ], $elements);
  2285. }
  2286. /**
  2287. * @dataProvider provideIsSuperGlobalCases
  2288. */
  2289. public function testIsSuperGlobal(bool $expected, string $source, int $index): void
  2290. {
  2291. $tokens = Tokens::fromCode($source);
  2292. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2293. self::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
  2294. }
  2295. public static function provideIsSuperGlobalCases(): iterable
  2296. {
  2297. $superNames = [
  2298. '$_COOKIE',
  2299. '$_ENV',
  2300. '$_FILES',
  2301. '$_GET',
  2302. '$_POST',
  2303. '$_REQUEST',
  2304. '$_SERVER',
  2305. '$_SESSION',
  2306. '$GLOBALS',
  2307. ];
  2308. $cases = [];
  2309. foreach ($superNames as $superName) {
  2310. $cases[] = [
  2311. true,
  2312. sprintf('<?php echo %s[0];', $superName),
  2313. 3,
  2314. ];
  2315. }
  2316. $notGlobalCodeCases = [
  2317. '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
  2318. '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
  2319. ];
  2320. foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
  2321. $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
  2322. for ($i = 0; $i < $tokensCount; ++$i) {
  2323. $cases[] = [
  2324. false,
  2325. $notGlobalCodeCase,
  2326. $i,
  2327. ];
  2328. }
  2329. }
  2330. return $cases;
  2331. }
  2332. /**
  2333. * @dataProvider provideGetClassyModifiersCases
  2334. *
  2335. * @param array<string, null|int> $expectedModifiers
  2336. */
  2337. public function testGetClassyModifiers(array $expectedModifiers, int $index, string $source): void
  2338. {
  2339. $tokens = Tokens::fromCode($source);
  2340. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2341. self::assertSame($expectedModifiers, $tokensAnalyzer->getClassyModifiers($index));
  2342. }
  2343. public static function provideGetClassyModifiersCases(): iterable
  2344. {
  2345. yield 'final' => [
  2346. ['final' => 1, 'abstract' => null, 'readonly' => null],
  2347. 3,
  2348. '<?php final class Foo {}',
  2349. ];
  2350. yield 'abstract' => [
  2351. ['final' => null, 'abstract' => 3, 'readonly' => null],
  2352. 5,
  2353. '<?php /* comment */ abstract class Foo {}',
  2354. ];
  2355. }
  2356. /**
  2357. * @requires PHP 8.2
  2358. *
  2359. * @dataProvider provideGetClassyModifiersOnPhp82Cases
  2360. *
  2361. * @param array<string, null|int> $expectedModifiers
  2362. */
  2363. public function testGetClassyModifiersOnPhp82(array $expectedModifiers, int $index, string $source): void
  2364. {
  2365. $tokens = Tokens::fromCode($source);
  2366. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2367. self::assertSame($expectedModifiers, $tokensAnalyzer->getClassyModifiers($index));
  2368. }
  2369. public static function provideGetClassyModifiersOnPhp82Cases(): iterable
  2370. {
  2371. yield 'readonly' => [
  2372. ['final' => null, 'abstract' => null, 'readonly' => 1],
  2373. 3,
  2374. '<?php readonly class Foo {}',
  2375. ];
  2376. yield 'readonly final' => [
  2377. ['final' => 3, 'abstract' => null, 'readonly' => 1],
  2378. 5,
  2379. '<?php readonly final class Foo {}',
  2380. ];
  2381. yield 'readonly final reversed' => [
  2382. ['final' => 1, 'abstract' => null, 'readonly' => 3],
  2383. 5,
  2384. '<?php final readonly class Foo {}',
  2385. ];
  2386. yield 'readonly final reversed + comment' => [
  2387. ['final' => 1, 'abstract' => null, 'readonly' => 5],
  2388. 7,
  2389. '<?php final /* comment */ readonly class Foo {}',
  2390. ];
  2391. yield 'readonly abstract' => [
  2392. ['final' => null, 'abstract' => 3, 'readonly' => 1],
  2393. 5,
  2394. '<?php readonly abstract class Foo {}',
  2395. ];
  2396. yield 'readonly abstract reversed' => [
  2397. ['final' => null, 'abstract' => 1, 'readonly' => 3],
  2398. 5,
  2399. '<?php abstract readonly class Foo {}',
  2400. ];
  2401. yield 'readonly abstract reversed + comment' => [
  2402. ['final' => null, 'abstract' => 1, 'readonly' => 5],
  2403. 7,
  2404. '<?php abstract /* comment */ readonly class Foo {}',
  2405. ];
  2406. }
  2407. public function testGetClassyModifiersForNonClassyThrowsAnException(): void
  2408. {
  2409. $tokens = Tokens::fromCode('<?php echo 1;');
  2410. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2411. $this->expectException(\InvalidArgumentException::class);
  2412. $tokensAnalyzer->getClassyModifiers(1);
  2413. }
  2414. /**
  2415. * @param array<int, bool> $expected
  2416. */
  2417. private function doIsConstantInvocationTest(array $expected, string $source): void
  2418. {
  2419. $tokens = Tokens::fromCode($source);
  2420. self::assertCount(
  2421. $tokens->countTokenKind(T_STRING),
  2422. $expected,
  2423. 'All T_STRING tokens must be tested'
  2424. );
  2425. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2426. foreach ($expected as $index => $expectedValue) {
  2427. self::assertSame(
  2428. $expectedValue,
  2429. $tokensAnalyzer->isConstantInvocation($index),
  2430. sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
  2431. );
  2432. }
  2433. }
  2434. }