NativeFunctionInvocationFixerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
  13. use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
  14. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  15. use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
  16. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  17. /**
  18. * @author Andreas Möller <am@localheinz.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer
  23. */
  24. final class NativeFunctionInvocationFixerTest extends AbstractFixerTestCase
  25. {
  26. public function testConfigureRejectsUnknownConfigurationKey(): void
  27. {
  28. $key = 'foo';
  29. $this->expectException(InvalidConfigurationException::class);
  30. $this->expectExceptionMessage(sprintf(
  31. '[native_function_invocation] Invalid configuration: The option "%s" does not exist.',
  32. $key
  33. ));
  34. $this->fixer->configure([
  35. $key => 'bar',
  36. ]);
  37. }
  38. /**
  39. * @dataProvider provideInvalidConfigurationElementCases
  40. *
  41. * @param mixed $element
  42. */
  43. public function testConfigureRejectsInvalidConfigurationElement($element): void
  44. {
  45. $this->expectException(InvalidConfigurationException::class);
  46. $this->expectExceptionMessage(sprintf(
  47. 'Each element must be a non-empty, trimmed string, got "%s" instead.',
  48. \is_object($element) ? \get_class($element) : \gettype($element)
  49. ));
  50. $this->fixer->configure([
  51. 'exclude' => [
  52. $element,
  53. ],
  54. ]);
  55. }
  56. public function provideInvalidConfigurationElementCases(): array
  57. {
  58. return [
  59. 'null' => [null],
  60. 'false' => [false],
  61. 'true' => [false],
  62. 'int' => [1],
  63. 'array' => [[]],
  64. 'float' => [0.1],
  65. 'object' => [new \stdClass()],
  66. 'not-trimmed' => [' is_string '],
  67. ];
  68. }
  69. /**
  70. * @param string[] $include
  71. *
  72. * @dataProvider provideConfigureIncludeSetsCases
  73. */
  74. public function testConfigureIncludeSets(
  75. array $include,
  76. ?string $expectedExceptionClass = null,
  77. ?string $expectedExceptionMessage = null
  78. ): void {
  79. if (null !== $expectedExceptionClass) {
  80. $this->expectException($expectedExceptionClass);
  81. $this->expectExceptionMessageMatches(sprintf('#^%s$#', preg_quote($expectedExceptionMessage, '#')));
  82. }
  83. $this->fixer->configure(['include' => $include]);
  84. if (null === $expectedExceptionClass) {
  85. $this->addToAssertionCount(1);
  86. }
  87. }
  88. public function provideConfigureIncludeSetsCases(): array
  89. {
  90. return [
  91. [['foo', 'bar']],
  92. [[NativeFunctionInvocationFixer::SET_ALL]],
  93. [[NativeFunctionInvocationFixer::SET_ALL, 'bar']],
  94. [
  95. ['@xxx'],
  96. InvalidFixerConfigurationException::class,
  97. '[native_function_invocation] Invalid configuration: Unknown set "@xxx", known sets are "@all", "@internal", "@compiler_optimized".',
  98. ],
  99. [
  100. [' x '],
  101. InvalidFixerConfigurationException::class,
  102. '[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
  103. ],
  104. ];
  105. }
  106. public function testConfigureResetsExclude(): void
  107. {
  108. $this->fixer->configure([
  109. 'exclude' => [
  110. 'is_string',
  111. ],
  112. ]);
  113. $before = <<<'PHP'
  114. <?php
  115. namespace WithClassNotPrefixed;
  116. class Bar
  117. {
  118. public function baz($foo)
  119. {
  120. if (isset($foo)) {
  121. is_string($foo);
  122. }
  123. }
  124. }
  125. PHP;
  126. $after = <<<'PHP'
  127. <?php
  128. namespace WithClassNotPrefixed;
  129. class Bar
  130. {
  131. public function baz($foo)
  132. {
  133. if (isset($foo)) {
  134. \is_string($foo);
  135. }
  136. }
  137. }
  138. PHP;
  139. $this->doTest($before);
  140. $this->fixer->configure([]);
  141. $this->doTest($after, $before);
  142. }
  143. /**
  144. * @dataProvider provideFixWithDefaultConfigurationCases
  145. */
  146. public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void
  147. {
  148. $this->doTest($expected, $input);
  149. }
  150. public function provideFixWithDefaultConfigurationCases(): array
  151. {
  152. return [
  153. [
  154. '<?php
  155. \is_string($foo);
  156. ',
  157. ],
  158. [
  159. '<?php
  160. \is_string($foo);
  161. ',
  162. '<?php
  163. is_string($foo);
  164. ',
  165. ],
  166. [
  167. '<?php
  168. class Foo
  169. {
  170. public function bar($foo)
  171. {
  172. return \is_string($foo);
  173. }
  174. }
  175. ',
  176. ],
  177. [
  178. '<?php
  179. json_encode($foo);
  180. \strlen($foo);
  181. ',
  182. '<?php
  183. json_encode($foo);
  184. strlen($foo);
  185. ',
  186. ],
  187. [
  188. '<?php
  189. class Foo
  190. {
  191. public function bar($foo)
  192. {
  193. return \IS_STRING($foo);
  194. }
  195. }
  196. ',
  197. '<?php
  198. class Foo
  199. {
  200. public function bar($foo)
  201. {
  202. return IS_STRING($foo);
  203. }
  204. }
  205. ',
  206. ],
  207. 'fix multiple calls in single code' => [
  208. '<?php
  209. json_encode($foo);
  210. \strlen($foo);
  211. \strlen($foo);
  212. ',
  213. '<?php
  214. json_encode($foo);
  215. strlen($foo);
  216. strlen($foo);
  217. ',
  218. ],
  219. ];
  220. }
  221. /**
  222. * @dataProvider provideFixWithConfiguredExcludeCases
  223. */
  224. public function testFixWithConfiguredExclude(string $expected, ?string $input = null): void
  225. {
  226. $this->fixer->configure([
  227. 'exclude' => [
  228. 'is_string',
  229. ],
  230. ]);
  231. $this->doTest($expected, $input);
  232. }
  233. public function provideFixWithConfiguredExcludeCases(): array
  234. {
  235. return [
  236. [
  237. '<?php
  238. is_string($foo);
  239. ',
  240. ],
  241. [
  242. '<?php
  243. class Foo
  244. {
  245. public function bar($foo)
  246. {
  247. return is_string($foo);
  248. }
  249. }
  250. ',
  251. ],
  252. ];
  253. }
  254. /**
  255. * @dataProvider provideFixWithNamespaceConfigurationCases
  256. */
  257. public function testFixWithNamespaceConfiguration(string $expected, ?string $input = null, string $scope = 'namespaced'): void
  258. {
  259. $this->fixer->configure(['scope' => $scope]);
  260. $this->doTest($expected, $input);
  261. }
  262. public function provideFixWithNamespaceConfigurationCases(): array
  263. {
  264. return [
  265. [
  266. '<?php echo count([1]);',
  267. ],
  268. [
  269. '<?php
  270. namespace space1 { ?>
  271. <?php echo \count([2]) ?>
  272. <?php }namespace {echo count([1]);}
  273. ',
  274. '<?php
  275. namespace space1 { ?>
  276. <?php echo count([2]) ?>
  277. <?php }namespace {echo count([1]);}
  278. ',
  279. ],
  280. [
  281. '<?php
  282. namespace Bar {
  283. echo \strLEN("in 1");
  284. }
  285. namespace {
  286. echo strlen("out 1");
  287. }
  288. namespace {
  289. echo strlen("out 2");
  290. }
  291. namespace Bar{
  292. echo \strlen("in 2");
  293. }
  294. namespace {
  295. echo strlen("out 3");
  296. }
  297. ',
  298. '<?php
  299. namespace Bar {
  300. echo strLEN("in 1");
  301. }
  302. namespace {
  303. echo strlen("out 1");
  304. }
  305. namespace {
  306. echo strlen("out 2");
  307. }
  308. namespace Bar{
  309. echo strlen("in 2");
  310. }
  311. namespace {
  312. echo strlen("out 3");
  313. }
  314. ',
  315. ],
  316. [
  317. '<?php
  318. namespace space11 ?>
  319. <?php
  320. echo \strlen(__NAMESPACE__);
  321. namespace space2;
  322. echo \strlen(__NAMESPACE__);
  323. ',
  324. '<?php
  325. namespace space11 ?>
  326. <?php
  327. echo strlen(__NAMESPACE__);
  328. namespace space2;
  329. echo strlen(__NAMESPACE__);
  330. ',
  331. ],
  332. [
  333. '<?php namespace PhpCsFixer\Tests\Fixer\Casing;\count([1]);',
  334. '<?php namespace PhpCsFixer\Tests\Fixer\Casing;count([1]);',
  335. ],
  336. [
  337. '<?php
  338. namespace Space12;
  339. echo \count([1]);
  340. namespace Space2;
  341. echo \count([1]);
  342. ?>
  343. ',
  344. '<?php
  345. namespace Space12;
  346. echo count([1]);
  347. namespace Space2;
  348. echo count([1]);
  349. ?>
  350. ',
  351. ],
  352. [
  353. '<?php namespace {echo strlen("out 2");}',
  354. ],
  355. [
  356. '<?php
  357. namespace space13 {
  358. echo \strlen("in 1");
  359. }
  360. namespace space2 {
  361. echo \strlen("in 2");
  362. }
  363. namespace { // global
  364. echo strlen("global 1");
  365. }
  366. ',
  367. '<?php
  368. namespace space13 {
  369. echo strlen("in 1");
  370. }
  371. namespace space2 {
  372. echo strlen("in 2");
  373. }
  374. namespace { // global
  375. echo strlen("global 1");
  376. }
  377. ',
  378. ],
  379. [
  380. '<?php
  381. namespace space1 {
  382. echo \count([1]);
  383. }
  384. namespace {
  385. echo \count([1]);
  386. }
  387. ',
  388. '<?php
  389. namespace space1 {
  390. echo count([1]);
  391. }
  392. namespace {
  393. echo count([1]);
  394. }
  395. ',
  396. 'all',
  397. ],
  398. ];
  399. }
  400. /**
  401. * @dataProvider provideFixWithConfiguredIncludeCases
  402. */
  403. public function testFixWithConfiguredInclude(string $expected, ?string $input = null, array $configuration = []): void
  404. {
  405. $this->fixer->configure($configuration);
  406. $this->doTest($expected, $input);
  407. }
  408. public function provideFixWithConfiguredIncludeCases(): \Generator
  409. {
  410. yield from [
  411. 'include set + 1, exclude 1' => [
  412. '<?php
  413. echo \count([1]);
  414. \some_other($a, 3);
  415. echo strlen($a);
  416. not_me();
  417. ',
  418. '<?php
  419. echo count([1]);
  420. some_other($a, 3);
  421. echo strlen($a);
  422. not_me();
  423. ',
  424. [
  425. 'include' => [NativeFunctionInvocationFixer::SET_INTERNAL, 'some_other'],
  426. 'exclude' => ['strlen'],
  427. ],
  428. ],
  429. 'include @all' => [
  430. '<?php
  431. echo \count([1]);
  432. \some_other($a, 3);
  433. echo \strlen($a);
  434. \me_as_well();
  435. ',
  436. '<?php
  437. echo count([1]);
  438. some_other($a, 3);
  439. echo strlen($a);
  440. me_as_well();
  441. ',
  442. [
  443. 'include' => [NativeFunctionInvocationFixer::SET_ALL],
  444. ],
  445. ],
  446. 'include @compiler_optimized' => [
  447. '<?php
  448. // do not fix
  449. $a = strrev($a);
  450. $a .= str_repeat($a, 4);
  451. $b = already_prefixed_function();
  452. // fix
  453. $c = \get_class($d);
  454. $e = \intval($f);
  455. ',
  456. '<?php
  457. // do not fix
  458. $a = strrev($a);
  459. $a .= str_repeat($a, 4);
  460. $b = \already_prefixed_function();
  461. // fix
  462. $c = get_class($d);
  463. $e = intval($f);
  464. ',
  465. [
  466. 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
  467. ],
  468. ],
  469. [
  470. '<?php class Foo {
  471. public function & strlen($name) {
  472. }
  473. }
  474. ',
  475. ],
  476. 'scope namespaced and strict enabled' => [
  477. '<?php
  478. $a = not_compiler_optimized_function();
  479. $b = intval($c);
  480. ',
  481. '<?php
  482. $a = \not_compiler_optimized_function();
  483. $b = \intval($c);
  484. ',
  485. [
  486. 'scope' => 'namespaced',
  487. 'strict' => true,
  488. ],
  489. ],
  490. [
  491. '<?php
  492. use function foo\json_decode;
  493. json_decode($base);
  494. ',
  495. null,
  496. [
  497. 'include' => [NativeFunctionInvocationFixer::SET_ALL],
  498. ],
  499. ],
  500. ];
  501. if (\PHP_VERSION_ID < 80000) {
  502. yield 'include @compiler_optimized with strict enabled' => [
  503. '<?php
  504. $a = not_compiler_optimized_function();
  505. $b = not_compiler_optimized_function();
  506. $c = \intval($d);
  507. ',
  508. '<?php
  509. $a = \not_compiler_optimized_function();
  510. $b = \ not_compiler_optimized_function();
  511. $c = intval($d);
  512. ',
  513. [
  514. 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
  515. 'strict' => true,
  516. ],
  517. ];
  518. }
  519. }
  520. /**
  521. * @requires PHP 7.3
  522. */
  523. public function testFix73(): void
  524. {
  525. $this->doTest(
  526. '<?php $name = \get_class($foo, );',
  527. '<?php $name = get_class($foo, );'
  528. );
  529. }
  530. /**
  531. * @requires PHP <8.0
  532. */
  533. public function testFixPrePHP80(): void
  534. {
  535. $this->doTest(
  536. '<?php
  537. echo \/**/strlen($a);
  538. echo \ strlen($a);
  539. echo \#
  540. #
  541. strlen($a);
  542. echo \strlen($a);
  543. ',
  544. '<?php
  545. echo \/**/strlen($a);
  546. echo \ strlen($a);
  547. echo \#
  548. #
  549. strlen($a);
  550. echo strlen($a);
  551. '
  552. );
  553. }
  554. /**
  555. * @dataProvider provideFix80Cases
  556. * @requires PHP 8.0
  557. */
  558. public function testFix80(string $expected, ?string $input = null, array $config = []): void
  559. {
  560. $this->fixer->configure($config);
  561. $this->doTest($expected, $input);
  562. }
  563. public function provideFix80Cases(): \Generator
  564. {
  565. yield 'attribute and strict' => [
  566. '<?php
  567. #[\Attribute(\Attribute::TARGET_CLASS)]
  568. class Foo {}
  569. ',
  570. null,
  571. ['strict' => true],
  572. ];
  573. yield 'null safe operator' => ['<?php $x?->count();'];
  574. yield 'multiple function-calls-like in attribute' => [
  575. '<?php
  576. #[Foo(), Bar(), Baz()]
  577. class Foo {}
  578. ',
  579. null,
  580. ['include' => ['@all']],
  581. ];
  582. }
  583. }