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 provideConfigureRejectsInvalidConfigurationElementCases
  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. get_debug_type($element)
  49. ));
  50. $this->fixer->configure([
  51. 'exclude' => [
  52. $element,
  53. ],
  54. ]);
  55. }
  56. public static function provideConfigureRejectsInvalidConfigurationElementCases(): iterable
  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 static function provideConfigureIncludeSetsCases(): iterable
  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" and "@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 static function provideFixWithDefaultConfigurationCases(): iterable
  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. '<?php $name = \get_class($foo, );',
  221. '<?php $name = get_class($foo, );',
  222. ],
  223. ];
  224. }
  225. /**
  226. * @dataProvider provideFixWithConfiguredExcludeCases
  227. */
  228. public function testFixWithConfiguredExclude(string $expected, ?string $input = null): void
  229. {
  230. $this->fixer->configure([
  231. 'exclude' => [
  232. 'is_string',
  233. ],
  234. ]);
  235. $this->doTest($expected, $input);
  236. }
  237. public static function provideFixWithConfiguredExcludeCases(): iterable
  238. {
  239. return [
  240. [
  241. '<?php
  242. is_string($foo);
  243. ',
  244. ],
  245. [
  246. '<?php
  247. class Foo
  248. {
  249. public function bar($foo)
  250. {
  251. return is_string($foo);
  252. }
  253. }
  254. ',
  255. ],
  256. ];
  257. }
  258. /**
  259. * @dataProvider provideFixWithNamespaceConfigurationCases
  260. */
  261. public function testFixWithNamespaceConfiguration(string $expected, ?string $input = null, string $scope = 'namespaced'): void
  262. {
  263. $this->fixer->configure(['scope' => $scope]);
  264. $this->doTest($expected, $input);
  265. }
  266. public static function provideFixWithNamespaceConfigurationCases(): iterable
  267. {
  268. return [
  269. [
  270. '<?php echo count([1]);',
  271. ],
  272. [
  273. '<?php
  274. namespace space1 { ?>
  275. <?php echo \count([2]) ?>
  276. <?php }namespace {echo count([1]);}
  277. ',
  278. '<?php
  279. namespace space1 { ?>
  280. <?php echo count([2]) ?>
  281. <?php }namespace {echo count([1]);}
  282. ',
  283. ],
  284. [
  285. '<?php
  286. namespace Bar {
  287. echo \strLEN("in 1");
  288. }
  289. namespace {
  290. echo strlen("out 1");
  291. }
  292. namespace {
  293. echo strlen("out 2");
  294. }
  295. namespace Bar{
  296. echo \strlen("in 2");
  297. }
  298. namespace {
  299. echo strlen("out 3");
  300. }
  301. ',
  302. '<?php
  303. namespace Bar {
  304. echo strLEN("in 1");
  305. }
  306. namespace {
  307. echo strlen("out 1");
  308. }
  309. namespace {
  310. echo strlen("out 2");
  311. }
  312. namespace Bar{
  313. echo strlen("in 2");
  314. }
  315. namespace {
  316. echo strlen("out 3");
  317. }
  318. ',
  319. ],
  320. [
  321. '<?php
  322. namespace space11 ?>
  323. <?php
  324. echo \strlen(__NAMESPACE__);
  325. namespace space2;
  326. echo \strlen(__NAMESPACE__);
  327. ',
  328. '<?php
  329. namespace space11 ?>
  330. <?php
  331. echo strlen(__NAMESPACE__);
  332. namespace space2;
  333. echo strlen(__NAMESPACE__);
  334. ',
  335. ],
  336. [
  337. '<?php namespace PhpCsFixer\Tests\Fixer\Casing;\count([1]);',
  338. '<?php namespace PhpCsFixer\Tests\Fixer\Casing;count([1]);',
  339. ],
  340. [
  341. '<?php
  342. namespace Space12;
  343. echo \count([1]);
  344. namespace Space2;
  345. echo \count([1]);
  346. ?>
  347. ',
  348. '<?php
  349. namespace Space12;
  350. echo count([1]);
  351. namespace Space2;
  352. echo count([1]);
  353. ?>
  354. ',
  355. ],
  356. [
  357. '<?php namespace {echo strlen("out 2");}',
  358. ],
  359. [
  360. '<?php
  361. namespace space13 {
  362. echo \strlen("in 1");
  363. }
  364. namespace space2 {
  365. echo \strlen("in 2");
  366. }
  367. namespace { // global
  368. echo strlen("global 1");
  369. }
  370. ',
  371. '<?php
  372. namespace space13 {
  373. echo strlen("in 1");
  374. }
  375. namespace space2 {
  376. echo strlen("in 2");
  377. }
  378. namespace { // global
  379. echo strlen("global 1");
  380. }
  381. ',
  382. ],
  383. [
  384. '<?php
  385. namespace space1 {
  386. echo \count([1]);
  387. }
  388. namespace {
  389. echo \count([1]);
  390. }
  391. ',
  392. '<?php
  393. namespace space1 {
  394. echo count([1]);
  395. }
  396. namespace {
  397. echo count([1]);
  398. }
  399. ',
  400. 'all',
  401. ],
  402. ];
  403. }
  404. /**
  405. * @param array<string, mixed> $configuration
  406. *
  407. * @dataProvider provideFixWithConfiguredIncludeCases
  408. */
  409. public function testFixWithConfiguredInclude(string $expected, ?string $input = null, array $configuration = []): void
  410. {
  411. $this->fixer->configure($configuration);
  412. $this->doTest($expected, $input);
  413. }
  414. public static function provideFixWithConfiguredIncludeCases(): iterable
  415. {
  416. yield 'include set + 1, exclude 1' => [
  417. '<?php
  418. echo \count([1]);
  419. \some_other($a, 3);
  420. echo strlen($a);
  421. not_me();
  422. ',
  423. '<?php
  424. echo count([1]);
  425. some_other($a, 3);
  426. echo strlen($a);
  427. not_me();
  428. ',
  429. [
  430. 'include' => [NativeFunctionInvocationFixer::SET_INTERNAL, 'some_other'],
  431. 'exclude' => ['strlen'],
  432. ],
  433. ];
  434. yield 'include @all' => [
  435. '<?php
  436. echo \count([1]);
  437. \some_other($a, 3);
  438. echo \strlen($a);
  439. \me_as_well();
  440. ',
  441. '<?php
  442. echo count([1]);
  443. some_other($a, 3);
  444. echo strlen($a);
  445. me_as_well();
  446. ',
  447. [
  448. 'include' => [NativeFunctionInvocationFixer::SET_ALL],
  449. ],
  450. ];
  451. yield 'include @compiler_optimized' => [
  452. '<?php
  453. // do not fix
  454. $a = strrev($a);
  455. $a .= str_repeat($a, 4);
  456. $b = already_prefixed_function();
  457. // fix
  458. $c = \get_class($d);
  459. $e = \intval($f);
  460. ',
  461. '<?php
  462. // do not fix
  463. $a = strrev($a);
  464. $a .= str_repeat($a, 4);
  465. $b = \already_prefixed_function();
  466. // fix
  467. $c = get_class($d);
  468. $e = intval($f);
  469. ',
  470. [
  471. 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
  472. ],
  473. ];
  474. yield [
  475. '<?php class Foo {
  476. public function & strlen($name) {
  477. }
  478. }
  479. ',
  480. ];
  481. yield 'scope namespaced and strict enabled' => [
  482. '<?php
  483. $a = not_compiler_optimized_function();
  484. $b = intval($c);
  485. ',
  486. '<?php
  487. $a = \not_compiler_optimized_function();
  488. $b = \intval($c);
  489. ',
  490. [
  491. 'scope' => 'namespaced',
  492. 'strict' => true,
  493. ],
  494. ];
  495. yield [
  496. '<?php
  497. use function foo\json_decode;
  498. json_decode($base);
  499. ',
  500. null,
  501. [
  502. 'include' => [NativeFunctionInvocationFixer::SET_ALL],
  503. ],
  504. ];
  505. if (\PHP_VERSION_ID < 8_00_00) {
  506. yield 'include @compiler_optimized with strict enabled' => [
  507. '<?php
  508. $a = not_compiler_optimized_function();
  509. $b = not_compiler_optimized_function();
  510. $c = \intval($d);
  511. ',
  512. '<?php
  513. $a = \not_compiler_optimized_function();
  514. $b = \ not_compiler_optimized_function();
  515. $c = intval($d);
  516. ',
  517. [
  518. 'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
  519. 'strict' => true,
  520. ],
  521. ];
  522. }
  523. }
  524. /**
  525. * @requires PHP <8.0
  526. */
  527. public function testFixPrePHP80(): void
  528. {
  529. $this->doTest(
  530. '<?php
  531. echo \/**/strlen($a);
  532. echo \ strlen($a);
  533. echo \#
  534. #
  535. strlen($a);
  536. echo \strlen($a);
  537. ',
  538. '<?php
  539. echo \/**/strlen($a);
  540. echo \ strlen($a);
  541. echo \#
  542. #
  543. strlen($a);
  544. echo strlen($a);
  545. '
  546. );
  547. }
  548. /**
  549. * @param array<string, mixed> $config
  550. *
  551. * @dataProvider provideFix80Cases
  552. *
  553. * @requires PHP 8.0
  554. */
  555. public function testFix80(string $expected, ?string $input = null, array $config = []): void
  556. {
  557. $this->fixer->configure($config);
  558. $this->doTest($expected, $input);
  559. }
  560. public static function provideFix80Cases(): iterable
  561. {
  562. yield 'attribute and strict' => [
  563. '<?php
  564. #[\Attribute(\Attribute::TARGET_CLASS)]
  565. class Foo {}
  566. ',
  567. null,
  568. ['strict' => true],
  569. ];
  570. yield 'null safe operator' => ['<?php $x?->count();'];
  571. yield 'multiple function-calls-like in attribute' => [
  572. '<?php
  573. #[Foo(), Bar(), Baz()]
  574. class Foo {}
  575. ',
  576. null,
  577. ['include' => ['@all']],
  578. ];
  579. }
  580. }