TokensAnalyzerTest.php 50 KB

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