TokensAnalyzerTest.php 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068
  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. }
  529. /**
  530. * @dataProvider provideIsLambda70Cases
  531. * @requires PHP 7.0
  532. */
  533. public function testIsLambda70(string $source, array $expected): void
  534. {
  535. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  536. foreach ($expected as $index => $expectedValue) {
  537. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  538. }
  539. }
  540. public function provideIsLambda70Cases()
  541. {
  542. return [
  543. [
  544. '<?php
  545. $a = function (): array {
  546. return [];
  547. };',
  548. [6 => true],
  549. ],
  550. [
  551. '<?php
  552. function foo (): array {
  553. return [];
  554. };',
  555. [2 => false],
  556. ],
  557. ];
  558. }
  559. /**
  560. * @dataProvider provideIsLambda74Cases
  561. * @requires PHP 7.4
  562. */
  563. public function testIsLambda74(string $source, array $expected): void
  564. {
  565. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  566. foreach ($expected as $index => $expectedValue) {
  567. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  568. }
  569. }
  570. public function provideIsLambda74Cases()
  571. {
  572. return [
  573. [
  574. '<?php $fn = fn() => [];',
  575. [5 => true],
  576. ],
  577. [
  578. '<?php $fn = fn () => [];',
  579. [5 => true],
  580. ],
  581. ];
  582. }
  583. /**
  584. * @dataProvider provideIsLambda71Cases
  585. * @requires PHP 7.1
  586. */
  587. public function testIsLambda71(string $source, array $expected): void
  588. {
  589. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  590. foreach ($expected as $index => $expectedValue) {
  591. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  592. }
  593. }
  594. public function provideIsLambda71Cases()
  595. {
  596. return [
  597. [
  598. '<?php
  599. $a = function (): void {
  600. return [];
  601. };',
  602. [6 => true],
  603. ],
  604. [
  605. '<?php
  606. function foo (): void {
  607. return [];
  608. };',
  609. [2 => false],
  610. ],
  611. [
  612. '<?php
  613. $a = function (): ?int {
  614. return [];
  615. };',
  616. [6 => true],
  617. ],
  618. [
  619. '<?php
  620. $a = function (): int {
  621. return [];
  622. };',
  623. [6 => true],
  624. ],
  625. [
  626. '<?php
  627. function foo (): ?int {
  628. return [];
  629. };',
  630. [2 => false],
  631. ],
  632. ];
  633. }
  634. /**
  635. * @dataProvider provideIsLambda80Cases
  636. * @requires PHP 8.0
  637. */
  638. public function testIsLambda80(string $source, array $expected): void
  639. {
  640. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  641. foreach ($expected as $index => $expectedValue) {
  642. static::assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  643. }
  644. }
  645. public function provideIsLambda80Cases()
  646. {
  647. return [
  648. [
  649. '<?php
  650. $a = function (): ?static {
  651. return [];
  652. };',
  653. [6 => true],
  654. ],
  655. [
  656. '<?php
  657. $a = function (): static {
  658. return [];
  659. };',
  660. [6 => true],
  661. ],
  662. [
  663. '<?php
  664. $c = 4; //
  665. $a = function(
  666. $a,
  667. $b,
  668. ) use (
  669. $c,
  670. ) {
  671. echo $a + $b + $c;
  672. };
  673. $a(1,2);',
  674. [14 => true],
  675. ],
  676. ];
  677. }
  678. public function testIsLambdaInvalid(): void
  679. {
  680. $this->expectException(\LogicException::class);
  681. $this->expectExceptionMessage('No T_FUNCTION or T_FN at given index 0, got "T_OPEN_TAG".');
  682. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  683. $tokensAnalyzer->isLambda(0);
  684. }
  685. /**
  686. * @dataProvider provideIsConstantInvocationCases
  687. */
  688. public function testIsConstantInvocation(string $source, array $expected): void
  689. {
  690. $this->doIsConstantInvocationTest($source, $expected);
  691. }
  692. public function provideIsConstantInvocationCases()
  693. {
  694. return [
  695. [
  696. '<?php echo FOO;',
  697. [3 => true],
  698. ],
  699. [
  700. '<?php echo \FOO;',
  701. [4 => true],
  702. ],
  703. [
  704. '<?php echo Foo\Bar\BAR;',
  705. [3 => false, 5 => false, 7 => true],
  706. ],
  707. [
  708. '<?php echo FOO ? BAR : BAZ;',
  709. [3 => true, 7 => true, 11 => true],
  710. ],
  711. [
  712. '<?php echo FOO & BAR | BAZ;',
  713. [3 => true, 7 => true, 11 => true],
  714. ],
  715. [
  716. '<?php echo FOO & $bar;',
  717. [3 => true],
  718. ],
  719. [
  720. '<?php echo $foo[BAR];',
  721. [5 => true],
  722. ],
  723. [
  724. '<?php echo FOO[BAR];',
  725. [3 => true, 5 => true],
  726. ],
  727. [
  728. '<?php func(FOO, Bar\BAZ);',
  729. [1 => false, 3 => true, 6 => false, 8 => true],
  730. ],
  731. [
  732. '<?php if (FOO && BAR) {}',
  733. [4 => true, 8 => true],
  734. ],
  735. [
  736. '<?php return FOO * X\Y\BAR;',
  737. [3 => true, 7 => false, 9 => false, 11 => true],
  738. ],
  739. [
  740. '<?php function x() { yield FOO; yield FOO => BAR; }',
  741. [3 => false, 11 => true, 16 => true, 20 => true],
  742. ],
  743. [
  744. '<?php switch ($a) { case FOO: break; }',
  745. [11 => true],
  746. ],
  747. [
  748. '<?php namespace FOO;',
  749. [3 => false],
  750. ],
  751. [
  752. '<?php use FOO;',
  753. [3 => false],
  754. ],
  755. [
  756. '<?php use function FOO\BAR\BAZ;',
  757. [5 => false, 7 => false, 9 => false],
  758. ],
  759. [
  760. '<?php namespace X; const FOO = 1;',
  761. [3 => false, 8 => false],
  762. ],
  763. [
  764. '<?php class FOO {}',
  765. [3 => false],
  766. ],
  767. [
  768. '<?php interface FOO {}',
  769. [3 => false],
  770. ],
  771. [
  772. '<?php trait FOO {}',
  773. [3 => false],
  774. ],
  775. [
  776. '<?php class x extends FOO {}',
  777. [3 => false, 7 => false],
  778. ],
  779. [
  780. '<?php class x implements FOO {}',
  781. [3 => false, 7 => false],
  782. ],
  783. [
  784. '<?php class x implements FOO, BAR, BAZ {}',
  785. [3 => false, 7 => false, 10 => false, 13 => false],
  786. ],
  787. [
  788. '<?php class x { const FOO = 1; }',
  789. [3 => false, 9 => false],
  790. ],
  791. [
  792. '<?php class x { use FOO; }',
  793. [3 => false, 9 => false],
  794. ],
  795. [
  796. '<?php class x { use FOO, BAR { FOO::BAZ insteadof BAR; } }',
  797. [3 => false, 9 => false, 12 => false, 16 => false, 18 => false, 22 => false],
  798. ],
  799. [
  800. '<?php function x (FOO $foo, BAR &$bar, BAZ ...$baz) {}',
  801. [3 => false, 6 => false, 11 => false, 17 => false],
  802. ],
  803. [
  804. '<?php FOO();',
  805. [1 => false],
  806. ],
  807. [
  808. '<?php FOO::x();',
  809. [1 => false, 3 => false],
  810. ],
  811. [
  812. '<?php x::FOO();',
  813. [1 => false, 3 => false],
  814. ],
  815. [
  816. '<?php $foo instanceof FOO;',
  817. [5 => false],
  818. ],
  819. [
  820. '<?php try {} catch (FOO $e) {}',
  821. [9 => false],
  822. ],
  823. [
  824. '<?php "$foo[BAR]";',
  825. [4 => false],
  826. ],
  827. [
  828. '<?php "{$foo[BAR]}";',
  829. [5 => true],
  830. ],
  831. [
  832. '<?php FOO: goto FOO;',
  833. [1 => false, 6 => false],
  834. ],
  835. [
  836. '<?php foo(E_USER_DEPRECATED | E_DEPRECATED);',
  837. [1 => false, 3 => true, 7 => true],
  838. ],
  839. [
  840. '<?php interface Foo extends Bar, Baz, Qux {}',
  841. [3 => false, 7 => false, 10 => false, 13 => false],
  842. ],
  843. [
  844. '<?php use Foo\Bar, Foo\Baz, Foo\Qux;',
  845. [3 => false, 5 => false, 8 => false, 10 => false, 13 => false, 15 => false],
  846. ],
  847. ];
  848. }
  849. /**
  850. * @dataProvider provideIsConstantInvocation70Cases
  851. * @requires PHP 7.0
  852. */
  853. public function testIsConstantInvocation70(string $source, array $expected): void
  854. {
  855. $this->doIsConstantInvocationTest($source, $expected);
  856. }
  857. public function provideIsConstantInvocation70Cases()
  858. {
  859. return [
  860. [
  861. '<?php function x(): FOO {}',
  862. [3 => false, 8 => false],
  863. ],
  864. [
  865. '<?php use X\Y\{FOO, BAR as BAR2, BAZ};',
  866. [3 => false, 5 => false, 8 => false, 11 => false, 15 => false, 18 => false],
  867. ],
  868. ];
  869. }
  870. /**
  871. * @dataProvider provideIsConstantInvocation71Cases
  872. * @requires PHP 7.1
  873. */
  874. public function testIsConstantInvocation71(string $source, array $expected): void
  875. {
  876. $this->doIsConstantInvocationTest($source, $expected);
  877. }
  878. public function provideIsConstantInvocation71Cases()
  879. {
  880. return [
  881. [
  882. '<?php function x(?FOO $foo) {}',
  883. [3 => false, 6 => false],
  884. ],
  885. [
  886. '<?php function x(): ?FOO {}',
  887. [3 => false, 9 => false],
  888. ],
  889. [
  890. '<?php try {} catch (FOO|BAR|BAZ $e) {}',
  891. [9 => false, 11 => false, 13 => false],
  892. ],
  893. [
  894. '<?php interface Foo { public function bar(): Baz; }',
  895. [3 => false, 11 => false, 16 => false],
  896. ],
  897. [
  898. '<?php interface Foo { public function bar(): \Baz; }',
  899. [3 => false, 11 => false, 17 => false],
  900. ],
  901. [
  902. '<?php interface Foo { public function bar(): ?Baz; }',
  903. [3 => false, 11 => false, 17 => false],
  904. ],
  905. [
  906. '<?php interface Foo { public function bar(): ?\Baz; }',
  907. [3 => false, 11 => false, 18 => false],
  908. ],
  909. ];
  910. }
  911. /**
  912. * @dataProvider provideIsConstantInvocationPhp80Cases
  913. * @requires PHP 8.0
  914. */
  915. public function testIsConstantInvocationPhp80(string $source, array $expected): void
  916. {
  917. $this->doIsConstantInvocationTest($source, $expected);
  918. }
  919. public function provideIsConstantInvocationPhp80Cases()
  920. {
  921. yield [
  922. '<?php $a?->b?->c;',
  923. [3 => false, 5 => false],
  924. ];
  925. yield [
  926. '<?php try {} catch (Exception) {}',
  927. [9 => false],
  928. ];
  929. yield [
  930. '<?php try {} catch (\Exception) {}',
  931. [10 => false],
  932. ];
  933. yield [
  934. '<?php try {} catch (Foo | Bar) {}',
  935. [9 => false, 13 => false],
  936. ];
  937. yield [
  938. '<?php #[Foo] function foo() {}',
  939. [2 => false, 7 => false],
  940. ];
  941. yield [
  942. '<?php #[Foo()] function foo() {}',
  943. [2 => false, 9 => false],
  944. ];
  945. }
  946. public function testIsConstantInvocationInvalid(): void
  947. {
  948. $this->expectException(\LogicException::class);
  949. $this->expectExceptionMessage('No T_STRING at given index 0, got "T_OPEN_TAG".');
  950. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode('<?php '));
  951. $tokensAnalyzer->isConstantInvocation(0);
  952. }
  953. /**
  954. * @requires PHP 8.0
  955. */
  956. public function testIsConstantInvocationForNullSafeObjectOperator(): void
  957. {
  958. $tokens = Tokens::fromCode('<?php $a?->b?->c;');
  959. $tokensAnalyzer = new TokensAnalyzer($tokens);
  960. foreach ($tokens as $index => $token) {
  961. if (!$token->isGivenKind(T_STRING)) {
  962. continue;
  963. }
  964. static::assertFalse($tokensAnalyzer->isConstantInvocation($index));
  965. }
  966. }
  967. /**
  968. * @dataProvider provideIsUnarySuccessorOperatorCases
  969. */
  970. public function testIsUnarySuccessorOperator(string $source, array $expected): void
  971. {
  972. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  973. foreach ($expected as $index => $isUnary) {
  974. static::assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  975. if ($isUnary) {
  976. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  977. static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  978. }
  979. }
  980. }
  981. public function provideIsUnarySuccessorOperatorCases()
  982. {
  983. return [
  984. [
  985. '<?php $a++;',
  986. [2 => true],
  987. ],
  988. [
  989. '<?php $a--;',
  990. [2 => true],
  991. ],
  992. [
  993. '<?php $a ++;',
  994. [3 => true],
  995. ],
  996. [
  997. '<?php $a++ + 1;',
  998. [2 => true, 4 => false],
  999. ],
  1000. [
  1001. '<?php ${"a"}++;',
  1002. [5 => true],
  1003. ],
  1004. [
  1005. '<?php $foo->bar++;',
  1006. [4 => true],
  1007. ],
  1008. [
  1009. '<?php $foo->{"bar"}++;',
  1010. [6 => true],
  1011. ],
  1012. 'array access' => [
  1013. '<?php $a["foo"]++;',
  1014. [5 => true],
  1015. ],
  1016. 'array curly access' => [
  1017. '<?php $a{"foo"}++;',
  1018. [5 => true],
  1019. ],
  1020. ];
  1021. }
  1022. /**
  1023. * @dataProvider provideIsUnaryPredecessorOperatorCases
  1024. */
  1025. public function testIsUnaryPredecessorOperator(string $source, array $expected): void
  1026. {
  1027. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1028. foreach ($expected as $index => $isUnary) {
  1029. static::assertSame($isUnary, $tokensAnalyzer->isUnaryPredecessorOperator($index));
  1030. if ($isUnary) {
  1031. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1032. static::assertFalse($tokensAnalyzer->isBinaryOperator($index));
  1033. }
  1034. }
  1035. }
  1036. public function provideIsUnaryPredecessorOperatorCases()
  1037. {
  1038. return [
  1039. [
  1040. '<?php ++$a;',
  1041. [1 => true],
  1042. ],
  1043. [
  1044. '<?php --$a;',
  1045. [1 => true],
  1046. ],
  1047. [
  1048. '<?php -- $a;',
  1049. [1 => true],
  1050. ],
  1051. [
  1052. '<?php $a + ++$b;',
  1053. [3 => false, 5 => true],
  1054. ],
  1055. [
  1056. '<?php !!$a;',
  1057. [1 => true, 2 => true],
  1058. ],
  1059. [
  1060. '<?php $a = &$b;',
  1061. [5 => true],
  1062. ],
  1063. [
  1064. '<?php function &foo() {}',
  1065. [3 => true],
  1066. ],
  1067. [
  1068. '<?php @foo();',
  1069. [1 => true],
  1070. ],
  1071. [
  1072. '<?php foo(+ $a, -$b);',
  1073. [3 => true, 8 => true],
  1074. ],
  1075. [
  1076. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  1077. [5 => true, 11 => true, 17 => true],
  1078. ],
  1079. [
  1080. '<?php function foo($a, ...$b) {}',
  1081. [8 => true],
  1082. ],
  1083. [
  1084. '<?php function foo(&...$b) {}',
  1085. [5 => true, 6 => true],
  1086. ],
  1087. [
  1088. '<?php function foo(array ...$b) {}',
  1089. [7 => true],
  1090. ],
  1091. [
  1092. '<?php $foo = function(...$a) {};',
  1093. [7 => true],
  1094. ],
  1095. [
  1096. '<?php $foo = function($a, ...$b) {};',
  1097. [10 => true],
  1098. ],
  1099. ];
  1100. }
  1101. /**
  1102. * @dataProvider provideIsBinaryOperatorCases
  1103. */
  1104. public function testIsBinaryOperator(string $source, array $expected): void
  1105. {
  1106. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1107. foreach ($expected as $index => $isBinary) {
  1108. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1109. if ($isBinary) {
  1110. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1111. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1112. }
  1113. }
  1114. }
  1115. public function provideIsBinaryOperatorCases()
  1116. {
  1117. $cases = [
  1118. [
  1119. '<?php echo $a[1] + 1;',
  1120. [8 => true],
  1121. ],
  1122. [
  1123. '<?php echo $a{1} + 1;',
  1124. [8 => true],
  1125. ],
  1126. [
  1127. '<?php $a .= $b; ?>',
  1128. [3 => true],
  1129. ],
  1130. [
  1131. '<?php $a . \'a\' ?>',
  1132. [3 => true],
  1133. ],
  1134. [
  1135. '<?php $a &+ $b;',
  1136. [3 => true],
  1137. ],
  1138. [
  1139. '<?php $a && $b;',
  1140. [3 => true],
  1141. ],
  1142. [
  1143. '<?php $a & $b;',
  1144. [3 => true],
  1145. ],
  1146. [
  1147. '<?php [] + [];',
  1148. [4 => true],
  1149. ],
  1150. [
  1151. '<?php $a + $b;',
  1152. [3 => true],
  1153. ],
  1154. [
  1155. '<?php 1 + $b;',
  1156. [3 => true],
  1157. ],
  1158. [
  1159. '<?php 0.2 + $b;',
  1160. [3 => true],
  1161. ],
  1162. [
  1163. '<?php $a[1] + $b;',
  1164. [6 => true],
  1165. ],
  1166. [
  1167. '<?php FOO + $b;',
  1168. [3 => true],
  1169. ],
  1170. [
  1171. '<?php foo() + $b;',
  1172. [5 => true],
  1173. ],
  1174. [
  1175. '<?php ${"foo"} + $b;',
  1176. [6 => true],
  1177. ],
  1178. [
  1179. '<?php $a+$b;',
  1180. [2 => true],
  1181. ],
  1182. [
  1183. '<?php $a /* foo */ + /* bar */ $b;',
  1184. [5 => true],
  1185. ],
  1186. [
  1187. '<?php $a =
  1188. $b;',
  1189. [3 => true],
  1190. ],
  1191. [
  1192. '<?php $a
  1193. = $b;',
  1194. [3 => true],
  1195. ],
  1196. [
  1197. '<?php $a = array("b" => "c", );',
  1198. [3 => true, 9 => true, 12 => false],
  1199. ],
  1200. [
  1201. '<?php $a * -$b;',
  1202. [3 => true, 5 => false],
  1203. ],
  1204. [
  1205. '<?php $a = -2 / +5;',
  1206. [3 => true, 5 => false, 8 => true, 10 => false],
  1207. ],
  1208. [
  1209. '<?php $a = &$b;',
  1210. [3 => true, 5 => false],
  1211. ],
  1212. [
  1213. '<?php $a++ + $b;',
  1214. [2 => false, 4 => true],
  1215. ],
  1216. [
  1217. '<?php $a = FOO & $bar;',
  1218. [7 => true],
  1219. ],
  1220. [
  1221. '<?php __LINE__ - 1;',
  1222. [3 => true],
  1223. ],
  1224. [
  1225. '<?php `echo 1` + 1;',
  1226. [5 => true],
  1227. ],
  1228. [
  1229. '<?php $a ** $b;',
  1230. [3 => true],
  1231. ],
  1232. [
  1233. '<?php $a **= $b;',
  1234. [3 => true],
  1235. ],
  1236. ];
  1237. $operators = [
  1238. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  1239. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
  1240. ];
  1241. foreach ($operators as $operator) {
  1242. $cases[] = [
  1243. '<?php $a '.$operator.' $b;',
  1244. [3 => true],
  1245. ];
  1246. }
  1247. return $cases;
  1248. }
  1249. /**
  1250. * @dataProvider provideIsBinaryOperator70Cases
  1251. * @requires PHP 7.0
  1252. */
  1253. public function testIsBinaryOperator70(string $source, array $expected): void
  1254. {
  1255. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1256. foreach ($expected as $index => $isBinary) {
  1257. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1258. if ($isBinary) {
  1259. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1260. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1261. }
  1262. }
  1263. }
  1264. public function provideIsBinaryOperator70Cases()
  1265. {
  1266. return [
  1267. [
  1268. '<?php $a <=> $b;',
  1269. [3 => true],
  1270. ],
  1271. [
  1272. '<?php $a ?? $b;',
  1273. [3 => true],
  1274. ],
  1275. ];
  1276. }
  1277. /**
  1278. * @dataProvider provideIsArrayCases
  1279. */
  1280. public function testIsArray(string $source, int $tokenIndex, bool $isMultiLineArray = false): void
  1281. {
  1282. $tokens = Tokens::fromCode($source);
  1283. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1284. static::assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  1285. static::assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  1286. }
  1287. public function provideIsArrayCases()
  1288. {
  1289. return [
  1290. [
  1291. '<?php
  1292. array("a" => 1);
  1293. ',
  1294. 2,
  1295. ],
  1296. [
  1297. '<?php
  1298. ["a" => 2];
  1299. ',
  1300. 2, false,
  1301. ],
  1302. [
  1303. '<?php
  1304. array(
  1305. "a" => 3
  1306. );
  1307. ',
  1308. 2, true,
  1309. ],
  1310. [
  1311. '<?php
  1312. [
  1313. "a" => 4
  1314. ];
  1315. ',
  1316. 2, true,
  1317. ],
  1318. [
  1319. '<?php
  1320. array(
  1321. "a" => array(5, 6, 7),
  1322. 8 => new \Exception(\'Ellow\')
  1323. );
  1324. ',
  1325. 2, true,
  1326. ],
  1327. [
  1328. // mix short array syntax
  1329. '<?php
  1330. array(
  1331. "a" => [9, 10, 11],
  1332. 12 => new \Exception(\'Ellow\')
  1333. );
  1334. ',
  1335. 2, true,
  1336. ],
  1337. // Windows/Max EOL testing
  1338. [
  1339. "<?php\r\narray('a' => 13);\r\n",
  1340. 1,
  1341. ],
  1342. [
  1343. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  1344. 2, true,
  1345. ],
  1346. ];
  1347. }
  1348. /**
  1349. * @param int[] $tokenIndexes
  1350. *
  1351. * @dataProvider provideIsArray71Cases
  1352. * @requires PHP 7.1
  1353. */
  1354. public function testIsArray71(string $source, array $tokenIndexes): void
  1355. {
  1356. $tokens = Tokens::fromCode($source);
  1357. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1358. foreach ($tokens as $index => $token) {
  1359. $expect = \in_array($index, $tokenIndexes, true);
  1360. static::assertSame(
  1361. $expect,
  1362. $tokensAnalyzer->isArray($index),
  1363. sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  1364. );
  1365. }
  1366. }
  1367. public function provideIsArray71Cases()
  1368. {
  1369. return [
  1370. [
  1371. '<?php
  1372. [$a] = $z;
  1373. ["a" => $a, "b" => $b] = $array;
  1374. $c = [$d, $e] = $array[$a];
  1375. [[$a, $b], [$c, $d]] = $d;
  1376. $array = []; $d = array();
  1377. ',
  1378. [76, 84],
  1379. ],
  1380. ];
  1381. }
  1382. /**
  1383. * @dataProvider provideIsBinaryOperator71Cases
  1384. * @requires PHP 7.1
  1385. */
  1386. public function testIsBinaryOperator71(string $source, array $expected): void
  1387. {
  1388. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1389. foreach ($expected as $index => $isBinary) {
  1390. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1391. if ($isBinary) {
  1392. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1393. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1394. }
  1395. }
  1396. }
  1397. public function provideIsBinaryOperator71Cases()
  1398. {
  1399. return [
  1400. [
  1401. '<?php try {} catch (A | B $e) {}',
  1402. [11 => false],
  1403. ],
  1404. ];
  1405. }
  1406. /**
  1407. * @dataProvider provideIsBinaryOperator74Cases
  1408. * @requires PHP 7.4
  1409. */
  1410. public function testIsBinaryOperator74(string $source, array $expected): void
  1411. {
  1412. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  1413. foreach ($expected as $index => $isBinary) {
  1414. static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  1415. if ($isBinary) {
  1416. static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  1417. static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  1418. }
  1419. }
  1420. }
  1421. public function provideIsBinaryOperator74Cases()
  1422. {
  1423. return [
  1424. [
  1425. '<?php $a ??= $b;',
  1426. [3 => true],
  1427. ],
  1428. ];
  1429. }
  1430. /**
  1431. * @dataProvider provideArrayExceptionsCases
  1432. */
  1433. public function testIsNotArray(string $source, int $tokenIndex): void
  1434. {
  1435. $tokens = Tokens::fromCode($source);
  1436. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1437. static::assertFalse($tokensAnalyzer->isArray($tokenIndex));
  1438. }
  1439. /**
  1440. * @dataProvider provideArrayExceptionsCases
  1441. */
  1442. public function testIsMultiLineArrayException(string $source, int $tokenIndex): void
  1443. {
  1444. $this->expectException(\InvalidArgumentException::class);
  1445. $tokens = Tokens::fromCode($source);
  1446. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1447. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  1448. }
  1449. public function provideArrayExceptionsCases()
  1450. {
  1451. return [
  1452. ['<?php $a;', 1],
  1453. ["<?php\n \$a = (0+1); // [0,1]", 4],
  1454. ['<?php $text = "foo $bbb[0] bar";', 8],
  1455. ['<?php $text = "foo ${aaa[123]} bar";', 9],
  1456. ];
  1457. }
  1458. public function testIsBlockMultilineException(): void
  1459. {
  1460. $this->expectException(\LogicException::class);
  1461. $tokens = Tokens::fromCode('<?php foo(1, 2, 3);');
  1462. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1463. $tokensAnalyzer->isBlockMultiline($tokens, 1);
  1464. }
  1465. /**
  1466. * @param bool $isBlockMultiline
  1467. * @param string $source
  1468. * @param int $tokenIndex
  1469. *
  1470. * @dataProvider provideIsBlockMultilineCases
  1471. */
  1472. public function testIsBlockMultiline($isBlockMultiline, $source, $tokenIndex): void
  1473. {
  1474. $tokens = Tokens::fromCode($source);
  1475. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1476. static::assertSame($isBlockMultiline, $tokensAnalyzer->isBlockMultiline($tokens, $tokenIndex));
  1477. }
  1478. public static function provideIsBlockMultilineCases()
  1479. {
  1480. yield [
  1481. false,
  1482. '<?php foo(1, 2, 3);',
  1483. 2,
  1484. ];
  1485. yield [
  1486. true,
  1487. '<?php foo(1,
  1488. 2,
  1489. 3
  1490. );',
  1491. 2,
  1492. ];
  1493. yield [
  1494. false,
  1495. '<?php foo(1, "Multi
  1496. string", 2, 3);',
  1497. 2,
  1498. ];
  1499. yield [
  1500. false,
  1501. '<?php foo(1, havingNestedBlockThatIsMultilineDoesNotMakeTheMainBlockMultiline(
  1502. "a",
  1503. "b"
  1504. ), 2, 3);',
  1505. 2,
  1506. ];
  1507. }
  1508. /**
  1509. * @dataProvider provideGetFunctionPropertiesCases
  1510. */
  1511. public function testGetFunctionProperties(string $source, int $index, array $expected): void
  1512. {
  1513. $tokens = Tokens::fromCode($source);
  1514. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1515. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  1516. static::assertSame($expected, $attributes);
  1517. }
  1518. public function provideGetFunctionPropertiesCases()
  1519. {
  1520. $defaultAttributes = [
  1521. 'visibility' => null,
  1522. 'static' => false,
  1523. 'abstract' => false,
  1524. 'final' => false,
  1525. ];
  1526. $template = '
  1527. <?php
  1528. class TestClass {
  1529. %s function a() {
  1530. //
  1531. }
  1532. }
  1533. ';
  1534. $cases = [];
  1535. $attributes = $defaultAttributes;
  1536. $attributes['visibility'] = T_PRIVATE;
  1537. $cases[] = [sprintf($template, 'private'), 10, $attributes];
  1538. $attributes = $defaultAttributes;
  1539. $attributes['visibility'] = T_PUBLIC;
  1540. $cases[] = [sprintf($template, 'public'), 10, $attributes];
  1541. $attributes = $defaultAttributes;
  1542. $attributes['visibility'] = T_PROTECTED;
  1543. $cases[] = [sprintf($template, 'protected'), 10, $attributes];
  1544. $attributes = $defaultAttributes;
  1545. $attributes['visibility'] = null;
  1546. $attributes['static'] = true;
  1547. $cases[] = [sprintf($template, 'static'), 10, $attributes];
  1548. $attributes = $defaultAttributes;
  1549. $attributes['visibility'] = T_PUBLIC;
  1550. $attributes['static'] = true;
  1551. $attributes['final'] = true;
  1552. $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
  1553. $attributes = $defaultAttributes;
  1554. $attributes['visibility'] = null;
  1555. $attributes['abstract'] = true;
  1556. $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
  1557. $attributes = $defaultAttributes;
  1558. $attributes['visibility'] = T_PUBLIC;
  1559. $attributes['abstract'] = true;
  1560. $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
  1561. $attributes = $defaultAttributes;
  1562. $cases[] = [sprintf($template, ''), 8, $attributes];
  1563. return $cases;
  1564. }
  1565. public function testIsWhilePartOfDoWhile(): void
  1566. {
  1567. $source =
  1568. <<<'SRC'
  1569. <?php
  1570. // `not do`
  1571. while(false) {
  1572. }
  1573. while (false);
  1574. while (false)?>
  1575. <?php
  1576. if(false){
  1577. }while(false);
  1578. if(false){
  1579. }while(false)?><?php
  1580. while(false){}while(false){}
  1581. while ($i <= 10):
  1582. echo $i;
  1583. $i++;
  1584. endwhile;
  1585. ?>
  1586. <?php while(false): ?>
  1587. <?php endwhile ?>
  1588. <?php
  1589. // `do`
  1590. do{
  1591. } while(false);
  1592. do{
  1593. } while(false)?>
  1594. <?php
  1595. if (false){}do{}while(false);
  1596. // `not do`, `do`
  1597. if(false){}while(false){}do{}while(false);
  1598. SRC;
  1599. $expected = [
  1600. 3 => false,
  1601. 12 => false,
  1602. 19 => false,
  1603. 34 => false,
  1604. 47 => false,
  1605. 53 => false,
  1606. 59 => false,
  1607. 66 => false,
  1608. 91 => false,
  1609. 112 => true,
  1610. 123 => true,
  1611. 139 => true,
  1612. 153 => false,
  1613. 162 => true,
  1614. ];
  1615. $tokens = Tokens::fromCode($source);
  1616. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1617. foreach ($tokens as $index => $token) {
  1618. if (!$token->isGivenKind(T_WHILE)) {
  1619. continue;
  1620. }
  1621. static::assertSame(
  1622. $expected[$index],
  1623. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  1624. sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not ')
  1625. );
  1626. }
  1627. }
  1628. /**
  1629. * @dataProvider provideGetImportUseIndexesCases
  1630. */
  1631. public function testGetImportUseIndexes(array $expected, string $input, bool $perNamespace = false): void
  1632. {
  1633. $tokens = Tokens::fromCode($input);
  1634. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1635. static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1636. }
  1637. public function provideGetImportUseIndexesCases()
  1638. {
  1639. return [
  1640. [
  1641. [1, 8],
  1642. '<?php use E\F?><?php use A\B;',
  1643. ],
  1644. [
  1645. [[1], [14], [29]],
  1646. '<?php
  1647. use T\A;
  1648. namespace A { use D\C; }
  1649. namespace b { use D\C; }
  1650. ',
  1651. true,
  1652. ],
  1653. [
  1654. [[1, 8]],
  1655. '<?php use D\B; use A\C?>',
  1656. true,
  1657. ],
  1658. [
  1659. [1, 8],
  1660. '<?php use D\B; use A\C?>',
  1661. ],
  1662. [
  1663. [7, 22],
  1664. '<?php
  1665. namespace A { use D\C; }
  1666. namespace b { use D\C; }
  1667. ',
  1668. ],
  1669. [
  1670. [3, 10, 34, 45, 54, 59, 77, 95],
  1671. <<<'EOF'
  1672. use Zoo\Bar;
  1673. use Foo\Bar;
  1674. use Foo\Zar\Baz;
  1675. <?php
  1676. use Foo\Bar;
  1677. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  1678. use Foo\Bir as FBB;
  1679. use Foo\Zar\Baz;
  1680. use SomeClass;
  1681. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  1682. use Zoo\Bar;
  1683. $a = new someclass();
  1684. use Zoo\Tar;
  1685. class AnnotatedClass
  1686. {
  1687. }
  1688. EOF
  1689. ,
  1690. ],
  1691. ];
  1692. }
  1693. /**
  1694. * @dataProvider provideGetImportUseIndexesPHP70Cases
  1695. * @requires PHP 7.0
  1696. */
  1697. public function testGetImportUseIndexesPHP70(array $expected, string $input, bool $perNamespace = false): void
  1698. {
  1699. $tokens = Tokens::fromCode($input);
  1700. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1701. static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1702. }
  1703. public function provideGetImportUseIndexesPHP70Cases()
  1704. {
  1705. return [
  1706. [
  1707. [1, 22, 41],
  1708. '<?php
  1709. use some\a\{ClassA, ClassB, ClassC as C};
  1710. use function some\a\{fn_a, fn_b, fn_c};
  1711. use const some\a\{ConstA, ConstB, ConstC};
  1712. ',
  1713. ],
  1714. [
  1715. [[1, 22, 41]],
  1716. '<?php
  1717. use some\a\{ClassA, ClassB, ClassC as C};
  1718. use function some\a\{fn_a, fn_b, fn_c};
  1719. use const some\a\{ConstA, ConstB, ConstC};
  1720. ',
  1721. true,
  1722. ],
  1723. ];
  1724. }
  1725. /**
  1726. * @dataProvider provideGetImportUseIndexesPHP72Cases
  1727. * @requires PHP 7.2
  1728. */
  1729. public function testGetImportUseIndexesPHP72(array $expected, string $input, bool $perNamespace = false): void
  1730. {
  1731. $tokens = Tokens::fromCode($input);
  1732. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1733. static::assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1734. }
  1735. public function provideGetImportUseIndexesPHP72Cases()
  1736. {
  1737. return [
  1738. [
  1739. [1, 23, 43],
  1740. '<?php
  1741. use some\a\{ClassA, ClassB, ClassC as C,};
  1742. use function some\a\{fn_a, fn_b, fn_c,};
  1743. use const some\a\{ConstA, ConstB, ConstC,};
  1744. ',
  1745. ],
  1746. [
  1747. [[1, 23, 43]],
  1748. '<?php
  1749. use some\a\{ClassA, ClassB, ClassC as C,};
  1750. use function some\a\{fn_a, fn_b, fn_c,};
  1751. use const some\a\{ConstA, ConstB, ConstC,};
  1752. ',
  1753. true,
  1754. ],
  1755. ];
  1756. }
  1757. public function testGetClassyElementsWithMultipleNestedAnonymousClass(): void
  1758. {
  1759. $source = '<?php
  1760. class MyTestWithAnonymousClass extends TestCase
  1761. {
  1762. public function setUp()
  1763. {
  1764. $provider = new class(function () {}) {};
  1765. }
  1766. public function testSomethingWithMoney(
  1767. Money $amount
  1768. ) {
  1769. $a = new class(function () {
  1770. new class(function () {
  1771. new class(function () {})
  1772. {
  1773. const A=1;
  1774. };
  1775. })
  1776. {
  1777. const B=1;
  1778. public function foo() {
  1779. $c = new class() {const AA=3;};
  1780. $d = new class {const AB=3;};
  1781. }
  1782. };
  1783. })
  1784. {
  1785. const C=1;
  1786. };
  1787. }
  1788. }';
  1789. $tokens = Tokens::fromCode($source);
  1790. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1791. $elements = $tokensAnalyzer->getClassyElements();
  1792. static::assertSame([
  1793. 13 => [
  1794. 'token' => $tokens[13],
  1795. 'type' => 'method', // setUp
  1796. 'classIndex' => 1,
  1797. ],
  1798. 46 => [
  1799. 'token' => $tokens[46],
  1800. 'type' => 'method', // testSomethingWithMoney
  1801. 'classIndex' => 1,
  1802. ],
  1803. 100 => [
  1804. 'token' => $tokens[100], // const A
  1805. 'type' => 'const',
  1806. 'classIndex' => 87,
  1807. ],
  1808. 115 => [
  1809. 'token' => $tokens[115], // const B
  1810. 'type' => 'const',
  1811. 'classIndex' => 65,
  1812. ],
  1813. 124 => [
  1814. 'token' => $tokens[124],
  1815. 'type' => 'method', // foo
  1816. 'classIndex' => 65, // $a
  1817. ],
  1818. 143 => [
  1819. 'token' => $tokens[143], // const AA
  1820. 'type' => 'const',
  1821. 'classIndex' => 138,
  1822. ],
  1823. 161 => [
  1824. 'token' => $tokens[161], // const AB
  1825. 'type' => 'const',
  1826. 'classIndex' => 158,
  1827. ],
  1828. ], $elements);
  1829. }
  1830. /**
  1831. * @dataProvider provideIsSuperGlobalCases
  1832. */
  1833. public function testIsSuperGlobal(bool $expected, string $source, int $index): void
  1834. {
  1835. $tokens = Tokens::fromCode($source);
  1836. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1837. static::assertSame($expected, $tokensAnalyzer->isSuperGlobal($index));
  1838. }
  1839. public function provideIsSuperGlobalCases()
  1840. {
  1841. $superNames = [
  1842. '$_COOKIE',
  1843. '$_ENV',
  1844. '$_FILES',
  1845. '$_GET',
  1846. '$_POST',
  1847. '$_REQUEST',
  1848. '$_SERVER',
  1849. '$_SESSION',
  1850. '$GLOBALS',
  1851. ];
  1852. $cases = [];
  1853. foreach ($superNames as $superName) {
  1854. $cases[] = [
  1855. true,
  1856. sprintf('<?php echo %s[0];', $superName),
  1857. 3,
  1858. ];
  1859. }
  1860. $notGlobalCodeCases = [
  1861. '<?php echo 1; $a = static function($b) use ($a) { $a->$b(); }; // $_SERVER',
  1862. '<?php class Foo{}?> <?php $_A = 1; /* $_SESSION */',
  1863. ];
  1864. foreach ($notGlobalCodeCases as $notGlobalCodeCase) {
  1865. $tokensCount = \count(Tokens::fromCode($notGlobalCodeCase));
  1866. for ($i = 0; $i < $tokensCount; ++$i) {
  1867. $cases[] = [
  1868. false,
  1869. $notGlobalCodeCase,
  1870. $i,
  1871. ];
  1872. }
  1873. }
  1874. return $cases;
  1875. }
  1876. private function doIsConstantInvocationTest(string $source, array $expected): void
  1877. {
  1878. $tokens = Tokens::fromCode($source);
  1879. static::assertCount(
  1880. $tokens->countTokenKind(T_STRING),
  1881. $expected,
  1882. 'All T_STRING tokens must be tested'
  1883. );
  1884. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1885. foreach ($expected as $index => $expectedValue) {
  1886. static::assertSame(
  1887. $expectedValue,
  1888. $tokensAnalyzer->isConstantInvocation($index),
  1889. sprintf('Token at index '.$index.' should match the expected value (%s).', $expectedValue ? 'true' : 'false')
  1890. );
  1891. }
  1892. }
  1893. }