TokensAnalyzerTest.php 54 KB

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