TokensAnalyzerTest.php 53 KB

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