TokensAnalyzerTest.php 56 KB

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