TokensAnalyzerTest.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158
  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, int $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. public function testGetClassyElementsWithNullableProperties(): void
  175. {
  176. $source = <<<'PHP'
  177. <?php
  178. class Foo
  179. {
  180. public int $prop0;
  181. protected ?array $prop1;
  182. private string $prop2 = 1;
  183. var ? Foo\Bar $prop3 = array(1,2,3);
  184. }
  185. PHP;
  186. $tokens = Tokens::fromCode($source);
  187. $tokensAnalyzer = new TokensAnalyzer($tokens);
  188. $elements = $tokensAnalyzer->getClassyElements();
  189. self::assertSame(
  190. [
  191. 11 => [
  192. 'classIndex' => 1,
  193. 'token' => $tokens[11],
  194. 'type' => 'property',
  195. ],
  196. 19 => [
  197. 'classIndex' => 1,
  198. 'token' => $tokens[19],
  199. 'type' => 'property',
  200. ],
  201. 26 => [
  202. 'classIndex' => 1,
  203. 'token' => $tokens[26],
  204. 'type' => 'property',
  205. ],
  206. 41 => [
  207. 'classIndex' => 1,
  208. 'token' => $tokens[41],
  209. 'type' => 'property',
  210. ],
  211. ],
  212. $elements
  213. );
  214. }
  215. public function testGetClassyElementsWithAnonymousClass(): void
  216. {
  217. $source = <<<'PHP'
  218. <?php
  219. class A {
  220. public $A;
  221. private function B()
  222. {
  223. return new class(){
  224. protected $level1;
  225. private function XYZ() {
  226. return new class(){private $level2 = 1;};
  227. }
  228. };
  229. }
  230. private function C() {
  231. }
  232. }
  233. function B() {} // do not count this
  234. PHP;
  235. $tokens = Tokens::fromCode($source);
  236. $tokensAnalyzer = new TokensAnalyzer($tokens);
  237. $elements = $tokensAnalyzer->getClassyElements();
  238. self::assertSame(
  239. [
  240. 9 => [
  241. 'classIndex' => 1,
  242. 'token' => $tokens[9],
  243. 'type' => 'property', // $A
  244. ],
  245. 14 => [
  246. 'classIndex' => 1,
  247. 'token' => $tokens[14],
  248. 'type' => 'method', // B
  249. ],
  250. 33 => [
  251. 'classIndex' => 26,
  252. 'token' => $tokens[33],
  253. 'type' => 'property', // $level1
  254. ],
  255. 38 => [
  256. 'classIndex' => 26,
  257. 'token' => $tokens[38],
  258. 'type' => 'method', // XYZ
  259. ],
  260. 56 => [
  261. 'classIndex' => 50,
  262. 'token' => $tokens[56],
  263. 'type' => 'property', // $level2
  264. ],
  265. 74 => [
  266. 'classIndex' => 1,
  267. 'token' => $tokens[74],
  268. 'type' => 'method', // C
  269. ],
  270. ],
  271. $elements
  272. );
  273. }
  274. public function testGetClassyElementsWithMultipleAnonymousClass(): void
  275. {
  276. $source = <<<'PHP'
  277. <?php class A0
  278. {
  279. public function AA0()
  280. {
  281. return new class
  282. {
  283. public function BB0()
  284. {
  285. }
  286. };
  287. }
  288. public function otherFunction0()
  289. {
  290. }
  291. }
  292. class A1
  293. {
  294. public function AA1()
  295. {
  296. return new class
  297. {
  298. public function BB1()
  299. {
  300. return new class
  301. {
  302. public function CC1()
  303. {
  304. return new class
  305. {
  306. public function DD1()
  307. {
  308. return new class{};
  309. }
  310. public function DD2()
  311. {
  312. return new class{};
  313. }
  314. };
  315. }
  316. };
  317. }
  318. public function BB2()
  319. {
  320. return new class
  321. {
  322. public function CC2()
  323. {
  324. return new class
  325. {
  326. public function DD2()
  327. {
  328. return new class{};
  329. }
  330. };
  331. }
  332. };
  333. }
  334. };
  335. }
  336. public function otherFunction1()
  337. {
  338. }
  339. }
  340. PHP;
  341. $tokens = Tokens::fromCode($source);
  342. $tokensAnalyzer = new TokensAnalyzer($tokens);
  343. $elements = $tokensAnalyzer->getClassyElements();
  344. self::assertSame(
  345. [
  346. 9 => [
  347. 'classIndex' => 1,
  348. 'token' => $tokens[9],
  349. 'type' => 'method',
  350. ],
  351. 27 => [
  352. 'classIndex' => 21,
  353. 'token' => $tokens[27],
  354. 'type' => 'method',
  355. ],
  356. 44 => [
  357. 'classIndex' => 1,
  358. 'token' => $tokens[44],
  359. 'type' => 'method',
  360. ],
  361. 64 => [
  362. 'classIndex' => 56,
  363. 'token' => $tokens[64],
  364. 'type' => 'method',
  365. ],
  366. 82 => [
  367. 'classIndex' => 76,
  368. 'token' => $tokens[82],
  369. 'type' => 'method',
  370. ],
  371. 100 => [
  372. 'classIndex' => 94,
  373. 'token' => $tokens[100],
  374. 'type' => 'method',
  375. ],
  376. 118 => [
  377. 'classIndex' => 112,
  378. 'token' => $tokens[118],
  379. 'type' => 'method',
  380. ],
  381. 139 => [
  382. 'classIndex' => 112,
  383. 'token' => $tokens[139],
  384. 'type' => 'method',
  385. ],
  386. 170 => [
  387. 'classIndex' => 76,
  388. 'token' => $tokens[170],
  389. 'type' => 'method',
  390. ],
  391. 188 => [
  392. 'classIndex' => 182,
  393. 'token' => $tokens[188],
  394. 'type' => 'method',
  395. ],
  396. 206 => [
  397. 'classIndex' => 200,
  398. 'token' => $tokens[206],
  399. 'type' => 'method',
  400. ],
  401. 242 => [
  402. 'classIndex' => 56,
  403. 'token' => $tokens[242],
  404. 'type' => 'method',
  405. ],
  406. ],
  407. $elements
  408. );
  409. }
  410. public function testGetClassyElements74(): void
  411. {
  412. $source = <<<'PHP'
  413. <?php
  414. class Foo
  415. {
  416. public int $bar = 3;
  417. protected ?string $baz;
  418. private ?string $bazNull = null;
  419. public static iterable $staticProp;
  420. public float $x, $y;
  421. var bool $flag1;
  422. var ?bool $flag2;
  423. }
  424. PHP;
  425. $tokens = Tokens::fromCode($source);
  426. $tokensAnalyzer = new TokensAnalyzer($tokens);
  427. $elements = $tokensAnalyzer->getClassyElements();
  428. $expected = [];
  429. foreach ([11, 23, 31, 44, 51, 54, 61, 69] as $index) {
  430. $expected[$index] = [
  431. 'classIndex' => 1,
  432. 'token' => $tokens[$index],
  433. 'type' => 'property',
  434. ];
  435. }
  436. self::assertSame($expected, $elements);
  437. }
  438. /**
  439. * @param array<int, array{classIndex: int, type: string}> $expected
  440. *
  441. * @dataProvider provideGetClassyElements81Cases
  442. *
  443. * @requires PHP 8.1
  444. */
  445. public function testGetClassyElements81(array $expected, string $source): void
  446. {
  447. $tokens = Tokens::fromCode($source);
  448. $tokensAnalyzer = new TokensAnalyzer($tokens);
  449. $elements = $tokensAnalyzer->getClassyElements();
  450. array_walk(
  451. $expected,
  452. static function (array &$element, int $index) use ($tokens): void {
  453. $element['token'] = $tokens[$index];
  454. ksort($element);
  455. }
  456. );
  457. self::assertSame($expected, $elements);
  458. }
  459. public static function provideGetClassyElements81Cases(): iterable
  460. {
  461. yield [
  462. [
  463. 11 => [
  464. 'classIndex' => 1,
  465. 'type' => 'property', // $prop1
  466. ],
  467. 20 => [
  468. 'classIndex' => 1,
  469. 'type' => 'property', // $prop2
  470. ],
  471. 29 => [
  472. 'classIndex' => 1,
  473. 'type' => 'property', // $prop13
  474. ],
  475. ],
  476. '<?php
  477. class Foo
  478. {
  479. readonly string $prop1;
  480. readonly public string $prop2;
  481. public readonly string $prop3;
  482. }
  483. ',
  484. ];
  485. yield 'final const' => [
  486. [
  487. 11 => [
  488. 'classIndex' => 1,
  489. 'type' => 'const', // A
  490. ],
  491. 24 => [
  492. 'classIndex' => 1,
  493. 'type' => 'const', // B
  494. ],
  495. ],
  496. '<?php
  497. class Foo
  498. {
  499. final public const A = "1";
  500. public final const B = "2";
  501. }
  502. ',
  503. ];
  504. yield 'enum final const' => [
  505. [
  506. 11 => [
  507. 'classIndex' => 1,
  508. 'type' => 'const', // A
  509. ],
  510. 24 => [
  511. 'classIndex' => 1,
  512. 'type' => 'const', // B
  513. ],
  514. ],
  515. '<?php
  516. enum Foo
  517. {
  518. final public const A = "1";
  519. public final const B = "2";
  520. }
  521. ',
  522. ];
  523. yield 'enum case' => [
  524. [
  525. 12 => [
  526. 'classIndex' => 1,
  527. 'type' => 'const', // Spades
  528. ],
  529. 21 => [
  530. 'classIndex' => 1,
  531. 'type' => 'case', // Hearts
  532. ],
  533. 32 => [
  534. 'classIndex' => 1,
  535. 'type' => 'method', // function tests
  536. ],
  537. 81 => [
  538. 'classIndex' => 75,
  539. 'type' => 'method', // function bar123
  540. ],
  541. 135 => [
  542. 'classIndex' => 127,
  543. 'type' => 'method', // function bar7
  544. ],
  545. ],
  546. '<?php
  547. enum Foo: string
  548. {
  549. private const Spades = 123;
  550. case Hearts = "H";
  551. private function test($foo) {
  552. switch ($foo) {
  553. case 1:
  554. // case 2
  555. case 3:
  556. echo 123;
  557. break;
  558. }
  559. return new class {
  560. public function bar123($foo) {
  561. switch ($foo) {
  562. case 1:
  563. // case 2
  564. case 3:
  565. echo 123;
  566. };
  567. }
  568. };
  569. }
  570. }
  571. class Bar {
  572. public function bar7($foo) {
  573. switch ($foo) {
  574. case 1:
  575. // case 2
  576. case 3:
  577. echo 123;
  578. };
  579. }
  580. }
  581. ',
  582. ];
  583. yield 'enum' => [
  584. [
  585. 10 => [
  586. 'classIndex' => 1,
  587. 'type' => 'case',
  588. ],
  589. 19 => [
  590. 'classIndex' => 1,
  591. 'type' => 'case',
  592. ],
  593. 28 => [
  594. 'classIndex' => 1,
  595. 'type' => 'method',
  596. ],
  597. ],
  598. '<?php
  599. enum Foo: string
  600. {
  601. case Bar = "bar";
  602. case Baz = "baz";
  603. function qux() {
  604. switch (true) {
  605. case "x": break;
  606. }
  607. }
  608. }
  609. ',
  610. ];
  611. }
  612. /**
  613. * @param array<int, array{classIndex: int, type: string}> $expected
  614. *
  615. * @dataProvider provideGetClassyElements82Cases
  616. *
  617. * @requires PHP 8.2
  618. */
  619. public function testGetClassyElements82(array $expected, string $source): void
  620. {
  621. $tokens = Tokens::fromCode($source);
  622. $tokensAnalyzer = new TokensAnalyzer($tokens);
  623. $elements = $tokensAnalyzer->getClassyElements();
  624. array_walk(
  625. $expected,
  626. static function (array &$element, int $index) use ($tokens): void {
  627. $element['token'] = $tokens[$index];
  628. ksort($element);
  629. },
  630. );
  631. self::assertSame($expected, $elements);
  632. }
  633. public static function provideGetClassyElements82Cases(): iterable
  634. {
  635. yield 'constant in trait' => [
  636. [
  637. 7 => [
  638. 'classIndex' => 1,
  639. 'type' => 'const',
  640. ],
  641. 18 => [
  642. 'classIndex' => 1,
  643. 'type' => 'const',
  644. ],
  645. ],
  646. <<<'PHP'
  647. <?php
  648. trait Foo
  649. {
  650. const BAR = 0;
  651. final const BAZ = 1;
  652. }
  653. PHP,
  654. ];
  655. }
  656. /**
  657. * @param array<int, bool> $expected
  658. *
  659. * @dataProvider provideIsAnonymousClassCases
  660. */
  661. public function testIsAnonymousClass(array $expected, string $source): void
  662. {
  663. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  664. foreach ($expected as $index => $expectedValue) {
  665. self::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  666. }
  667. }
  668. /**
  669. * @return iterable<array{array<int, bool>, string}>
  670. */
  671. public static function provideIsAnonymousClassCases(): iterable
  672. {
  673. yield [
  674. [1 => false],
  675. '<?php class foo {}',
  676. ];
  677. yield [
  678. [7 => true],
  679. '<?php $foo = new class() {};',
  680. ];
  681. yield [
  682. [7 => true],
  683. '<?php $foo = new class() extends Foo implements Bar, Baz {};',
  684. ];
  685. yield [
  686. [1 => false, 19 => true],
  687. '<?php class Foo { function bar() { return new class() {}; } }',
  688. ];
  689. yield [
  690. [7 => true, 11 => true],
  691. '<?php $a = new class(new class($d->a) implements B{}) extends C{};',
  692. ];
  693. yield [
  694. [1 => false],
  695. '<?php interface foo {}',
  696. ];
  697. }
  698. /**
  699. * @param array<int, bool> $expected
  700. *
  701. * @dataProvider provideIsAnonymousClass80Cases
  702. *
  703. * @requires PHP 8.0
  704. */
  705. public function testIsAnonymousClass80(array $expected, string $source): void
  706. {
  707. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  708. foreach ($expected as $index => $expectedValue) {
  709. self::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  710. }
  711. }
  712. /**
  713. * @return iterable<array{array<int, bool>, string}>
  714. */
  715. public static function provideIsAnonymousClass80Cases(): iterable
  716. {
  717. yield [
  718. [11 => true],
  719. '<?php $object = new #[ExampleAttribute] class(){};',
  720. ];
  721. yield [
  722. [27 => true],
  723. '<?php $object = new #[A] #[B] #[C]#[D]/* */ /** */#[E]class(){};',
  724. ];
  725. }
  726. /**
  727. * @param array<int, bool> $expected
  728. *
  729. * @dataProvider provideIsAnonymousClass81Cases
  730. *
  731. * @requires PHP 8.1
  732. */
  733. public function testIsAnonymousClass81(array $expected, string $source): void
  734. {
  735. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  736. foreach ($expected as $index => $expectedValue) {
  737. self::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  738. }
  739. }
  740. /**
  741. * @return iterable<array{array<int, bool>, string}>
  742. */
  743. public static function provideIsAnonymousClass81Cases(): iterable
  744. {
  745. yield [
  746. [1 => false],
  747. '<?php enum foo {}',
  748. ];
  749. }
  750. /**
  751. * @param array<int, bool> $expected
  752. *
  753. * @dataProvider provideIsAnonymousClass83Cases
  754. *
  755. * @requires PHP 8.3
  756. */
  757. public function testIsAnonymousClass83(array $expected, string $source): void
  758. {
  759. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  760. foreach ($expected as $index => $expectedValue) {
  761. self::assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  762. }
  763. }
  764. /**
  765. * @return iterable<array{array<int, bool>, string}>
  766. */
  767. public static function provideIsAnonymousClass83Cases(): iterable
  768. {
  769. yield 'simple readonly anonymous class' => [
  770. [9 => true],
  771. '<?php $instance = new readonly class {};',
  772. ];
  773. yield 'readonly anonymous class with attribute' => [
  774. [13 => true],
  775. '<?php $instance = new #[Foo] readonly class {};',
  776. ];
  777. yield 'readonly anonymous class with multiple attributes' => [
  778. [17 => true],
  779. '<?php $instance = new #[Foo] #[BAR] readonly class {};',
  780. ];
  781. }
  782. /**
  783. * @param array<int, bool> $expected
  784. *
  785. * @dataProvider provideIsLambdaCases
  786. */
  787. public function testIsLambda(array $expected, string $source): void
  788. {
  789. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  790. foreach ($expected as $index => $isLambda) {
  791. self::assertSame($isLambda, $tokensAnalyzer->isLambda($index));
  792. }
  793. }
  794. public static function provideIsLambdaCases(): iterable
  795. {
  796. yield [
  797. [1 => false],
  798. '<?php function foo () {};',
  799. ];
  800. yield [
  801. [1 => false],
  802. '<?php function /** foo */ foo () {};',
  803. ];
  804. yield [
  805. [5 => true],
  806. '<?php $foo = function () {};',
  807. ];
  808. yield [
  809. [5 => true],
  810. '<?php $foo = function /** foo */ () {};',
  811. ];
  812. yield [
  813. [7 => true],
  814. '<?php
  815. preg_replace_callback(
  816. "/(^|[a-z])/",
  817. function (array $matches) {
  818. return "a";
  819. },
  820. $string
  821. );',
  822. ];
  823. yield [
  824. [5 => true],
  825. '<?php $foo = function &() {};',
  826. ];
  827. yield [
  828. [6 => true],
  829. '<?php
  830. $a = function (): array {
  831. return [];
  832. };',
  833. ];
  834. yield [
  835. [2 => false],
  836. '<?php
  837. function foo (): array {
  838. return [];
  839. };',
  840. ];
  841. yield [
  842. [6 => true],
  843. '<?php
  844. $a = function (): void {
  845. return [];
  846. };',
  847. ];
  848. yield [
  849. [2 => false],
  850. '<?php
  851. function foo (): void {
  852. return [];
  853. };',
  854. ];
  855. yield [
  856. [6 => true],
  857. '<?php
  858. $a = function (): ?int {
  859. return [];
  860. };',
  861. ];
  862. yield [
  863. [6 => true],
  864. '<?php
  865. $a = function (): int {
  866. return [];
  867. };',
  868. ];
  869. yield [
  870. [2 => false],
  871. '<?php
  872. function foo (): ?int {
  873. return [];
  874. };',
  875. ];
  876. }
  877. /**
  878. * @param array<int, bool> $expected
  879. *
  880. * @dataProvider provideIsLambda74Cases
  881. */
  882. public function testIsLambda74(array $expected, string $source): void
  883. {
  884. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  885. foreach ($expected as $index => $expectedValue) {
  886. self::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  887. }
  888. }
  889. public static function provideIsLambda74Cases(): iterable
  890. {
  891. yield [
  892. [5 => true],
  893. '<?php $fn = fn() => [];',
  894. ];
  895. yield [
  896. [5 => true],
  897. '<?php $fn = fn () => [];',
  898. ];
  899. }
  900. /**
  901. * @param array<int, bool> $expected
  902. *
  903. * @dataProvider provideIsLambda80Cases
  904. *
  905. * @requires PHP 8.0
  906. */
  907. public function testIsLambda80(array $expected, string $source): void
  908. {
  909. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  910. foreach ($expected as $index => $expectedValue) {
  911. self::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  912. }
  913. }
  914. public static function provideIsLambda80Cases(): iterable
  915. {
  916. yield [
  917. [6 => true],
  918. '<?php
  919. $a = function (): ?static {
  920. return [];
  921. };',
  922. ];
  923. yield [
  924. [6 => true],
  925. '<?php
  926. $a = function (): static {
  927. return [];
  928. };',
  929. ];
  930. yield [
  931. [14 => true],
  932. '<?php
  933. $c = 4; //
  934. $a = function(
  935. $a,
  936. $b,
  937. ) use (
  938. $c,
  939. ) {
  940. echo $a + $b + $c;
  941. };
  942. $a(1,2);',
  943. ];
  944. }
  945. public function testIsLambdaInvalid(): void
  946. {
  947. $this->expectException(\LogicException::class);
  948. $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
  949. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  950. $tokensAnalyzer->isLambda(0);
  951. }
  952. /**
  953. * @param array<int, bool> $expected
  954. *
  955. * @dataProvider provideIsConstantInvocationCases
  956. */
  957. public function testIsConstantInvocation(array $expected, string $source): void
  958. {
  959. $this->doIsConstantInvocationTest($expected, $source);
  960. }
  961. public static function provideIsConstantInvocationCases(): iterable
  962. {
  963. yield [
  964. [3 => true],
  965. '<?php echo FOO;',
  966. ];
  967. yield [
  968. [4 => true],
  969. '<?php echo \FOO;',
  970. ];
  971. yield [
  972. [3 => false, 5 => false, 7 => true],
  973. '<?php echo Foo\Bar\BAR;',
  974. ];
  975. yield [
  976. [3 => true, 7 => true, 11 => true],
  977. '<?php echo FOO ? BAR : BAZ;',
  978. ];
  979. yield 'Bitwise & and bitwise |' => [
  980. [3 => true, 7 => true, 11 => true],
  981. '<?php echo FOO & BAR | BAZ;',
  982. ];
  983. yield 'Bitwise &' => [
  984. [3 => true],
  985. '<?php echo FOO & $bar;',
  986. ];
  987. yield [
  988. [5 => true],
  989. '<?php echo $foo[BAR];',
  990. ];
  991. yield [
  992. [3 => true, 5 => true],
  993. '<?php echo FOO[BAR];',
  994. ];
  995. yield [
  996. [1 => false, 3 => true, 6 => false, 8 => true],
  997. '<?php func(FOO, Bar\BAZ);',
  998. ];
  999. yield [
  1000. [4 => true, 8 => true],
  1001. '<?php if (FOO && BAR) {}',
  1002. ];
  1003. yield [
  1004. [3 => true, 7 => false, 9 => false, 11 => true],
  1005. '<?php return FOO * X\Y\BAR;',
  1006. ];
  1007. yield [
  1008. [3 => false, 11 => true, 16 => true, 20 => true],
  1009. '<?php function x() { yield FOO; yield FOO => BAR; }',
  1010. ];
  1011. yield [
  1012. [11 => true],
  1013. '<?php switch ($a) { case FOO: break; }',
  1014. ];
  1015. yield [
  1016. [3 => false],
  1017. '<?php namespace FOO;',
  1018. ];
  1019. yield [
  1020. [3 => false],
  1021. '<?php use FOO;',
  1022. ];
  1023. yield [
  1024. [5 => false, 7 => false, 9 => false],
  1025. '<?php use function FOO\BAR\BAZ;',
  1026. ];
  1027. yield [
  1028. [3 => false, 8 => false],
  1029. '<?php namespace X; const FOO = 1;',
  1030. ];
  1031. yield [
  1032. [3 => false],
  1033. '<?php class FOO {}',
  1034. ];
  1035. yield [
  1036. [3 => false],
  1037. '<?php interface FOO {}',
  1038. ];
  1039. yield [
  1040. [3 => false],
  1041. '<?php trait FOO {}',
  1042. ];
  1043. yield [
  1044. [3 => false, 7 => false],
  1045. '<?php class x extends FOO {}',
  1046. ];
  1047. yield [
  1048. [3 => false, 7 => false],
  1049. '<?php class x implements FOO {}',
  1050. ];
  1051. yield [
  1052. [3 => false, 7 => false, 10 => false, 13 => false],
  1053. '<?php class x implements FOO, BAR, BAZ {}',
  1054. ];
  1055. yield [
  1056. [3 => false, 9 => false],
  1057. '<?php class x { const FOO = 1; }',
  1058. ];
  1059. yield [
  1060. [3 => false, 9 => false],
  1061. '<?php class x { use FOO; }',
  1062. ];
  1063. yield [
  1064. [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
  1065. '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
  1066. ];
  1067. yield [
  1068. [3 => false, 6 => false, 11 => false, 17 => false],
  1069. '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
  1070. ];
  1071. yield [
  1072. [1 => false],
  1073. '<?php FOO();',
  1074. ];
  1075. yield [
  1076. [1 => false, 3 => false],
  1077. '<?php FOO::x();',
  1078. ];
  1079. yield [
  1080. [1 => false, 3 => false],
  1081. '<?php x::FOO();',
  1082. ];
  1083. yield [
  1084. [5 => false],
  1085. '<?php $foo instanceof FOO;',
  1086. ];
  1087. yield [
  1088. [9 => false],
  1089. '<?php try {} catch (FOO $e) {}',
  1090. ];
  1091. yield [
  1092. [4 => false],
  1093. '<?php "$foo[BAR]";',
  1094. ];
  1095. yield [
  1096. [5 => true],
  1097. '<?php "{$foo[BAR]}";',
  1098. ];
  1099. yield [
  1100. [1 => false, 6 => false],
  1101. '<?php FOO: goto FOO;',
  1102. ];
  1103. yield [
  1104. [1 => false, 3 => true, 7 => true],
  1105. '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
  1106. ];
  1107. yield [
  1108. [3 => false, 7 => false, 10 => false, 13 => false],
  1109. '<?php interface Foo extends Bar, Baz, Qux {}',
  1110. ];
  1111. yield [
  1112. [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
  1113. '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
  1114. ];
  1115. yield [
  1116. [3 => false, 8 => false],
  1117. '<?php function x(): FOO {}',
  1118. ];
  1119. yield [
  1120. [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
  1121. '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
  1122. ];
  1123. yield [
  1124. [6 => false, 16 => false, 21 => false],
  1125. '<?php
  1126. abstract class Baz
  1127. {
  1128. abstract public function test(): Foo;
  1129. }
  1130. ',
  1131. ];
  1132. yield [
  1133. [3 => false, 6 => false],
  1134. '<?php function x(?FOO $foo) {}',
  1135. ];
  1136. yield [
  1137. [3 => false, 9 => false],
  1138. '<?php function x(): ?FOO {}',
  1139. ];
  1140. yield [
  1141. [9 => false, 11 => false, 13 => false],
  1142. '<?php try {} catch (FOO|BAR|BAZ $e) {}',
  1143. ];
  1144. yield [
  1145. [3 => false, 11 => false, 16 => false],
  1146. '<?php interface Foo { public function bar(): Baz; }',
  1147. ];
  1148. yield [
  1149. [3 => false, 11 => false, 17 => false],
  1150. '<?php interface Foo { public function bar(): \Baz; }',
  1151. ];
  1152. yield [
  1153. [3 => false, 11 => false, 17 => false],
  1154. '<?php interface Foo { public function bar(): ?Baz; }',
  1155. ];
  1156. yield [
  1157. [3 => false, 11 => false, 18 => false],
  1158. '<?php interface Foo { public function bar(): ?\Baz; }',
  1159. ];
  1160. }
  1161. /**
  1162. * @param array<int, bool> $expected
  1163. *
  1164. * @dataProvider provideIsConstantInvocationPhp80Cases
  1165. *
  1166. * @requires PHP 8.0
  1167. */
  1168. public function testIsConstantInvocationPhp80(array $expected, string $source): void
  1169. {
  1170. $this->doIsConstantInvocationTest($expected, $source);
  1171. }
  1172. public static function provideIsConstantInvocationPhp80Cases(): iterable
  1173. {
  1174. yield 'abstract method return alternation' => [
  1175. [6 => false, 16 => false, 21 => false, 23 => false],
  1176. '<?php
  1177. abstract class Baz
  1178. {
  1179. abstract public function test(): Foo|Bar;
  1180. }
  1181. ',
  1182. ];
  1183. yield 'function return alternation' => [
  1184. [3 => false, 8 => false, 10 => false],
  1185. '<?php function test(): Foo|Bar {}',
  1186. ];
  1187. yield 'nullsafe operator' => [
  1188. [3 => false, 5 => false],
  1189. '<?php $a?->b?->c;',
  1190. ];
  1191. yield 'non-capturing catch' => [
  1192. [9 => false],
  1193. '<?php try {} catch (Exception) {}',
  1194. ];
  1195. yield 'non-capturing catch 2' => [
  1196. [10 => false],
  1197. '<?php try {} catch (\Exception) {}',
  1198. ];
  1199. yield 'non-capturing multiple catch' => [
  1200. [9 => false, 13 => false],
  1201. '<?php try {} catch (Foo | Bar) {}',
  1202. ];
  1203. yield 'attribute 1' => [
  1204. [2 => false, 5 => false, 10 => false],
  1205. '<?php #[Foo, Bar] function foo() {}',
  1206. ];
  1207. yield 'attribute 2' => [
  1208. [2 => false, 7 => false, 14 => false],
  1209. '<?php #[Foo(), Bar()] function foo() {}',
  1210. ];
  1211. yield [
  1212. [2 => false, 9 => false],
  1213. '<?php #[Foo()] function foo() {}',
  1214. ];
  1215. yield [
  1216. [3 => false, 10 => false],
  1217. '<?php #[\Foo()] function foo() {}',
  1218. ];
  1219. yield [
  1220. [2 => false, 4 => false, 11 => false],
  1221. '<?php #[A\Foo()] function foo() {}',
  1222. ];
  1223. yield [
  1224. [3 => false, 5 => false, 12 => false],
  1225. '<?php #[\A\Foo()] function foo() {}',
  1226. ];
  1227. yield 'multiple type catch with variable' => [
  1228. [5 => false, 15 => false, 18 => false],
  1229. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException $e) {}',
  1230. ];
  1231. yield 'multiple type catch without variable 1' => [
  1232. [5 => false, 15 => false, 18 => false],
  1233. '<?php try { foo(); } catch(\InvalidArgumentException|\LogicException) {}',
  1234. ];
  1235. yield 'multiple type catch without variable 2' => [
  1236. [5 => false, 15 => false, 17 => false, 19 => false, 21 => false, 24 => false, 27 => false],
  1237. '<?php try { foo(); } catch(\D|Z|A\B|\InvalidArgumentException|\LogicException) {}',
  1238. ];
  1239. yield 'multiple type catch without variable 3' => [
  1240. [5 => false, 14 => false, 16 => false, 19 => false, 22 => false],
  1241. '<?php try { foo(); } catch(A\B|\InvalidArgumentException|\LogicException) {}',
  1242. ];
  1243. yield 'attribute before' => [
  1244. [4 => false, 6 => false, 8 => false, 13 => false, 17 => false, 23 => false, 25 => false],
  1245. '<?php
  1246. use Psr\Log\LoggerInterface;
  1247. function f( #[Target(\'xxx\')] LoggerInterface|null $logger) {}
  1248. ',
  1249. ];
  1250. }
  1251. /**
  1252. * @param array<int, bool> $expected
  1253. *
  1254. * @dataProvider provideIsConstantInvocationPhp81Cases
  1255. *
  1256. * @requires PHP 8.1
  1257. */
  1258. public function testIsConstantInvocationPhp81(array $expected, string $source): void
  1259. {
  1260. $this->doIsConstantInvocationTest($expected, $source);
  1261. }
  1262. public static function provideIsConstantInvocationPhp81Cases(): iterable
  1263. {
  1264. yield [
  1265. [5 => false, 15 => false],
  1266. '<?php
  1267. abstract class Baz
  1268. {
  1269. final public const Y = "i";
  1270. }
  1271. ',
  1272. ];
  1273. yield [
  1274. [4 => false, 12 => false, 23 => false],
  1275. '<?php
  1276. class Test {
  1277. public function __construct(
  1278. public $prop = new Foo,
  1279. ) {}
  1280. }
  1281. ',
  1282. ];
  1283. yield 'intersection' => [
  1284. [3 => false, 9 => false, 11 => false],
  1285. '<?php function foo(): \Foo&Bar {}',
  1286. ];
  1287. yield 'abstract method return intersection' => [
  1288. [6 => false, 16 => false, 21 => false, 23 => false, 25 => false, 27 => false, 29 => false],
  1289. '<?php
  1290. abstract class Baz
  1291. {
  1292. abstract public function foo(): Foo&Bar1&Bar2&Bar3&Bar4;
  1293. }
  1294. ',
  1295. ];
  1296. }
  1297. /**
  1298. * @param array<int, bool> $expected
  1299. *
  1300. * @dataProvider provideIsConstantInvocationPhp82Cases
  1301. *
  1302. * @requires PHP 8.2
  1303. */
  1304. public function testIsConstantInvocationPhp82(array $expected, string $source): void
  1305. {
  1306. $this->doIsConstantInvocationTest($expected, $source);
  1307. }
  1308. /**
  1309. * @return iterable<array{array<int, bool>, string}>
  1310. */
  1311. public static function provideIsConstantInvocationPhp82Cases(): iterable
  1312. {
  1313. yield [
  1314. [3 => false, 11 => false, 13 => false, 17 => false, 20 => false, 23 => false, 25 => false, 28 => false, 31 => false, 33 => false, 35 => false, 39 => false, 42 => false, 44 => false],
  1315. '<?php class Foo { public (\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var; }',
  1316. ];
  1317. yield [
  1318. [3 => false, 8 => false, 10 => false, 14 => false, 17 => false, 20 => false, 22 => false, 25 => false, 28 => false, 30 => false, 32 => false, 36 => false, 39 => false, 41 => false],
  1319. '<?php function foo ((\A&B)|(C&\D)|E\F|\G|(A&H\I)|(A&\J\K) $var) {}',
  1320. ];
  1321. }
  1322. /**
  1323. * @param array<int, bool> $expected
  1324. *
  1325. * @dataProvider provideIsConstantInvocationPhp83Cases
  1326. *
  1327. * @requires PHP 8.3
  1328. */
  1329. public function testIsConstantInvocationPhp83(array $expected, string $source): void
  1330. {
  1331. $this->doIsConstantInvocationTest($expected, $source);
  1332. }
  1333. /**
  1334. * @return iterable<array{array<int, bool>, string}>
  1335. */
  1336. public static function provideIsConstantInvocationPhp83Cases(): iterable
  1337. {
  1338. yield [
  1339. [3 => false, 11 => false, 13 => false, 17 => true],
  1340. '<?php class Foo { public const string FOO = BAR; }',
  1341. ];
  1342. yield [
  1343. [3 => false, 11 => false, 13 => false, 17 => true],
  1344. '<?php class Foo { public const int FOO = BAR; }',
  1345. ];
  1346. yield [
  1347. [3 => false, 11 => false, 13 => false, 17 => true],
  1348. '<?php class Foo { public const bool FOO = BAR; }',
  1349. ];
  1350. yield [
  1351. [3 => false, 13 => false, 18 => true],
  1352. '<?php class Foo { public const array FOO = [BAR]; }',
  1353. ];
  1354. yield [
  1355. [3 => false, 11 => false, 13 => false, 17 => true],
  1356. '<?php class Foo { public const A FOO = BAR; }',
  1357. ];
  1358. yield [
  1359. [3 => false, 11 => false, 13 => false, 15 => false, 19 => true],
  1360. '<?php class Foo { public const A|B FOO = BAR; }',
  1361. ];
  1362. yield [
  1363. [3 => false, 11 => false, 13 => false, 15 => false, 19 => true],
  1364. '<?php class Foo { public const A&B FOO = BAR; }',
  1365. ];
  1366. yield [
  1367. [3 => false, 12 => false, 14 => false, 17 => false, 19 => false, 23 => true],
  1368. '<?php class Foo { public const (A&B)|C FOO = BAR; }',
  1369. ];
  1370. yield [
  1371. [3 => false, 12 => false, 14 => false, 18 => false, 20 => false, 23 => false, 27 => true],
  1372. '<?php class Foo { public const (A&B)|(C&D) FOO = BAR; }',
  1373. ];
  1374. yield [
  1375. [3 => false, 12 => false, 15 => false, 17 => false, 21 => false, 23 => false, 25 => false, 28 => false, 32 => true],
  1376. '<?php class Foo { public const (A&\B\C)|(D\E&F) FOO = BAR; }',
  1377. ];
  1378. }
  1379. public function testIsConstantInvocationInvalid(): void
  1380. {
  1381. $this->expectException(\LogicException::class);
  1382. $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
  1383. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  1384. $tokensAnalyzer->isConstantInvocation(0);
  1385. }
  1386. /**
  1387. * @requires PHP 8.0
  1388. */
  1389. public function testIsConstantInvocationForNullSafeObjectOperator(): void
  1390. {
  1391. $tokens = Tokens::fromCode('<?php $a?->b?->c;');
  1392. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1393. foreach ($tokens as $index => $token) {
  1394. if (!$token->isGivenKind(T_STRING)) {
  1395. continue;
  1396. }
  1397. self::assertFalse($tokensAnalyzer->isConstantInvocation($index));
  1398. }
  1399. }
  1400. /**
  1401. * @param array<int, bool> $expected
  1402. *
  1403. * @dataProvider provideIsUnarySuccessorOperatorCases
  1404. */
  1405. public function testIsUnarySuccessorOperator(array $expected, string $source): void
  1406. {
  1407. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1408. foreach ($expected as $index => $isUnary) {
  1409. self::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  1410. if ($isUnary) {
  1411. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1412. self::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1413. }
  1414. }
  1415. }
  1416. public static function provideIsUnarySuccessorOperatorCases(): iterable
  1417. {
  1418. yield [
  1419. [2 => true],
  1420. '<?php $a++;',
  1421. ];
  1422. yield [
  1423. [2 => true],
  1424. '<?php $a--;',
  1425. ];
  1426. yield [
  1427. [3 => true],
  1428. '<?php $a ++;',
  1429. ];
  1430. yield [
  1431. [2 => true, 4 => false],
  1432. '<?php $a++ + 1;',
  1433. ];
  1434. yield [
  1435. [5 => true],
  1436. '<?php ${"a"}++;',
  1437. ];
  1438. yield [
  1439. [4 => true],
  1440. '<?php $foo->bar++;',
  1441. ];
  1442. yield [
  1443. [6 => true],
  1444. '<?php $foo->{"bar"}++;',
  1445. ];
  1446. yield 'array access' => [
  1447. [5 => true],
  1448. '<?php $a["foo"]++;',
  1449. ];
  1450. yield 'array curly access' => [
  1451. [5 => true],
  1452. '<?php $a{"foo"}++;',
  1453. ];
  1454. }
  1455. /**
  1456. * @param list<int> $expected
  1457. *
  1458. * @dataProvider provideIsUnaryPredecessorOperatorCases
  1459. */
  1460. public function testIsUnaryPredecessorOperator(array $expected, string $source): void
  1461. {
  1462. $tokens = Tokens::fromCode($source);
  1463. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1464. foreach ($tokens as $index => $token) {
  1465. $expect = \in_array($index, $expected, true);
  1466. self::assertSame(
  1467. $expect,
  1468. $tokensAnalyzer->isUnaryPredecessorOperator($index),
  1469. sprintf('Expected %sunary predecessor operator, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1470. );
  1471. if ($expect) {
  1472. self::assertFalse(
  1473. $tokensAnalyzer->isUnarySuccessorOperator($index),
  1474. sprintf('Expected no unary successor operator, got @ %d "%s".', $index, var_export($token, true))
  1475. );
  1476. self::assertFalse(
  1477. $tokensAnalyzer->isBinaryOperator($index),
  1478. sprintf('Expected no binary operator, got @ %d "%s".', $index, var_export($token, true))
  1479. );
  1480. }
  1481. }
  1482. }
  1483. public static function provideIsUnaryPredecessorOperatorCases(): iterable
  1484. {
  1485. yield [
  1486. [1],
  1487. '<?php ++$a;',
  1488. ];
  1489. yield [
  1490. [1],
  1491. '<?php --$a;',
  1492. ];
  1493. yield [
  1494. [1],
  1495. '<?php -- $a;',
  1496. ];
  1497. yield [
  1498. [5],
  1499. '<?php $a + ++$b;',
  1500. ];
  1501. yield [
  1502. [1, 2],
  1503. '<?php !!$a;',
  1504. ];
  1505. yield [
  1506. [5],
  1507. '<?php $a = &$b;',
  1508. ];
  1509. yield [
  1510. [3],
  1511. '<?php function &foo() {}',
  1512. ];
  1513. yield [
  1514. [1],
  1515. '<?php @foo();',
  1516. ];
  1517. yield [
  1518. [3, 8],
  1519. '<?php foo(+ $a, -$b);',
  1520. ];
  1521. yield [
  1522. [5, 11, 17],
  1523. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  1524. ];
  1525. yield [
  1526. [8],
  1527. '<?php function foo($a, ...$b) {}',
  1528. ];
  1529. yield [
  1530. [5, 6],
  1531. '<?php function foo(&...$b) {}',
  1532. ];
  1533. yield [
  1534. [7],
  1535. '<?php function foo(array ...$b) {}',
  1536. ];
  1537. yield [
  1538. [7],
  1539. '<?php $foo = function(...$a) {};',
  1540. ];
  1541. yield [
  1542. [10],
  1543. '<?php $foo = function($a, ...$b) {};',
  1544. ];
  1545. yield [
  1546. [9],
  1547. '<?php $foo = function(int &$x) {};',
  1548. ];
  1549. yield [
  1550. [9],
  1551. '<?php $foo = fn(int &$x) => null;',
  1552. ];
  1553. yield [
  1554. [],
  1555. '<?php fn() => A_CONSTANT & $object->property;',
  1556. ];
  1557. }
  1558. /**
  1559. * @param list<int> $expected
  1560. *
  1561. * @dataProvider provideIsBinaryOperatorCases
  1562. */
  1563. public function testIsBinaryOperator(array $expected, string $source): void
  1564. {
  1565. $tokens = Tokens::fromCode($source);
  1566. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1567. foreach ($tokens as $index => $token) {
  1568. $expect = \in_array($index, $expected, true);
  1569. self::assertSame(
  1570. $expect,
  1571. $tokensAnalyzer->isBinaryOperator($index),
  1572. sprintf('Expected %sbinary operator, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1573. );
  1574. if ($expect) {
  1575. self::assertFalse(
  1576. $tokensAnalyzer->isUnarySuccessorOperator($index),
  1577. sprintf('Expected no unary successor operator, got @ %d "%s".', $index, var_export($token, true))
  1578. );
  1579. self::assertFalse(
  1580. $tokensAnalyzer->isUnaryPredecessorOperator($index),
  1581. sprintf('Expected no unary predecessor operator, got @ %d "%s".', $index, var_export($token, true))
  1582. );
  1583. }
  1584. }
  1585. }
  1586. public static function provideIsBinaryOperatorCases(): iterable
  1587. {
  1588. yield [
  1589. [8],
  1590. '<?php echo $a[1] + 1;',
  1591. ];
  1592. yield [
  1593. [8],
  1594. '<?php echo $a{1} + 1;',
  1595. ];
  1596. yield [
  1597. [3],
  1598. '<?php $a .= $b; ?>',
  1599. ];
  1600. yield [
  1601. [3],
  1602. '<?php $a . \'a\' ?>',
  1603. ];
  1604. yield [
  1605. [3],
  1606. '<?php $a &+ $b;',
  1607. ];
  1608. yield [
  1609. [3],
  1610. '<?php $a && $b;',
  1611. ];
  1612. yield [
  1613. [3],
  1614. '<?php $a & $b;',
  1615. ];
  1616. yield [
  1617. [4],
  1618. '<?php [] + [];',
  1619. ];
  1620. yield [
  1621. [3],
  1622. '<?php $a + $b;',
  1623. ];
  1624. yield [
  1625. [3],
  1626. '<?php 1 + $b;',
  1627. ];
  1628. yield [
  1629. [3],
  1630. '<?php 0.2 + $b;',
  1631. ];
  1632. yield [
  1633. [6],
  1634. '<?php $a[1] + $b;',
  1635. ];
  1636. yield [
  1637. [3],
  1638. '<?php FOO + $b;',
  1639. ];
  1640. yield [
  1641. [5],
  1642. '<?php foo() + $b;',
  1643. ];
  1644. yield [
  1645. [6],
  1646. '<?php ${"foo"} + $b;',
  1647. ];
  1648. yield [
  1649. [2],
  1650. '<?php $a+$b;',
  1651. ];
  1652. yield [
  1653. [5],
  1654. '<?php $a /* foo */ + /* bar */ $b;',
  1655. ];
  1656. yield [
  1657. [3],
  1658. '<?php $a =
  1659. $b;',
  1660. ];
  1661. yield [
  1662. [3],
  1663. '<?php $a
  1664. = $b;',
  1665. ];
  1666. yield [
  1667. [3, 9],
  1668. '<?php $a = array("b" => "c", );',
  1669. ];
  1670. yield [
  1671. [3],
  1672. '<?php $a * -$b;',
  1673. ];
  1674. yield [
  1675. [3, 8],
  1676. '<?php $a = -2 / +5;',
  1677. ];
  1678. yield [
  1679. [3, 5 => false],
  1680. '<?php $a = &$b;',
  1681. ];
  1682. yield [
  1683. [4],
  1684. '<?php $a++ + $b;',
  1685. ];
  1686. yield [
  1687. [3, 7],
  1688. '<?php $a = FOO & $bar;',
  1689. ];
  1690. yield [
  1691. [3],
  1692. '<?php __LINE__ - 1;',
  1693. ];
  1694. yield [
  1695. [5],
  1696. '<?php `echo 1` + 1;',
  1697. ];
  1698. yield [
  1699. [3],
  1700. '<?php $a ** $b;',
  1701. ];
  1702. yield [
  1703. [3],
  1704. '<?php $a **= $b;',
  1705. ];
  1706. yield [
  1707. [3],
  1708. '<?php $a = "{$value}-{$theSwitch}";',
  1709. ];
  1710. yield [
  1711. [],
  1712. '<?=$path?>-<?=$id?>',
  1713. ];
  1714. $operators = [
  1715. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  1716. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
  1717. ];
  1718. foreach ($operators as $operator) {
  1719. yield [
  1720. [3],
  1721. '<?php $a '.$operator.' $b;',
  1722. ];
  1723. }
  1724. yield [
  1725. [3],
  1726. '<?php $a <=> $b;',
  1727. ];
  1728. yield [
  1729. [3],
  1730. '<?php $a ?? $b;',
  1731. ];
  1732. yield [
  1733. [],
  1734. '<?php try {} catch (A | B $e) {}',
  1735. ];
  1736. yield [
  1737. [3],
  1738. '<?php $a ??= $b;',
  1739. ];
  1740. yield [
  1741. [5, 11],
  1742. '<?php fn() => $object->property & A_CONSTANT;',
  1743. ];
  1744. }
  1745. /**
  1746. * @dataProvider provideIsArrayCases
  1747. */
  1748. public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
  1749. {
  1750. $tokens = Tokens::fromCode($source);
  1751. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1752. self::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  1753. self::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  1754. }
  1755. public static function provideIsArrayCases(): iterable
  1756. {
  1757. yield [
  1758. '<?php
  1759. array("a" => 1);
  1760. ',
  1761. 2,
  1762. ];
  1763. yield [
  1764. '<?php
  1765. ["a" => 2];
  1766. ',
  1767. 2, false,
  1768. ];
  1769. yield [
  1770. '<?php
  1771. array(
  1772. "a" => 3
  1773. );
  1774. ',
  1775. 2, true,
  1776. ];
  1777. yield [
  1778. '<?php
  1779. [
  1780. "a" => 4
  1781. ];
  1782. ',
  1783. 2, true,
  1784. ];
  1785. yield [
  1786. '<?php
  1787. array(
  1788. "a" => array(5, 6, 7),
  1789. 8 => new \Exception(\'Hello\')
  1790. );
  1791. ',
  1792. 2, true,
  1793. ];
  1794. yield [
  1795. // mix short array syntax
  1796. '<?php
  1797. array(
  1798. "a" => [9, 10, 11],
  1799. 12 => new \Exception(\'Hello\')
  1800. );
  1801. ',
  1802. 2, true,
  1803. ];
  1804. // Windows/Max EOL testing
  1805. yield [
  1806. "<?php\r\narray('a' => 13);\r\n",
  1807. 1,
  1808. ];
  1809. yield [
  1810. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  1811. 2, true,
  1812. ];
  1813. }
  1814. /**
  1815. * @param list<int> $tokenIndexes
  1816. *
  1817. * @dataProvider provideIsArray71Cases
  1818. */
  1819. public function testIsArray71(string $source, array $tokenIndexes): void
  1820. {
  1821. $tokens = Tokens::fromCode($source);
  1822. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1823. foreach ($tokens as $index => $token) {
  1824. $expect = \in_array($index, $tokenIndexes, true);
  1825. self::assertSame(
  1826. $expect,
  1827. $tokensAnalyzer->isArray($index),
  1828. sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1829. );
  1830. }
  1831. }
  1832. public static function provideIsArray71Cases(): iterable
  1833. {
  1834. yield [
  1835. '<?php
  1836. [$a] = $z;
  1837. ["a" => $a, "b" => $b] = $array;
  1838. $c = [$d, $e] = $array[$a];
  1839. [[$a, $b], [$c, $d]] = $d;
  1840. $array = []; $d = array();
  1841. ',
  1842. [76, 84],
  1843. ];
  1844. }
  1845. /**
  1846. * @param array<int, bool> $expected
  1847. *
  1848. * @dataProvider provideIsBinaryOperator80Cases
  1849. *
  1850. * @requires PHP 8.0
  1851. */
  1852. public function testIsBinaryOperator80(array $expected, string $source): void
  1853. {
  1854. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1855. foreach ($expected as $index => $isBinary) {
  1856. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1857. if ($isBinary) {
  1858. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1859. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1860. }
  1861. }
  1862. }
  1863. public static function provideIsBinaryOperator80Cases(): iterable
  1864. {
  1865. yield [
  1866. [6 => false],
  1867. '<?php function foo(array|string $x) {}',
  1868. ];
  1869. yield [
  1870. [6 => false],
  1871. '<?php function foo(string|array $x) {}',
  1872. ];
  1873. yield [
  1874. [6 => false],
  1875. '<?php function foo(int|callable $x) {}',
  1876. ];
  1877. yield [
  1878. [6 => false],
  1879. '<?php function foo(callable|int $x) {}',
  1880. ];
  1881. yield [
  1882. [6 => false],
  1883. '<?php function foo(bool|int &$x) {}',
  1884. ];
  1885. }
  1886. /**
  1887. * @param array<int, bool> $expected
  1888. *
  1889. * @dataProvider provideIsBinaryOperator81Cases
  1890. *
  1891. * @requires PHP 8.1
  1892. */
  1893. public function testIsBinaryOperator81(array $expected, string $source): void
  1894. {
  1895. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1896. foreach ($expected as $index => $isBinary) {
  1897. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1898. if ($isBinary) {
  1899. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1900. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1901. }
  1902. }
  1903. }
  1904. public static function provideIsBinaryOperator81Cases(): iterable
  1905. {
  1906. yield 'type intersection' => [
  1907. [6 => false],
  1908. '<?php function foo(array&string $x) {}',
  1909. ];
  1910. }
  1911. /**
  1912. * @param array<int, bool> $expected
  1913. *
  1914. * @dataProvider provideIsBinaryOperator82Cases
  1915. *
  1916. * @requires PHP 8.2
  1917. */
  1918. public function testIsBinaryOperator82(array $expected, string $source): void
  1919. {
  1920. $tokens = Tokens::fromCode($source);
  1921. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1922. foreach ($tokens as $index => $token) {
  1923. $isBinary = isset($expected[$index]);
  1924. self::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1925. if ($isBinary) {
  1926. self::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1927. self::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1928. }
  1929. }
  1930. }
  1931. public static function provideIsBinaryOperator82Cases(): iterable
  1932. {
  1933. yield [
  1934. [],
  1935. '<?php class Dnf { public static I|(P&S11) $f2;}',
  1936. ];
  1937. yield [
  1938. [],
  1939. '<?php function Foo((A&B)|I $x): (X&Z)|(p\f\G&Y\Z)|z { return foo();}',
  1940. ];
  1941. $particularEndOfFile = 'A|(B&C); }';
  1942. yield sprintf('block "%s" at the end of file that is a type', $particularEndOfFile) => [
  1943. [],
  1944. '<?php abstract class A { abstract function foo(): '.$particularEndOfFile,
  1945. ];
  1946. yield sprintf('block "%s" at the end of file that is not a type', $particularEndOfFile) => [
  1947. [12 => true, 15 => true],
  1948. '<?php function foo() { return '.$particularEndOfFile,
  1949. ];
  1950. }
  1951. /**
  1952. * @dataProvider provideArrayExceptionsCases
  1953. */
  1954. public function testIsNotArray(string $source, int $tokenIndex): void
  1955. {
  1956. $tokens = Tokens::fromCode($source);
  1957. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1958. self::assertFalse($tokensAnalyzer->isArray($tokenIndex));
  1959. }
  1960. /**
  1961. * @dataProvider provideArrayExceptionsCases
  1962. */
  1963. public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
  1964. {
  1965. $this->expectException(\InvalidArgumentException::class);
  1966. $tokens = Tokens::fromCode($source);
  1967. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1968. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  1969. }
  1970. public static function provideArrayExceptionsCases(): iterable
  1971. {
  1972. yield ['<?php $a;', 1];
  1973. yield ["<?php\n \$a = (0+1); // [0,1]", 4];
  1974. yield ['<?php $text = "foo $bbb[0] bar";', 8];
  1975. yield ['<?php $text = "foo ${aaa[123]} bar";', 9];
  1976. }
  1977. public function testIsBlockMultilineException(): void
  1978. {
  1979. $this->expectException(\LogicException::class);
  1980. $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
  1981. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1982. $tokensAnalyzer->isBlockMultiline($tokens, 1);
  1983. }
  1984. /**
  1985. * @dataProvider provideIsBlockMultilineCases
  1986. */
  1987. public function testIsBlockMultiline(bool $isBlockMultiline, string $source, int $tokenIndex): void
  1988. {
  1989. $tokens = Tokens::fromCode($source);
  1990. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1991. self::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
  1992. }
  1993. public static function provideIsBlockMultilineCases(): iterable
  1994. {
  1995. yield [
  1996. false,
  1997. '<?php foo(1, 2, 3);',
  1998. 2,
  1999. ];
  2000. yield [
  2001. true,
  2002. '<?php foo(1,
  2003. 2,
  2004. 3
  2005. );',
  2006. 2,
  2007. ];
  2008. yield [
  2009. false,
  2010. '<?php foo(1, "Multi
  2011. string", 2, 3);',
  2012. 2,
  2013. ];
  2014. yield [
  2015. false,
  2016. '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
  2017. "a",
  2018. "b"
  2019. ), 2, 3);',
  2020. 2,
  2021. ];
  2022. }
  2023. /**
  2024. * @param array{visibility: ?int, static: bool, abstract: bool, final: bool} $expected
  2025. *
  2026. * @dataProvider provideGetFunctionPropertiesCases
  2027. */
  2028. public function testGetFunctionProperties(string $source, int $index, array $expected): void
  2029. {
  2030. $tokens = Tokens::fromCode($source);
  2031. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2032. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  2033. self::assertSame($expected, $attributes);
  2034. }
  2035. public static function provideGetFunctionPropertiesCases(): iterable
  2036. {
  2037. $defaultAttributes = [
  2038. 'visibility' => null,
  2039. 'static' => false,
  2040. 'abstract' => false,
  2041. 'final' => false,
  2042. ];
  2043. $template = '
  2044. <?php
  2045. class TestClass {
  2046. %s function a() {
  2047. //
  2048. }
  2049. }
  2050. ';
  2051. $cases = [];
  2052. $attributes = $defaultAttributes;
  2053. $attributes['visibility'] = T_PRIVATE;
  2054. $cases[] = [sprintf($template, 'private'), 10, $attributes];
  2055. $attributes = $defaultAttributes;
  2056. $attributes['visibility'] = T_PUBLIC;
  2057. $cases[] = [sprintf($template, 'public'), 10, $attributes];
  2058. $attributes = $defaultAttributes;
  2059. $attributes['visibility'] = T_PROTECTED;
  2060. $cases[] = [sprintf($template, 'protected'), 10, $attributes];
  2061. $attributes = $defaultAttributes;
  2062. $attributes['visibility'] = null;
  2063. $attributes['static'] = true;
  2064. $cases[] = [sprintf($template, 'static'), 10, $attributes];
  2065. $attributes = $defaultAttributes;
  2066. $attributes['visibility'] = T_PUBLIC;
  2067. $attributes['static'] = true;
  2068. $attributes['final'] = true;
  2069. $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
  2070. $attributes = $defaultAttributes;
  2071. $attributes['visibility'] = null;
  2072. $attributes['abstract'] = true;
  2073. $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
  2074. $attributes = $defaultAttributes;
  2075. $attributes['visibility'] = T_PUBLIC;
  2076. $attributes['abstract'] = true;
  2077. $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
  2078. $attributes = $defaultAttributes;
  2079. $cases[] = [sprintf($template, ''), 8, $attributes];
  2080. return $cases;
  2081. }
  2082. public function testIsWhilePartOfDoWhile(): void
  2083. {
  2084. $source =
  2085. <<<'SRC'
  2086. <?php
  2087. // `not do`
  2088. while(false) {
  2089. }
  2090. while (false);
  2091. while (false)?>
  2092. <?php
  2093. if(false){
  2094. }while(false);
  2095. if(false){
  2096. }while(false)?><?php
  2097. while(false){}while(false){}
  2098. while ($i <= 10):
  2099. echo $i;
  2100. $i++;
  2101. endwhile;
  2102. ?>
  2103. <?php while(false): ?>
  2104. <?php endwhile ?>
  2105. <?php
  2106. // `do`
  2107. do{
  2108. } while(false);
  2109. do{
  2110. } while(false)?>
  2111. <?php
  2112. if (false){}do{}while(false);
  2113. // `not do`, `do`
  2114. if(false){}while(false){}do{}while(false);
  2115. SRC;
  2116. $expected = [
  2117. 3 => false,
  2118. 12 => false,
  2119. 19 => false,
  2120. 34 => false,
  2121. 47 => false,
  2122. 53 => false,
  2123. 59 => false,
  2124. 66 => false,
  2125. 91 => false,
  2126. 112 => true,
  2127. 123 => true,
  2128. 139 => true,
  2129. 153 => false,
  2130. 162 => true,
  2131. ];
  2132. $tokens = Tokens::fromCode($source);
  2133. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2134. foreach ($tokens as $index => $token) {
  2135. if (!$token->isGivenKind(T_WHILE)) {
  2136. continue;
  2137. }
  2138. $isExpected = $expected[$index] ?? null;
  2139. self::assertSame(
  2140. $isExpected,
  2141. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  2142. sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $isExpected ? '' : 'not ')
  2143. );
  2144. }
  2145. }
  2146. /**
  2147. * @dataProvider provideIsEnumCaseCases
  2148. *
  2149. * @param array<int, bool> $expected
  2150. *
  2151. * @requires PHP 8.1
  2152. */
  2153. public function testIsEnumCase(string $source, array $expected): void
  2154. {
  2155. $tokens = Tokens::fromCode($source);
  2156. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2157. foreach ($tokens as $index => $token) {
  2158. if (!$token->isGivenKind(T_CASE)) {
  2159. try {
  2160. $tokensAnalyzer->isEnumCase($index);
  2161. self::fail('TokensAnalyzer::isEnumCase() did not throw LogicException.');
  2162. } catch (\Throwable $e) {
  2163. self::assertInstanceOf(\LogicException::class, $e);
  2164. self::assertMatchesRegularExpression('/^No T_CASE given at index \d+, got \S+ instead\.$/', $e->getMessage());
  2165. }
  2166. continue;
  2167. }
  2168. self::assertSame($expected[$index], $tokensAnalyzer->isEnumCase($index));
  2169. }
  2170. }
  2171. public static function provideIsEnumCaseCases(): iterable
  2172. {
  2173. yield 'switch only' => [
  2174. '<?php
  2175. function bar(string $a): string
  2176. {
  2177. switch ($a) {
  2178. case \'one\':
  2179. return $a;
  2180. case \'two\':
  2181. default:
  2182. return strtoupper($a);
  2183. }
  2184. }
  2185. ',
  2186. [
  2187. 23 => false,
  2188. 33 => false,
  2189. ],
  2190. ];
  2191. yield 'pure enum' => [
  2192. '<?php
  2193. enum Foo
  2194. {
  2195. case One;
  2196. case Two;
  2197. }
  2198. ',
  2199. [
  2200. 7 => true,
  2201. 12 => true,
  2202. ],
  2203. ];
  2204. yield 'pure enum with switch' => [
  2205. '<?php
  2206. enum Foo
  2207. {
  2208. case One;
  2209. case Two;
  2210. public static function getLowerName(self $instance): string
  2211. {
  2212. switch ($instance->name) {
  2213. case \'One\':
  2214. case \'Two\':
  2215. return strtolower($instance->name);
  2216. }
  2217. }
  2218. }
  2219. ',
  2220. [
  2221. 7 => true,
  2222. 12 => true,
  2223. 45 => false,
  2224. 50 => false,
  2225. ],
  2226. ];
  2227. yield 'backed enum' => [
  2228. '<?php
  2229. enum Suit: string
  2230. {
  2231. case Hearts = \'hearts\';
  2232. case Spades = \'spades\';
  2233. case Clubs = \'clubs\';
  2234. case Diamonds = \'diamonds\';
  2235. }
  2236. ',
  2237. [
  2238. 10 => true,
  2239. 19 => true,
  2240. 28 => true,
  2241. 37 => true,
  2242. ],
  2243. ];
  2244. yield 'backed enum with switch' => [
  2245. '<?php
  2246. enum Suit: string
  2247. {
  2248. case Hearts = \'hearts\';
  2249. case Spades = \'spades\';
  2250. case Clubs = \'clubs\';
  2251. case Diamonds = \'diamonds\';
  2252. public static function getUppercasedValue(self $instance): string
  2253. {
  2254. switch ($instance->value) {
  2255. case \'hearts\':
  2256. case \'spades\':
  2257. return strtoupper($instance->value);
  2258. default:
  2259. return $instance->value;
  2260. }
  2261. }
  2262. }
  2263. ',
  2264. [
  2265. 10 => true,
  2266. 19 => true,
  2267. 28 => true,
  2268. 37 => true,
  2269. 74 => false,
  2270. 79 => false,
  2271. ],
  2272. ];
  2273. }
  2274. /**
  2275. * @param array<int, list<int>>|list<int> $expected
  2276. *
  2277. * @dataProvider provideGetImportUseIndexesCases
  2278. */
  2279. public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
  2280. {
  2281. $tokens = Tokens::fromCode($input);
  2282. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2283. self::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  2284. }
  2285. public static function provideGetImportUseIndexesCases(): iterable
  2286. {
  2287. yield [
  2288. [1, 8],
  2289. '<?php use E\F?><?php use A\B;',
  2290. ];
  2291. yield [
  2292. [[1], [14], [29]],
  2293. '<?php
  2294. use T\A;
  2295. namespace A { use D\C; }
  2296. namespace b { use D\C; }
  2297. ',
  2298. true,
  2299. ];
  2300. yield [
  2301. [[1, 8]],
  2302. '<?php use D\B; use A\C?>',
  2303. true,
  2304. ];
  2305. yield [
  2306. [1, 8],
  2307. '<?php use D\B; use A\C?>',
  2308. ];
  2309. yield [
  2310. [7, 22],
  2311. '<?php
  2312. namespace A { use D\C; }
  2313. namespace b { use D\C; }
  2314. ',
  2315. ];
  2316. yield [
  2317. [3, 10, 34, 45, 54, 59, 77, 95],
  2318. <<<'EOF'
  2319. use Zoo\Bar;
  2320. use Foo\Bar;
  2321. use Foo\Zar\Baz;
  2322. <?php
  2323. use Foo\Bar;
  2324. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  2325. use Foo\Bir as FBB;
  2326. use Foo\Zar\Baz;
  2327. use SomeClass;
  2328. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  2329. use Zoo\Bar;
  2330. $a = new someclass();
  2331. use Zoo\Tar;
  2332. class AnnotatedClass
  2333. {
  2334. }
  2335. EOF,
  2336. ];
  2337. yield [
  2338. [1, 22, 41],
  2339. '<?php
  2340. use some\a\{ClassA, ClassB, ClassC as C};
  2341. use function some\a\{fn_a, fn_b, fn_c};
  2342. use const some\a\{ConstA, ConstB, ConstC};
  2343. ',
  2344. ];
  2345. yield [
  2346. [[1, 22, 41]],
  2347. '<?php
  2348. use some\a\{ClassA, ClassB, ClassC as C};
  2349. use function some\a\{fn_a, fn_b, fn_c};
  2350. use const some\a\{ConstA, ConstB, ConstC};
  2351. ',
  2352. true,
  2353. ];
  2354. yield [
  2355. [1, 23, 43],
  2356. '<?php
  2357. use some\a\{ClassA, ClassB, ClassC as C,};
  2358. use function some\a\{fn_a, fn_b, fn_c,};
  2359. use const some\a\{ConstA, ConstB, ConstC,};
  2360. ',
  2361. ];
  2362. yield [
  2363. [[1, 23, 43]],
  2364. '<?php
  2365. use some\a\{ClassA, ClassB, ClassC as C,};
  2366. use function some\a\{fn_a, fn_b, fn_c,};
  2367. use const some\a\{ConstA, ConstB, ConstC,};
  2368. ',
  2369. true,
  2370. ];
  2371. }
  2372. public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
  2373. {
  2374. $source = '<?php
  2375. class MyTestWithAnonymousClass extends TestCase
  2376. {
  2377. public function setUp()
  2378. {
  2379. $provider = new class(function () {}) {};
  2380. }
  2381. public function testSomethingWithMoney(
  2382. Money $amount
  2383. ) {
  2384. $a = new class(function () {
  2385. new class(function () {
  2386. new class(function () {})
  2387. {
  2388. const A=1;
  2389. };
  2390. })
  2391. {
  2392. const B=1;
  2393. public function foo() {
  2394. $c = new class() {const AA=3;};
  2395. $d = new class {const AB=3;};
  2396. }
  2397. };
  2398. })
  2399. {
  2400. const C=1;
  2401. };
  2402. }
  2403. }';
  2404. $tokens = Tokens::fromCode($source);
  2405. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2406. $elements = $tokensAnalyzer->getClassyElements();
  2407. self::assertSame([
  2408. 13 => [
  2409. 'classIndex' => 1,
  2410. 'token' => $tokens[13],
  2411. 'type' => 'method', // setUp
  2412. ],
  2413. 46 => [
  2414. 'classIndex' => 1,
  2415. 'token' => $tokens[46],
  2416. 'type' => 'method', // testSomethingWithMoney
  2417. ],
  2418. 100 => [
  2419. 'classIndex' => 87,
  2420. 'token' => $tokens[100], // const A
  2421. 'type' => 'const',
  2422. ],
  2423. 115 => [
  2424. 'classIndex' => 65,
  2425. 'token' => $tokens[115], // const B
  2426. 'type' => 'const',
  2427. ],
  2428. 124 => [
  2429. 'classIndex' => 65, // $a
  2430. 'token' => $tokens[124],
  2431. 'type' => 'method', // foo
  2432. ],
  2433. 143 => [
  2434. 'classIndex' => 138,
  2435. 'token' => $tokens[143], // const AA
  2436. 'type' => 'const',
  2437. ],
  2438. 161 => [
  2439. 'classIndex' => 158,
  2440. 'token' => $tokens[161], // const AB
  2441. 'type' => 'const',
  2442. ],
  2443. ], $elements);
  2444. }
  2445. /**
  2446. * @dataProvider provideIsSuperGlobalCases
  2447. */
  2448. public function testIsSuperGlobal(bool $expected, string $source, int $index): void
  2449. {
  2450. $tokens = Tokens::fromCode($source);
  2451. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2452. self::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
  2453. }
  2454. public static function provideIsSuperGlobalCases(): iterable
  2455. {
  2456. $superNames = [
  2457. '$_COOKIE',
  2458. '$_ENV',
  2459. '$_FILES',
  2460. '$_GET',
  2461. '$_POST',
  2462. '$_REQUEST',
  2463. '$_SERVER',
  2464. '$_SESSION',
  2465. '$GLOBALS',
  2466. ];
  2467. $cases = [];
  2468. foreach ($superNames as $superName) {
  2469. $cases[] = [
  2470. true,
  2471. sprintf('<?php echo %s[0];', $superName),
  2472. 3,
  2473. ];
  2474. }
  2475. $notGlobalCodeCases = [
  2476. '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
  2477. '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
  2478. ];
  2479. foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
  2480. $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
  2481. for ($i = 0; $i < $tokensCount; ++$i) {
  2482. $cases[] = [
  2483. false,
  2484. $notGlobalCodeCase,
  2485. $i,
  2486. ];
  2487. }
  2488. }
  2489. return $cases;
  2490. }
  2491. /**
  2492. * @dataProvider provideGetClassyModifiersCases
  2493. *
  2494. * @param array<string, null|int> $expectedModifiers
  2495. */
  2496. public function testGetClassyModifiers(array $expectedModifiers, int $index, string $source): void
  2497. {
  2498. $tokens = Tokens::fromCode($source);
  2499. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2500. self::assertSame($expectedModifiers, $tokensAnalyzer->getClassyModifiers($index));
  2501. }
  2502. public static function provideGetClassyModifiersCases(): iterable
  2503. {
  2504. yield 'final' => [
  2505. ['final' => 1, 'abstract' => null, 'readonly' => null],
  2506. 3,
  2507. '<?php final class Foo {}',
  2508. ];
  2509. yield 'abstract' => [
  2510. ['final' => null, 'abstract' => 3, 'readonly' => null],
  2511. 5,
  2512. '<?php /* comment */ abstract class Foo {}',
  2513. ];
  2514. }
  2515. /**
  2516. * @requires PHP 8.2
  2517. *
  2518. * @dataProvider provideGetClassyModifiersOnPhp82Cases
  2519. *
  2520. * @param array<string, null|int> $expectedModifiers
  2521. */
  2522. public function testGetClassyModifiersOnPhp82(array $expectedModifiers, int $index, string $source): void
  2523. {
  2524. $tokens = Tokens::fromCode($source);
  2525. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2526. self::assertSame($expectedModifiers, $tokensAnalyzer->getClassyModifiers($index));
  2527. }
  2528. public static function provideGetClassyModifiersOnPhp82Cases(): iterable
  2529. {
  2530. yield 'readonly' => [
  2531. ['final' => null, 'abstract' => null, 'readonly' => 1],
  2532. 3,
  2533. '<?php readonly class Foo {}',
  2534. ];
  2535. yield 'readonly final' => [
  2536. ['final' => 3, 'abstract' => null, 'readonly' => 1],
  2537. 5,
  2538. '<?php readonly final class Foo {}',
  2539. ];
  2540. yield 'readonly final reversed' => [
  2541. ['final' => 1, 'abstract' => null, 'readonly' => 3],
  2542. 5,
  2543. '<?php final readonly class Foo {}',
  2544. ];
  2545. yield 'readonly final reversed + comment' => [
  2546. ['final' => 1, 'abstract' => null, 'readonly' => 5],
  2547. 7,
  2548. '<?php final /* comment */ readonly class Foo {}',
  2549. ];
  2550. yield 'readonly abstract' => [
  2551. ['final' => null, 'abstract' => 3, 'readonly' => 1],
  2552. 5,
  2553. '<?php readonly abstract class Foo {}',
  2554. ];
  2555. yield 'readonly abstract reversed' => [
  2556. ['final' => null, 'abstract' => 1, 'readonly' => 3],
  2557. 5,
  2558. '<?php abstract readonly class Foo {}',
  2559. ];
  2560. yield 'readonly abstract reversed + comment' => [
  2561. ['final' => null, 'abstract' => 1, 'readonly' => 5],
  2562. 7,
  2563. '<?php abstract /* comment */ readonly class Foo {}',
  2564. ];
  2565. }
  2566. public function testGetClassyModifiersForNonClassyThrowsAnException(): void
  2567. {
  2568. $tokens = Tokens::fromCode('<?php echo 1;');
  2569. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2570. $this->expectException(\InvalidArgumentException::class);
  2571. $tokensAnalyzer->getClassyModifiers(1);
  2572. }
  2573. /**
  2574. * @dataProvider provideGetLastTokenIndexOfArrowFunctionCases
  2575. *
  2576. * @param array<int, int> $expectations
  2577. */
  2578. public function testGetLastTokenIndexOfArrowFunction(array $expectations, string $source): void
  2579. {
  2580. $tokens = Tokens::fromCode($source);
  2581. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2582. $indices = [];
  2583. foreach ($expectations as $index => $expectedEndIndex) {
  2584. $indices[$index] = $tokensAnalyzer->getLastTokenIndexOfArrowFunction($index);
  2585. }
  2586. self::assertSame($expectations, $indices);
  2587. }
  2588. public static function provideGetLastTokenIndexOfArrowFunctionCases(): iterable
  2589. {
  2590. yield 'simple cases' => [
  2591. [
  2592. 2 => 11,
  2593. 16 => 25,
  2594. 28 => 39,
  2595. 46 => 61,
  2596. ],
  2597. '<?php
  2598. fn(array $x) => $x;
  2599. static fn(): int => $x;
  2600. fn($x = 42) => $x;
  2601. $eq = fn ($x, $y) => $x == $y;
  2602. ',
  2603. ];
  2604. yield 'references, splat and arrow cases' => [
  2605. [
  2606. 2 => 10,
  2607. 13 => 21,
  2608. 24 => 35,
  2609. 42 => 51,
  2610. 65 => 77,
  2611. ],
  2612. '<?php
  2613. fn(&$x) => $x;
  2614. fn&($x) => $x;
  2615. fn($x, ...$rest) => $rest;
  2616. $fn = fn(&$x) => $x++;
  2617. $y = &$fn($x);
  2618. fn($x, &...$rest) => 1;
  2619. ',
  2620. ];
  2621. yield 'different endings' => [
  2622. [
  2623. 9 => 21,
  2624. 31 => 43,
  2625. ],
  2626. '<?php
  2627. $results = array_map(
  2628. fn ($item) => $item * 2,
  2629. $list
  2630. );
  2631. return fn ($y) => $x * $y ?>
  2632. ',
  2633. ];
  2634. yield 'nested arrow function' => [
  2635. [
  2636. 1 => 26,
  2637. 14 => 25,
  2638. ],
  2639. '<?php fn(array $x, $z) => (fn(int $z):bool => $z);',
  2640. ];
  2641. yield 'arrow function as argument' => [
  2642. [
  2643. 5 => 14,
  2644. ],
  2645. '<?php return foo(fn(array $x) => $x);',
  2646. ];
  2647. yield 'arrow function as collection item' => [
  2648. [
  2649. 9 => 18,
  2650. 26 => 35,
  2651. 46 => 55,
  2652. 62 => 69,
  2653. ],
  2654. '<?php return [
  2655. [1, fn(array $x) => $x1, 1],
  2656. [fn(array $x) => $x2, 1],
  2657. [1, fn(array $x) => $x3],
  2658. ([(fn($x4) => $x5)]),
  2659. ];',
  2660. ];
  2661. yield 'nested inside anonymous class' => [
  2662. [
  2663. 1 => 46,
  2664. 33 => 41,
  2665. ],
  2666. '<?php fn($x) => $a = new class($x) { public function foo() { return fn(&$x) => $x; } };',
  2667. ];
  2668. yield 'array destructuring' => [
  2669. [
  2670. 4 => 13,
  2671. ],
  2672. '<?php return [fn(array $x) => $x1] = $x;',
  2673. ];
  2674. yield 'array_map() callback with different token blocks' => [
  2675. [
  2676. 9 => 28,
  2677. ],
  2678. '<?php
  2679. $a = array_map(
  2680. fn (array $item) => $item[\'callback\']($item[\'value\']),
  2681. [/* items */]
  2682. );
  2683. ',
  2684. ];
  2685. yield 'arrow function returning array' => [
  2686. [
  2687. 5 => 21,
  2688. ],
  2689. '<?php $z = fn ($a) => [0, 1, $a];',
  2690. ];
  2691. }
  2692. public function testCannotGetLastTokenIndexOfArrowFunctionForNonFnToken(): void
  2693. {
  2694. $tokens = Tokens::fromCode('<?php echo 1;');
  2695. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2696. $this->expectException(\InvalidArgumentException::class);
  2697. $tokensAnalyzer->getLastTokenIndexOfArrowFunction(1);
  2698. }
  2699. /**
  2700. * @param array<int, bool> $expected
  2701. */
  2702. private function doIsConstantInvocationTest(array $expected, string $source): void
  2703. {
  2704. $tokens = Tokens::fromCode($source);
  2705. self::assertCount(
  2706. $tokens->countTokenKind(T_STRING),
  2707. $expected,
  2708. 'All T_STRING tokens must be tested'
  2709. );
  2710. $tokensAnalyzer = new TokensAnalyzer($tokens);
  2711. foreach ($expected as $index => $expectedValue) {
  2712. self::assertSame(
  2713. $expectedValue,
  2714. $tokensAnalyzer->isConstantInvocation($index),
  2715. sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
  2716. );
  2717. }
  2718. }
  2719. }