TokensAnalyzerTest.php 43 KB

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