TokensAnalyzerTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  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. $this->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 A() {
  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. $this->assertSame(
  136. [
  137. 9 => [
  138. 'token' => $tokens[9],
  139. 'type' => 'property',
  140. 'classIndex' => 1,
  141. ],
  142. 14 => [
  143. 'token' => $tokens[14],
  144. 'type' => 'method',
  145. 'classIndex' => 1,
  146. ],
  147. 33 => [
  148. 'token' => $tokens[33],
  149. 'type' => 'property',
  150. 'classIndex' => 26,
  151. ],
  152. 38 => [
  153. 'token' => $tokens[38],
  154. 'type' => 'method',
  155. 'classIndex' => 26,
  156. ],
  157. 56 => [
  158. 'token' => $tokens[56],
  159. 'type' => 'property',
  160. 'classIndex' => 50,
  161. ],
  162. 74 => [
  163. 'token' => $tokens[74],
  164. 'type' => 'method',
  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. $this->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. * @param string $source
  309. *
  310. * @dataProvider provideIsAnonymousClassCases
  311. */
  312. public function testIsAnonymousClass($source, array $expected)
  313. {
  314. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  315. foreach ($expected as $index => $expectedValue) {
  316. $this->assertSame($expectedValue, $tokensAnalyzer->isAnonymousClass($index));
  317. }
  318. }
  319. public function provideIsAnonymousClassCases()
  320. {
  321. return [
  322. [
  323. '<?php class foo {}',
  324. [1 => false],
  325. ],
  326. [
  327. '<?php $foo = new class() {};',
  328. [7 => true],
  329. ],
  330. [
  331. '<?php $foo = new class() extends Foo implements Bar, Baz {};',
  332. [7 => true],
  333. ],
  334. [
  335. '<?php class Foo { function bar() { return new class() {}; } }',
  336. [1 => false, 19 => true],
  337. ],
  338. [
  339. '<?php $a = new class(new class($d->a) implements B{}) extends C{};',
  340. [7 => true, 11 => true],
  341. ],
  342. ];
  343. }
  344. /**
  345. * @param string $source
  346. *
  347. * @dataProvider provideIsLambdaCases
  348. */
  349. public function testIsLambda($source, array $expected)
  350. {
  351. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  352. foreach ($expected as $index => $isLambda) {
  353. $this->assertSame($isLambda, $tokensAnalyzer->isLambda($index));
  354. }
  355. }
  356. public function provideIsLambdaCases()
  357. {
  358. return [
  359. [
  360. '<?php function foo () {};',
  361. [1 => false],
  362. ],
  363. [
  364. '<?php function /** foo */ foo () {};',
  365. [1 => false],
  366. ],
  367. [
  368. '<?php $foo = function () {};',
  369. [5 => true],
  370. ],
  371. [
  372. '<?php $foo = function /** foo */ () {};',
  373. [5 => true],
  374. ],
  375. [
  376. '<?php
  377. preg_replace_callback(
  378. "/(^|[a-z])/",
  379. function (array $matches) {
  380. return "a";
  381. },
  382. $string
  383. );',
  384. [7 => true],
  385. ],
  386. [
  387. '<?php $foo = function &() {};',
  388. [5 => true],
  389. ],
  390. ];
  391. }
  392. /**
  393. * @param string $source
  394. *
  395. * @dataProvider provideIsLambda70Cases
  396. * @requires PHP 7.0
  397. */
  398. public function testIsLambda70($source, array $expected)
  399. {
  400. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  401. foreach ($expected as $index => $expectedValue) {
  402. $this->assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  403. }
  404. }
  405. public function provideIsLambda70Cases()
  406. {
  407. return [
  408. [
  409. '<?php
  410. $a = function (): array {
  411. return [];
  412. };',
  413. [6 => true],
  414. ],
  415. [
  416. '<?php
  417. function foo (): array {
  418. return [];
  419. };',
  420. [2 => false],
  421. ],
  422. ];
  423. }
  424. /**
  425. * @param string $source
  426. *
  427. * @dataProvider provideIsLambda71Cases
  428. * @requires PHP 7.1
  429. */
  430. public function testIsLambda71($source, array $expected)
  431. {
  432. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  433. foreach ($expected as $index => $expectedValue) {
  434. $this->assertSame($expectedValue, $tokensAnalyzer->isLambda($index));
  435. }
  436. }
  437. public function provideIsLambda71Cases()
  438. {
  439. return [
  440. [
  441. '<?php
  442. $a = function (): void {
  443. return [];
  444. };',
  445. [6 => true],
  446. ],
  447. [
  448. '<?php
  449. function foo (): void {
  450. return [];
  451. };',
  452. [2 => false],
  453. ],
  454. [
  455. '<?php
  456. $a = function (): ?int {
  457. return [];
  458. };',
  459. [6 => true],
  460. ],
  461. [
  462. '<?php
  463. function foo (): ?int {
  464. return [];
  465. };',
  466. [2 => false],
  467. ],
  468. ];
  469. }
  470. /**
  471. * @param string $source
  472. *
  473. * @dataProvider provideIsUnarySuccessorOperatorCases
  474. */
  475. public function testIsUnarySuccessorOperator($source, array $expected)
  476. {
  477. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  478. foreach ($expected as $index => $isUnary) {
  479. $this->assertSame($isUnary, $tokensAnalyzer->isUnarySuccessorOperator($index));
  480. if ($isUnary) {
  481. $this->assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  482. $this->assertFalse($tokensAnalyzer->isBinaryOperator($index));
  483. }
  484. }
  485. }
  486. public function provideIsUnarySuccessorOperatorCases()
  487. {
  488. return [
  489. [
  490. '<?php $a++;',
  491. [2 => true],
  492. ],
  493. [
  494. '<?php $a--;',
  495. [2 => true],
  496. ],
  497. [
  498. '<?php $a ++;',
  499. [3 => true],
  500. ],
  501. [
  502. '<?php $a++ + 1;',
  503. [2 => true, 4 => false],
  504. ],
  505. [
  506. '<?php ${"a"}++;',
  507. [5 => true],
  508. ],
  509. [
  510. '<?php $foo->bar++;',
  511. [4 => true],
  512. ],
  513. [
  514. '<?php $foo->{"bar"}++;',
  515. [6 => true],
  516. ],
  517. [
  518. '<?php $a["foo"]++;',
  519. [5 => true],
  520. ],
  521. ];
  522. }
  523. /**
  524. * @param string $source
  525. *
  526. * @dataProvider provideIsUnaryPredecessorOperatorCases
  527. */
  528. public function testIsUnaryPredecessorOperator($source, array $expected)
  529. {
  530. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  531. foreach ($expected as $index => $isUnary) {
  532. $this->assertSame($isUnary, $tokensAnalyzer->isUnaryPredecessorOperator($index));
  533. if ($isUnary) {
  534. $this->assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  535. $this->assertFalse($tokensAnalyzer->isBinaryOperator($index));
  536. }
  537. }
  538. }
  539. public function provideIsUnaryPredecessorOperatorCases()
  540. {
  541. return [
  542. [
  543. '<?php ++$a;',
  544. [1 => true],
  545. ],
  546. [
  547. '<?php --$a;',
  548. [1 => true],
  549. ],
  550. [
  551. '<?php -- $a;',
  552. [1 => true],
  553. ],
  554. [
  555. '<?php $a + ++$b;',
  556. [3 => false, 5 => true],
  557. ],
  558. [
  559. '<?php !!$a;',
  560. [1 => true, 2 => true],
  561. ],
  562. [
  563. '<?php $a = &$b;',
  564. [5 => true],
  565. ],
  566. [
  567. '<?php function &foo() {}',
  568. [3 => true],
  569. ],
  570. [
  571. '<?php @foo();',
  572. [1 => true],
  573. ],
  574. [
  575. '<?php foo(+ $a, -$b);',
  576. [3 => true, 8 => true],
  577. ],
  578. [
  579. '<?php function foo(&$a, array &$b, Bar &$c) {}',
  580. [5 => true, 11 => true, 17 => true],
  581. ],
  582. [
  583. '<?php function foo($a, ...$b) {}',
  584. [8 => true],
  585. ],
  586. [
  587. '<?php function foo(&...$b) {}',
  588. [5 => true, 6 => true],
  589. ],
  590. [
  591. '<?php function foo(array ...$b) {}',
  592. [7 => true],
  593. ],
  594. [
  595. '<?php $foo = function(...$a) {};',
  596. [7 => true],
  597. ],
  598. [
  599. '<?php $foo = function($a, ...$b) {};',
  600. [10 => true],
  601. ],
  602. ];
  603. }
  604. /**
  605. * @param string $source
  606. *
  607. * @dataProvider provideIsBinaryOperatorCases
  608. */
  609. public function testIsBinaryOperator($source, array $expected)
  610. {
  611. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  612. foreach ($expected as $index => $isBinary) {
  613. $this->assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  614. if ($isBinary) {
  615. $this->assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  616. $this->assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  617. }
  618. }
  619. }
  620. public function provideIsBinaryOperatorCases()
  621. {
  622. $cases = [
  623. [
  624. '<?php [] + [];',
  625. [4 => true],
  626. ],
  627. [
  628. '<?php $a + $b;',
  629. [3 => true],
  630. ],
  631. [
  632. '<?php 1 + $b;',
  633. [3 => true],
  634. ],
  635. [
  636. '<?php 0.2 + $b;',
  637. [3 => true],
  638. ],
  639. [
  640. '<?php $a[1] + $b;',
  641. [6 => true],
  642. ],
  643. [
  644. '<?php FOO + $b;',
  645. [3 => true],
  646. ],
  647. [
  648. '<?php foo() + $b;',
  649. [5 => true],
  650. ],
  651. [
  652. '<?php ${"foo"} + $b;',
  653. [6 => true],
  654. ],
  655. [
  656. '<?php $a+$b;',
  657. [2 => true],
  658. ],
  659. [
  660. '<?php $a /* foo */ + /* bar */ $b;',
  661. [5 => true],
  662. ],
  663. [
  664. '<?php $a =
  665. $b;',
  666. [3 => true],
  667. ],
  668. [
  669. '<?php $a
  670. = $b;',
  671. [3 => true],
  672. ],
  673. [
  674. '<?php $a = array("b" => "c", );',
  675. [3 => true, 9 => true, 12 => false],
  676. ],
  677. [
  678. '<?php $a * -$b;',
  679. [3 => true, 5 => false],
  680. ],
  681. [
  682. '<?php $a = -2 / +5;',
  683. [3 => true, 5 => false, 8 => true, 10 => false],
  684. ],
  685. [
  686. '<?php $a = &$b;',
  687. [3 => true, 5 => false],
  688. ],
  689. [
  690. '<?php $a++ + $b;',
  691. [2 => false, 4 => true],
  692. ],
  693. [
  694. '<?php $a = FOO & $bar;',
  695. [7 => true],
  696. ],
  697. [
  698. '<?php __LINE__ - 1;',
  699. [3 => true],
  700. ],
  701. [
  702. '<?php `echo 1` + 1;',
  703. [5 => true],
  704. ],
  705. [
  706. '<?php $a ** $b;',
  707. [3 => true],
  708. ],
  709. [
  710. '<?php $a **= $b;',
  711. [3 => true],
  712. ],
  713. ];
  714. $operators = [
  715. '+', '-', '*', '/', '%', '<', '>', '|', '^', '&=', '&&', '||', '.=', '/=', '==', '>=', '===', '!=',
  716. '<>', '!==', '<=', 'and', 'or', 'xor', '-=', '%=', '*=', '|=', '+=', '<<', '<<=', '>>', '>>=', '^',
  717. ];
  718. foreach ($operators as $operator) {
  719. $cases[] = [
  720. '<?php $a '.$operator.' $b;',
  721. [3 => true],
  722. ];
  723. }
  724. return $cases;
  725. }
  726. /**
  727. * @param string $source
  728. *
  729. * @dataProvider provideIsBinaryOperator70Cases
  730. * @requires PHP 7.0
  731. */
  732. public function testIsBinaryOperator70($source, array $expected)
  733. {
  734. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  735. foreach ($expected as $index => $isBinary) {
  736. $this->assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  737. if ($isBinary) {
  738. $this->assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  739. $this->assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  740. }
  741. }
  742. }
  743. public function provideIsBinaryOperator70Cases()
  744. {
  745. return [
  746. [
  747. '<?php $a <=> $b;',
  748. [3 => true],
  749. ],
  750. [
  751. '<?php $a ?? $b;',
  752. [3 => true],
  753. ],
  754. ];
  755. }
  756. /**
  757. * @param string $source
  758. * @param int $tokenIndex
  759. * @param bool $isMultiLineArray
  760. *
  761. * @dataProvider provideIsArrayCases
  762. */
  763. public function testIsArray($source, $tokenIndex, $isMultiLineArray = false)
  764. {
  765. $tokens = Tokens::fromCode($source);
  766. $tokensAnalyzer = new TokensAnalyzer($tokens);
  767. $this->assertTrue($tokensAnalyzer->isArray($tokenIndex), 'Expected to be an array.');
  768. $this->assertSame($isMultiLineArray, $tokensAnalyzer->isArrayMultiLine($tokenIndex), sprintf('Expected %sto be a multiline array', $isMultiLineArray ? '' : 'not '));
  769. }
  770. public function provideIsArrayCases()
  771. {
  772. $cases = [
  773. [
  774. '<?php
  775. array("a" => 1);
  776. ',
  777. 2,
  778. ],
  779. [
  780. // short array PHP 5.4 single line
  781. '<?php
  782. ["a" => 2];
  783. ',
  784. 2, false,
  785. ],
  786. [
  787. '<?php
  788. array(
  789. "a" => 3
  790. );
  791. ',
  792. 2, true,
  793. ],
  794. [
  795. // short array PHP 5.4 multi line
  796. '<?php
  797. [
  798. "a" => 4
  799. ];
  800. ',
  801. 2, true,
  802. ],
  803. [
  804. '<?php
  805. array(
  806. "a" => array(5, 6, 7),
  807. 8 => new \Exception(\'Ellow\')
  808. );
  809. ',
  810. 2, true,
  811. ],
  812. [
  813. // mix short array syntax
  814. '<?php
  815. array(
  816. "a" => [9, 10, 11],
  817. 12 => new \Exception(\'Ellow\')
  818. );
  819. ',
  820. 2, true,
  821. ],
  822. // Windows/Max EOL testing
  823. [
  824. "<?php\r\narray('a' => 13);\r\n",
  825. 1,
  826. ],
  827. [
  828. "<?php\r\n array(\r\n 'a' => 14,\r\n 'b' => 15\r\n );\r\n",
  829. 2, true,
  830. ],
  831. ];
  832. return $cases;
  833. }
  834. /**
  835. * @param string $source
  836. * @param int[] $tokenIndexes
  837. *
  838. * @dataProvider provideIsArray71Cases
  839. * @requires PHP 7.1
  840. */
  841. public function testIsArray71($source, $tokenIndexes)
  842. {
  843. $tokens = Tokens::fromCode($source);
  844. $tokensAnalyzer = new TokensAnalyzer($tokens);
  845. foreach ($tokens as $index => $token) {
  846. $expect = in_array($index, $tokenIndexes, true);
  847. $this->assertSame(
  848. $expect,
  849. $tokensAnalyzer->isArray($index),
  850. sprintf('Expected %sarray, got @ %d "%s".', $expect ? '' : 'no ', $index, var_export($token, true))
  851. );
  852. }
  853. }
  854. public function provideIsArray71Cases()
  855. {
  856. return [
  857. [
  858. '<?php
  859. [$a] = $z;
  860. ["a" => $a, "b" => $b] = $array;
  861. $c = [$d, $e] = $array[$a];
  862. [[$a, $b], [$c, $d]] = $d;
  863. ',
  864. [51, 59],
  865. ],
  866. ];
  867. }
  868. /**
  869. * @param string $source
  870. *
  871. * @dataProvider provideIsBinaryOperator71Cases
  872. * @requires PHP 7.1
  873. */
  874. public function testIsBinaryOperator71($source, array $expected)
  875. {
  876. $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
  877. foreach ($expected as $index => $isBinary) {
  878. $this->assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
  879. if ($isBinary) {
  880. $this->assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
  881. $this->assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
  882. }
  883. }
  884. }
  885. public function provideIsBinaryOperator71Cases()
  886. {
  887. return [
  888. [
  889. '<?php try {} catch (A | B $e) {}',
  890. [11 => true],
  891. ],
  892. ];
  893. }
  894. /**
  895. * @param string $source
  896. * @param int $tokenIndex
  897. *
  898. * @dataProvider provideArrayExceptionsCases
  899. */
  900. public function testIsNotArray($source, $tokenIndex)
  901. {
  902. $tokens = Tokens::fromCode($source);
  903. $tokensAnalyzer = new TokensAnalyzer($tokens);
  904. $this->assertFalse($tokensAnalyzer->isArray($tokenIndex));
  905. }
  906. /**
  907. * @param string $source
  908. * @param int $tokenIndex
  909. *
  910. * @dataProvider provideArrayExceptionsCases
  911. */
  912. public function testIsMultiLineArrayException($source, $tokenIndex)
  913. {
  914. $this->expectException(\InvalidArgumentException::class);
  915. $tokens = Tokens::fromCode($source);
  916. $tokensAnalyzer = new TokensAnalyzer($tokens);
  917. $tokensAnalyzer->isArrayMultiLine($tokenIndex);
  918. }
  919. public function provideArrayExceptionsCases()
  920. {
  921. $cases = [
  922. ['<?php $a;', 1],
  923. ["<?php\n \$a = (0+1); // [0,1]", 4],
  924. ['<?php $text = "foo $bbb[0] bar";', 8],
  925. ['<?php $text = "foo ${aaa[123]} bar";', 9],
  926. ];
  927. return $cases;
  928. }
  929. /**
  930. * @param string $source
  931. * @param int $index
  932. * @param array $expected
  933. *
  934. * @dataProvider provideGetFunctionPropertiesCases
  935. */
  936. public function testGetFunctionProperties($source, $index, array $expected)
  937. {
  938. $tokens = Tokens::fromCode($source);
  939. $tokensAnalyzer = new TokensAnalyzer($tokens);
  940. $attributes = $tokensAnalyzer->getMethodAttributes($index);
  941. $this->assertSame($expected, $attributes);
  942. }
  943. public function provideGetFunctionPropertiesCases()
  944. {
  945. $defaultAttributes = [
  946. 'visibility' => null,
  947. 'static' => false,
  948. 'abstract' => false,
  949. 'final' => false,
  950. ];
  951. $template = '
  952. <?php
  953. class TestClass {
  954. %s function a() {
  955. //
  956. }
  957. }
  958. ';
  959. $cases = [];
  960. $attributes = $defaultAttributes;
  961. $attributes['visibility'] = T_PRIVATE;
  962. $cases[] = [sprintf($template, 'private'), 10, $attributes];
  963. $attributes = $defaultAttributes;
  964. $attributes['visibility'] = T_PUBLIC;
  965. $cases[] = [sprintf($template, 'public'), 10, $attributes];
  966. $attributes = $defaultAttributes;
  967. $attributes['visibility'] = T_PROTECTED;
  968. $cases[] = [sprintf($template, 'protected'), 10, $attributes];
  969. $attributes = $defaultAttributes;
  970. $attributes['visibility'] = null;
  971. $attributes['static'] = true;
  972. $cases[] = [sprintf($template, 'static'), 10, $attributes];
  973. $attributes = $defaultAttributes;
  974. $attributes['visibility'] = T_PUBLIC;
  975. $attributes['static'] = true;
  976. $attributes['final'] = true;
  977. $cases[] = [sprintf($template, 'final public static'), 14, $attributes];
  978. $attributes = $defaultAttributes;
  979. $attributes['visibility'] = null;
  980. $attributes['abstract'] = true;
  981. $cases[] = [sprintf($template, 'abstract'), 10, $attributes];
  982. $attributes = $defaultAttributes;
  983. $attributes['visibility'] = T_PUBLIC;
  984. $attributes['abstract'] = true;
  985. $cases[] = [sprintf($template, 'abstract public'), 12, $attributes];
  986. $attributes = $defaultAttributes;
  987. $cases[] = [sprintf($template, ''), 8, $attributes];
  988. return $cases;
  989. }
  990. public function testIsWhilePartOfDoWhile()
  991. {
  992. $source =
  993. <<<'SRC'
  994. <?php
  995. // `not do`
  996. while(false) {
  997. }
  998. while (false);
  999. while (false)?>
  1000. <?php
  1001. if(false){
  1002. }while(false);
  1003. if(false){
  1004. }while(false)?><?php
  1005. while(false){}while(false){}
  1006. while ($i <= 10):
  1007. echo $i;
  1008. $i++;
  1009. endwhile;
  1010. ?>
  1011. <?php while(false): ?>
  1012. <?php endwhile ?>
  1013. <?php
  1014. // `do`
  1015. do{
  1016. } while(false);
  1017. do{
  1018. } while(false)?>
  1019. <?php
  1020. if (false){}do{}while(false);
  1021. // `not do`, `do`
  1022. if(false){}while(false){}do{}while(false);
  1023. SRC;
  1024. $expected = [
  1025. 3 => false,
  1026. 12 => false,
  1027. 19 => false,
  1028. 34 => false,
  1029. 47 => false,
  1030. 53 => false,
  1031. 59 => false,
  1032. 66 => false,
  1033. 91 => false,
  1034. 112 => true,
  1035. 123 => true,
  1036. 139 => true,
  1037. 153 => false,
  1038. 162 => true,
  1039. ];
  1040. $tokens = Tokens::fromCode($source);
  1041. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1042. foreach ($tokens as $index => $token) {
  1043. if (!$token->isGivenKind(T_WHILE)) {
  1044. continue;
  1045. }
  1046. $this->assertSame(
  1047. $expected[$index],
  1048. $tokensAnalyzer->isWhilePartOfDoWhile($index),
  1049. sprintf('Expected token at index "%d" to be detected as %sa "do-while"-loop.', $index, true === $expected[$index] ? '' : 'not ')
  1050. );
  1051. }
  1052. }
  1053. /**
  1054. * @param string $input
  1055. * @param bool $perNamespace
  1056. *
  1057. * @dataProvider provideGetImportUseIndexesCases
  1058. */
  1059. public function testGetImportUseIndexes(array $expected, $input, $perNamespace = false)
  1060. {
  1061. $tokens = Tokens::fromCode($input);
  1062. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1063. $this->assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1064. }
  1065. public function provideGetImportUseIndexesCases()
  1066. {
  1067. return [
  1068. [
  1069. [1, 8],
  1070. '<?php use E\F?><?php use A\B;',
  1071. ],
  1072. [
  1073. [[1], [14], [29]],
  1074. '<?php
  1075. use T\A;
  1076. namespace A { use D\C; }
  1077. namespace b { use D\C; }
  1078. ',
  1079. true,
  1080. ],
  1081. [
  1082. [[1, 8]],
  1083. '<?php use D\B; use A\C?>',
  1084. true,
  1085. ],
  1086. [
  1087. [1, 8],
  1088. '<?php use D\B; use A\C?>',
  1089. ],
  1090. [
  1091. [7, 22],
  1092. '<?php
  1093. namespace A { use D\C; }
  1094. namespace b { use D\C; }
  1095. ',
  1096. ],
  1097. [
  1098. [3, 10, 34, 45, 54, 59, 77, 95],
  1099. <<<'EOF'
  1100. use Zoo\Bar;
  1101. use Foo\Bar;
  1102. use Foo\Zar\Baz;
  1103. <?php
  1104. use Foo\Bar;
  1105. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  1106. use Foo\Bir as FBB;
  1107. use Foo\Zar\Baz;
  1108. use SomeClass;
  1109. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  1110. use Zoo\Bar;
  1111. $a = new someclass();
  1112. use Zoo\Tar;
  1113. class AnnotatedClass
  1114. {
  1115. }
  1116. EOF
  1117. ,
  1118. ],
  1119. ];
  1120. }
  1121. /**
  1122. * @param string $input
  1123. * @param bool $perNamespace
  1124. *
  1125. * @dataProvider provideGetImportUseIndexesPHP70Cases
  1126. * @requires PHP 7.0
  1127. */
  1128. public function testGetImportUseIndexesPHP70(array $expected, $input, $perNamespace = false)
  1129. {
  1130. $tokens = Tokens::fromCode($input);
  1131. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1132. $this->assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1133. }
  1134. public function provideGetImportUseIndexesPHP70Cases()
  1135. {
  1136. return [
  1137. [
  1138. [1, 22, 41],
  1139. '<?php
  1140. use some\a\{ClassA, ClassB, ClassC as C};
  1141. use function some\a\{fn_a, fn_b, fn_c};
  1142. use const some\a\{ConstA, ConstB, ConstC};
  1143. ',
  1144. ],
  1145. [
  1146. [[1, 22, 41]],
  1147. '<?php
  1148. use some\a\{ClassA, ClassB, ClassC as C};
  1149. use function some\a\{fn_a, fn_b, fn_c};
  1150. use const some\a\{ConstA, ConstB, ConstC};
  1151. ',
  1152. true,
  1153. ],
  1154. ];
  1155. }
  1156. /**
  1157. * @param string $input
  1158. * @param bool $perNamespace
  1159. *
  1160. * @dataProvider provideGetImportUseIndexesPHP72Cases
  1161. * @requires PHP 7.2
  1162. */
  1163. public function testGetImportUseIndexesPHP72(array $expected, $input, $perNamespace = false)
  1164. {
  1165. $tokens = Tokens::fromCode($input);
  1166. $tokensAnalyzer = new TokensAnalyzer($tokens);
  1167. $this->assertSame($expected, $tokensAnalyzer->getImportUseIndexes($perNamespace));
  1168. }
  1169. public function provideGetImportUseIndexesPHP72Cases()
  1170. {
  1171. return [
  1172. [
  1173. [1, 23, 43],
  1174. '<?php
  1175. use some\a\{ClassA, ClassB, ClassC as C,};
  1176. use function some\a\{fn_a, fn_b, fn_c,};
  1177. use const some\a\{ConstA, ConstB, ConstC,};
  1178. ',
  1179. ],
  1180. [
  1181. [[1, 23, 43]],
  1182. '<?php
  1183. use some\a\{ClassA, ClassB, ClassC as C,};
  1184. use function some\a\{fn_a, fn_b, fn_c,};
  1185. use const some\a\{ConstA, ConstB, ConstC,};
  1186. ',
  1187. true,
  1188. ],
  1189. ];
  1190. }
  1191. }