TokensAnalyzerTest.php 67 KB

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