TokensAnalyzerTest.php 79 KB

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