FunctionsAnalyzerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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\Analyzer;
  12. use PhpCsFixer\Tests\TestCase;
  13. use PhpCsFixer\Tokenizer\Analyzer\Analysis\ArgumentAnalysis;
  14. use PhpCsFixer\Tokenizer\Analyzer\Analysis\TypeAnalysis;
  15. use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer;
  16. use PhpCsFixer\Tokenizer\Tokens;
  17. /**
  18. * @author VeeWee <toonverwerft@gmail.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer
  23. */
  24. final class FunctionsAnalyzerTest extends TestCase
  25. {
  26. /**
  27. * @param bool $isFunctionIndex
  28. * @param string $code
  29. * @param int $index
  30. *
  31. * @dataProvider provideIsGlobalFunctionCallCases
  32. */
  33. public function testIsGlobalFunctionCall($isFunctionIndex, $code, $index)
  34. {
  35. $tokens = Tokens::fromCode($code);
  36. $analyzer = new FunctionsAnalyzer();
  37. static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
  38. }
  39. public function provideIsGlobalFunctionCallCases()
  40. {
  41. yield '1' => [
  42. false,
  43. '<?php CONSTANT;',
  44. 1,
  45. ];
  46. yield '2' => [
  47. true,
  48. '<?php foo("bar");',
  49. 1,
  50. ];
  51. yield '3' => [
  52. false,
  53. '<?php \foo("bar");',
  54. 1,
  55. ];
  56. yield '4' => [
  57. true,
  58. '<?php \foo("bar");',
  59. 2,
  60. ];
  61. yield '5' => [
  62. false,
  63. '<?php foo\bar("baz");',
  64. 1,
  65. ];
  66. yield '6' => [
  67. false,
  68. '<?php foo\bar("baz");',
  69. 3,
  70. ];
  71. yield '7' => [
  72. false,
  73. '<?php foo::bar("baz");',
  74. 1,
  75. ];
  76. yield '8' => [
  77. false,
  78. '<?php foo::bar("baz");',
  79. 3,
  80. ];
  81. yield '9' => [
  82. false,
  83. '<?php $foo->bar("baz");',
  84. 3,
  85. ];
  86. yield '10' => [
  87. false,
  88. '<?php new bar("baz");',
  89. 3,
  90. ];
  91. yield '11' => [
  92. false,
  93. '<?php function foo() {}',
  94. 3,
  95. ];
  96. yield '12' => [
  97. false,
  98. '<?php function & foo() {}',
  99. 5,
  100. ];
  101. yield '13' => [
  102. false,
  103. '<?php namespace\foo("bar");',
  104. 3,
  105. ];
  106. yield '15' => [
  107. true,
  108. '<?php
  109. namespace A {
  110. use function A;
  111. }
  112. namespace B {
  113. use function D;
  114. A();
  115. }
  116. ',
  117. 30,
  118. ];
  119. yield '16' => [
  120. true,
  121. '<?php
  122. function A(){}
  123. A();
  124. ',
  125. 10,
  126. ];
  127. yield '17' => [
  128. true,
  129. '<?php
  130. function A(){}
  131. a();
  132. ',
  133. 10,
  134. ];
  135. yield '18' => [
  136. true,
  137. '<?php
  138. namespace {
  139. function A(){}
  140. A();
  141. }
  142. ',
  143. 14,
  144. ];
  145. yield '19' => [
  146. false,
  147. '<?php
  148. namespace Z {
  149. function A(){}
  150. A();
  151. }
  152. ',
  153. 16,
  154. ];
  155. yield '20' => [
  156. false,
  157. '<?php
  158. namespace Z;
  159. function A(){}
  160. A();
  161. ',
  162. 15,
  163. ];
  164. yield '21' => [
  165. true,
  166. '<?php
  167. function & A(){}
  168. A();
  169. ',
  170. 12,
  171. ];
  172. yield '22' => [
  173. true,
  174. '<?php
  175. class Foo
  176. {
  177. public function A(){}
  178. }
  179. A();
  180. ',
  181. 20,
  182. ];
  183. yield '23' => [
  184. true,
  185. '<?php
  186. namespace A {
  187. function A(){}
  188. }
  189. namespace B {
  190. A();
  191. }
  192. ',
  193. 24,
  194. ];
  195. yield '24' => [
  196. false,
  197. '<?php
  198. use function X\a;
  199. A();
  200. ',
  201. 11,
  202. ];
  203. yield '25' => [
  204. true,
  205. '<?php
  206. use A;
  207. A();
  208. ',
  209. 7,
  210. ];
  211. yield '26' => [
  212. true,
  213. '<?php
  214. use const A;
  215. A();
  216. ',
  217. 9,
  218. ];
  219. yield '27' => [
  220. true,
  221. '<?php
  222. use function A;
  223. str_repeat($a, $b);
  224. ',
  225. 9,
  226. ];
  227. yield '28' => [
  228. true,
  229. '<?php
  230. namespace {
  231. function A(){}
  232. A();
  233. $b = function(){};
  234. }
  235. ',
  236. 14,
  237. ];
  238. foreach ([1, 6, 11, 16, 21, 26] as $index) {
  239. yield [
  240. true,
  241. '<?php implode($a);implode($a);implode($a);implode($a);implode($a);implode($a);',
  242. $index,
  243. ];
  244. }
  245. if (\PHP_VERSION_ID < 80000) {
  246. yield '14' => [
  247. true,
  248. '<?php
  249. use function \ str_repeat;
  250. str_repeat($a, $b);
  251. ',
  252. 11,
  253. ];
  254. }
  255. }
  256. /**
  257. * @param bool $isFunctionIndex
  258. * @param string $code
  259. * @param int $index
  260. *
  261. * @dataProvider provideIsGlobalFunctionCallPhp70Cases
  262. * @requires PHP 7.0
  263. */
  264. public function testIsGlobalFunctionCallPhp70($isFunctionIndex, $code, $index)
  265. {
  266. $tokens = Tokens::fromCode($code);
  267. $analyzer = new FunctionsAnalyzer();
  268. static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
  269. }
  270. public function provideIsGlobalFunctionCallPhp70Cases()
  271. {
  272. yield [
  273. true,
  274. '<?php
  275. $z = new class(
  276. new class(){ private function A(){} }
  277. ){
  278. public function A() {}
  279. };
  280. A();
  281. ',
  282. 46,
  283. ];
  284. }
  285. /**
  286. * @param bool $isFunctionIndex
  287. * @param string $code
  288. * @param int $index
  289. *
  290. * @dataProvider provideIsGlobalFunctionCallPhp74Cases
  291. * @requires PHP 7.4
  292. */
  293. public function testIsGlobalFunctionCallPhp74($isFunctionIndex, $code, $index)
  294. {
  295. $tokens = Tokens::fromCode($code);
  296. $analyzer = new FunctionsAnalyzer();
  297. static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
  298. }
  299. public function provideIsGlobalFunctionCallPhp74Cases()
  300. {
  301. yield [
  302. false,
  303. '<?php $foo = fn() => false;',
  304. 5,
  305. ];
  306. }
  307. /**
  308. * @param bool $isFunctionIndex
  309. * @param string $code
  310. * @param int $index
  311. *
  312. * @dataProvider provideIsGlobalFunctionCallPhp80Cases
  313. * @requires PHP 8.0
  314. */
  315. public function testIsGlobalFunctionCallPhp80($isFunctionIndex, $code, $index)
  316. {
  317. $tokens = Tokens::fromCode($code);
  318. $analyzer = new FunctionsAnalyzer();
  319. static::assertSame($isFunctionIndex, $analyzer->isGlobalFunctionCall($tokens, $index));
  320. }
  321. public function provideIsGlobalFunctionCallPhp80Cases()
  322. {
  323. yield [
  324. true,
  325. '<?php $a = new (foo());',
  326. 8,
  327. ];
  328. yield [
  329. true,
  330. '<?php $b = $foo instanceof (foo());',
  331. 10,
  332. ];
  333. yield [
  334. false,
  335. '<?php
  336. #[\Attribute(\Attribute::TARGET_CLASS)]
  337. class Foo {}
  338. ',
  339. 3,
  340. ];
  341. }
  342. /**
  343. * @param string $code
  344. * @param int $methodIndex
  345. * @param array $expected
  346. *
  347. * @dataProvider provideFunctionsWithArgumentsCases
  348. */
  349. public function testFunctionArgumentInfo($code, $methodIndex, $expected)
  350. {
  351. $tokens = Tokens::fromCode($code);
  352. $analyzer = new FunctionsAnalyzer();
  353. static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
  354. }
  355. /**
  356. * @param string $code
  357. * @param int $methodIndex
  358. * @param array $expected
  359. *
  360. * @dataProvider provideFunctionsWithReturnTypeCases
  361. */
  362. public function testFunctionReturnTypeInfo($code, $methodIndex, $expected)
  363. {
  364. $tokens = Tokens::fromCode($code);
  365. $analyzer = new FunctionsAnalyzer();
  366. $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
  367. static::assertSame(serialize($expected), serialize($actual));
  368. }
  369. /**
  370. * @param string $code
  371. * @param int $methodIndex
  372. * @param array $expected
  373. *
  374. * @dataProvider provideFunctionsWithReturnTypePhp70Cases
  375. * @requires PHP 7.0
  376. */
  377. public function testFunctionReturnTypeInfoPhp70($code, $methodIndex, $expected)
  378. {
  379. $tokens = Tokens::fromCode($code);
  380. $analyzer = new FunctionsAnalyzer();
  381. $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
  382. static::assertSame(serialize($expected), serialize($actual));
  383. }
  384. public function provideFunctionsWithArgumentsCases()
  385. {
  386. $tests = [
  387. ['<?php function(){};', 1, []],
  388. ['<?php function($a){};', 1, [
  389. '$a' => new ArgumentAnalysis(
  390. '$a',
  391. 3,
  392. null,
  393. null
  394. ),
  395. ]],
  396. ['<?php function($a, $b){};', 1, [
  397. '$a' => new ArgumentAnalysis(
  398. '$a',
  399. 3,
  400. null,
  401. null
  402. ),
  403. '$b' => new ArgumentAnalysis(
  404. '$b',
  405. 6,
  406. null,
  407. null
  408. ),
  409. ]],
  410. ['<?php function($a, $b = array(1,2), $c = 3){};', 1, [
  411. '$a' => new ArgumentAnalysis(
  412. '$a',
  413. 3,
  414. null,
  415. null
  416. ),
  417. '$b' => new ArgumentAnalysis(
  418. '$b',
  419. 6,
  420. 'array(1,2)',
  421. null
  422. ),
  423. '$c' => new ArgumentAnalysis(
  424. '$c',
  425. 18,
  426. '3',
  427. null
  428. ),
  429. ]],
  430. ['<?php function(array $a = array()){};', 1, [
  431. '$a' => new ArgumentAnalysis(
  432. '$a',
  433. 5,
  434. 'array()',
  435. new TypeAnalysis(
  436. 'array',
  437. 3,
  438. 3
  439. )
  440. ),
  441. ]],
  442. ['<?php function(array ... $a){};', 1, [
  443. '$a' => new ArgumentAnalysis(
  444. '$a',
  445. 7,
  446. null,
  447. new TypeAnalysis(
  448. 'array',
  449. 3,
  450. 3
  451. )
  452. ),
  453. ]],
  454. ['<?php function(\Foo\Bar $a){};', 1, [
  455. '$a' => new ArgumentAnalysis(
  456. '$a',
  457. 8,
  458. null,
  459. new TypeAnalysis(
  460. '\Foo\Bar',
  461. 3,
  462. 6
  463. )
  464. ),
  465. ]],
  466. ];
  467. foreach ($tests as $index => $test) {
  468. yield $index => $test;
  469. }
  470. if (\PHP_VERSION_ID < 80000) {
  471. yield ['<?php function(\Foo/** TODO: change to something else */\Bar $a){};', 1, [
  472. '$a' => new ArgumentAnalysis(
  473. '$a',
  474. 9,
  475. null,
  476. new TypeAnalysis(
  477. '\Foo\Bar',
  478. 3,
  479. 7
  480. )
  481. ),
  482. ]];
  483. }
  484. }
  485. public function provideFunctionsWithReturnTypeCases()
  486. {
  487. yield ['<?php function(){};', 1, null];
  488. }
  489. public function provideFunctionsWithReturnTypePhp70Cases()
  490. {
  491. yield ['<?php function($a): array {};', 1, new TypeAnalysis('array', 7, 7)];
  492. yield ['<?php function($a): \Foo\Bar {};', 1, new TypeAnalysis('\Foo\Bar', 7, 10)];
  493. yield ['<?php function($a): /* not sure if really an array */array {};', 1, new TypeAnalysis('array', 8, 8)];
  494. if (\PHP_VERSION_ID < 80000) {
  495. yield ['<?php function($a): \Foo/** TODO: change to something else */\Bar {};', 1, new TypeAnalysis('\Foo\Bar', 7, 11)];
  496. }
  497. }
  498. /**
  499. * @param string $code
  500. * @param int $methodIndex
  501. * @param array $expected
  502. *
  503. * @dataProvider provideFunctionsWithArgumentsPhp74Cases
  504. * @requires PHP 7.4
  505. */
  506. public function testFunctionArgumentInfoPhp74($code, $methodIndex, $expected)
  507. {
  508. $tokens = Tokens::fromCode($code);
  509. $analyzer = new FunctionsAnalyzer();
  510. static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
  511. }
  512. public function provideFunctionsWithArgumentsPhp74Cases()
  513. {
  514. $tests = [
  515. ['<?php fn() => null;', 1, []],
  516. ['<?php fn($a) => null;', 1, [
  517. '$a' => new ArgumentAnalysis(
  518. '$a',
  519. 3,
  520. null,
  521. null
  522. ),
  523. ]],
  524. ['<?php fn($a, $b) => null;', 1, [
  525. '$a' => new ArgumentAnalysis(
  526. '$a',
  527. 3,
  528. null,
  529. null
  530. ),
  531. '$b' => new ArgumentAnalysis(
  532. '$b',
  533. 6,
  534. null,
  535. null
  536. ),
  537. ]],
  538. ['<?php fn($a, $b = array(1,2), $c = 3) => null;', 1, [
  539. '$a' => new ArgumentAnalysis(
  540. '$a',
  541. 3,
  542. null,
  543. null
  544. ),
  545. '$b' => new ArgumentAnalysis(
  546. '$b',
  547. 6,
  548. 'array(1,2)',
  549. null
  550. ),
  551. '$c' => new ArgumentAnalysis(
  552. '$c',
  553. 18,
  554. '3',
  555. null
  556. ),
  557. ]],
  558. ['<?php fn(array $a = array()) => null;', 1, [
  559. '$a' => new ArgumentAnalysis(
  560. '$a',
  561. 5,
  562. 'array()',
  563. new TypeAnalysis(
  564. 'array',
  565. 3,
  566. 3
  567. )
  568. ),
  569. ]],
  570. ['<?php fn(array ... $a) => null;', 1, [
  571. '$a' => new ArgumentAnalysis(
  572. '$a',
  573. 7,
  574. null,
  575. new TypeAnalysis(
  576. 'array',
  577. 3,
  578. 3
  579. )
  580. ),
  581. ]],
  582. ['<?php fn(\Foo\Bar $a) => null;', 1, [
  583. '$a' => new ArgumentAnalysis(
  584. '$a',
  585. 8,
  586. null,
  587. new TypeAnalysis(
  588. '\Foo\Bar',
  589. 3,
  590. 6
  591. )
  592. ),
  593. ]],
  594. ];
  595. foreach ($tests as $index => $test) {
  596. yield $index => $test;
  597. }
  598. if (\PHP_VERSION_ID < 80000) {
  599. yield ['<?php fn(\Foo/** TODO: change to something else */\Bar $a) => null;', 1, [
  600. '$a' => new ArgumentAnalysis(
  601. '$a',
  602. 9,
  603. null,
  604. new TypeAnalysis(
  605. '\Foo\Bar',
  606. 3,
  607. 7
  608. )
  609. ),
  610. ]];
  611. }
  612. }
  613. /**
  614. * @param string $code
  615. * @param int $methodIndex
  616. * @param array $expected
  617. *
  618. * @dataProvider provideFunctionsWithReturnTypePhp74Cases
  619. * @requires PHP 7.4
  620. */
  621. public function testFunctionReturnTypeInfoPhp74($code, $methodIndex, $expected)
  622. {
  623. $tokens = Tokens::fromCode($code);
  624. $analyzer = new FunctionsAnalyzer();
  625. $actual = $analyzer->getFunctionReturnType($tokens, $methodIndex);
  626. static::assertSame(serialize($expected), serialize($actual));
  627. }
  628. public function provideFunctionsWithReturnTypePhp74Cases()
  629. {
  630. yield ['<?php fn() => null;', 1, null];
  631. yield ['<?php fn(array $a) => null;', 1, null];
  632. yield ['<?php fn($a): array => null;', 1, new TypeAnalysis('array', 7, 7)];
  633. yield ['<?php fn($a): \Foo\Bar => null;', 1, new TypeAnalysis('\Foo\Bar', 7, 10)];
  634. yield ['<?php fn($a): /* not sure if really an array */array => null;', 1, new TypeAnalysis('array', 8, 8)];
  635. if (\PHP_VERSION_ID < 80000) {
  636. yield ['<?php fn($a): \Foo/** TODO: change to something else */\Bar => null;', 1, new TypeAnalysis('\Foo\Bar', 7, 11)];
  637. }
  638. }
  639. /**
  640. * @param bool $isTheSameClassCall
  641. * @param string $code
  642. * @param int $index
  643. *
  644. * @dataProvider provideIsTheSameClassCallCases
  645. */
  646. public function testIsTheSameClassCall($isTheSameClassCall, $code, $index)
  647. {
  648. $tokens = Tokens::fromCode($code);
  649. $analyzer = new FunctionsAnalyzer();
  650. static::assertSame($isTheSameClassCall, $analyzer->isTheSameClassCall($tokens, $index));
  651. }
  652. public function provideIsTheSameClassCallCases()
  653. {
  654. $template = '<?php
  655. class Foo {
  656. public function methodOne() {
  657. $x = %sotherMethod(1, 2, 3);
  658. }
  659. }
  660. ';
  661. yield [
  662. false,
  663. sprintf($template, '$this->'),
  664. -1,
  665. ];
  666. // 24 is index of "otherMethod" token
  667. for ($i = 0; $i < 40; ++$i) {
  668. yield [
  669. 24 === $i,
  670. sprintf($template, '$this->'),
  671. $i,
  672. ];
  673. yield [
  674. 24 === $i,
  675. sprintf($template, 'self::'),
  676. $i,
  677. ];
  678. yield [
  679. 24 === $i,
  680. sprintf($template, 'static::'),
  681. $i,
  682. ];
  683. }
  684. yield [
  685. true,
  686. sprintf($template, '$THIS->'),
  687. 24,
  688. ];
  689. yield [
  690. false,
  691. sprintf($template, '$notThis->'),
  692. 24,
  693. ];
  694. yield [
  695. false,
  696. sprintf($template, 'Bar::'),
  697. 24,
  698. ];
  699. }
  700. /**
  701. * @param string $code
  702. * @param int $methodIndex
  703. * @param array $expected
  704. *
  705. * @dataProvider provideFunctionsWithArgumentsPhp80Cases
  706. * @requires PHP 8.0
  707. */
  708. public function testFunctionArgumentInfoPhp80($code, $methodIndex, $expected)
  709. {
  710. $tokens = Tokens::fromCode($code);
  711. $analyzer = new FunctionsAnalyzer();
  712. static::assertSame(serialize($expected), serialize($analyzer->getFunctionArguments($tokens, $methodIndex)));
  713. }
  714. public function provideFunctionsWithArgumentsPhp80Cases()
  715. {
  716. yield ['<?php function($aa,){};', 1, [
  717. '$aa' => new ArgumentAnalysis(
  718. '$aa',
  719. 3,
  720. null,
  721. null
  722. ),
  723. ]];
  724. yield ['<?php fn($a, $bc ,) => null;', 1, [
  725. '$a' => new ArgumentAnalysis(
  726. '$a',
  727. 3,
  728. null,
  729. null
  730. ),
  731. '$bc' => new ArgumentAnalysis(
  732. '$bc',
  733. 6,
  734. null,
  735. null
  736. ),
  737. ]];
  738. }
  739. }