NoAliasFunctionsFixerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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\Alias;
  13. use PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Vladimir Reznichenko <kalessil@gmail.com>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Alias\NoAliasFunctionsFixer
  26. */
  27. final class NoAliasFunctionsFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @param _AutogeneratedInputConfiguration $configuration
  31. *
  32. * @dataProvider provideFixCases
  33. */
  34. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  35. {
  36. $this->fixer->configure($configuration);
  37. $this->doTest($expected, $input);
  38. }
  39. public static function provideFixCases(): iterable
  40. {
  41. $defaultSets = [
  42. '@internal',
  43. '@IMAP',
  44. '@pg',
  45. ];
  46. foreach (self::provideAllCases() as $set => $cases) {
  47. if (\in_array($set, $defaultSets, true)) {
  48. yield from $cases;
  49. } else {
  50. foreach ($cases as $case) {
  51. yield [$case[0]];
  52. }
  53. }
  54. }
  55. // static case to fix - in case previous generation is broken
  56. yield [
  57. '<?php is_int($a);',
  58. '<?php is_integer($a);',
  59. ];
  60. yield [
  61. '<?php socket_set_option($a, $b, $c, $d);',
  62. '<?php socket_setopt($a, $b, $c, $d);',
  63. ];
  64. yield 'dns -> mxrr' => [
  65. '<?php getmxrr();',
  66. '<?php dns_get_mx();',
  67. ];
  68. yield [
  69. '<?php $b=is_int(count(implode($b,$a)));',
  70. '<?php $b=is_integer(sizeof(join($b,$a)));',
  71. ];
  72. yield [
  73. '<?php
  74. interface JoinInterface
  75. {
  76. public function &join();
  77. }
  78. abstract class A
  79. {
  80. abstract public function join($a);
  81. public function is_integer($a)
  82. {
  83. $fputs = "is_double(\$a);\n"; // key_exists($b, $c);
  84. echo $fputs."\$is_writable";
  85. \B::close();
  86. Scope\is_long();
  87. namespace\is_long();
  88. $a->pos();
  89. new join();
  90. new \join();
  91. new ScopeB\join(mt_rand(0, 100));
  92. }
  93. }',
  94. ];
  95. yield '@internal' => [
  96. '<?php
  97. $a = rtrim($b);
  98. $a = imap_header($imap_stream, 1);
  99. mbereg_search_getregs();
  100. ',
  101. '<?php
  102. $a = chop($b);
  103. $a = imap_header($imap_stream, 1);
  104. mbereg_search_getregs();
  105. ',
  106. ['sets' => ['@internal']],
  107. ];
  108. yield '@IMAP' => [
  109. '<?php
  110. $a = chop($b);
  111. $a = imap_headerinfo($imap_stream, 1);
  112. mb_ereg_search_getregs();
  113. ',
  114. '<?php
  115. $a = chop($b);
  116. $a = imap_header($imap_stream, 1);
  117. mb_ereg_search_getregs();
  118. ',
  119. ['sets' => ['@IMAP']],
  120. ];
  121. yield '@mbreg' => [
  122. '<?php
  123. $a = chop($b);
  124. $a = imap_header($imap_stream, 1);
  125. mb_ereg_search_getregs();
  126. mktime();
  127. ',
  128. '<?php
  129. $a = chop($b);
  130. $a = imap_header($imap_stream, 1);
  131. mbereg_search_getregs();
  132. mktime();
  133. ',
  134. ['sets' => ['@mbreg']],
  135. ];
  136. yield '@all' => [
  137. '<?php
  138. $a = rtrim($b);
  139. $a = imap_headerinfo($imap_stream, 1);
  140. mb_ereg_search_getregs();
  141. time();
  142. time();
  143. $foo = exif_read_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  144. mktime($a);
  145. echo gmmktime(1, 2, 3, 4, 5, 6);
  146. ',
  147. '<?php
  148. $a = chop($b);
  149. $a = imap_header($imap_stream, 1);
  150. mbereg_search_getregs();
  151. mktime();
  152. gmmktime();
  153. $foo = read_exif_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  154. mktime($a);
  155. echo gmmktime(1, 2, 3, 4, 5, 6);
  156. ',
  157. ['sets' => ['@all']],
  158. ];
  159. yield '@IMAP, @mbreg' => [
  160. '<?php
  161. $a = chop($b);
  162. $a = imap_headerinfo($imap_stream, 1);
  163. mb_ereg_search_getregs();
  164. ',
  165. '<?php
  166. $a = chop($b);
  167. $a = imap_header($imap_stream, 1);
  168. mbereg_search_getregs();
  169. ',
  170. ['sets' => ['@IMAP', '@mbreg']],
  171. ];
  172. yield '@time' => [
  173. '<?php
  174. time();
  175. time();
  176. MKTIME($A);
  177. ECHO GMMKTIME(1, 2, 3, 4, 5, 6);
  178. ',
  179. '<?php
  180. MKTIME();
  181. GMMKTIME();
  182. MKTIME($A);
  183. ECHO GMMKTIME(1, 2, 3, 4, 5, 6);
  184. ',
  185. ['sets' => ['@time']],
  186. ];
  187. yield '@exif' => [
  188. '<?php
  189. $foo = exif_read_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  190. ',
  191. '<?php
  192. $foo = read_exif_data($filename, $sections_needed, $sub_arrays, $read_thumbnail);
  193. ',
  194. ['sets' => ['@exif']],
  195. ];
  196. foreach (self::provideAllCases() as $set => $cases) {
  197. foreach ($cases as $case) {
  198. yield [
  199. $case[0],
  200. $case[1] ?? null,
  201. ['sets' => [$set]],
  202. ];
  203. }
  204. }
  205. }
  206. /**
  207. * @dataProvider provideFix81Cases
  208. *
  209. * @requires PHP 8.1
  210. */
  211. public function testFix81(string $expected, ?string $input = null): void
  212. {
  213. $this->doTest($expected, $input);
  214. }
  215. /**
  216. * @return iterable<string, array{string}>
  217. */
  218. public static function provideFix81Cases(): iterable
  219. {
  220. yield 'simple 8.1' => [
  221. '<?php $a = is_double(...);',
  222. ];
  223. }
  224. private static function provideAllCases(): iterable
  225. {
  226. $reflectionConstant = new \ReflectionClassConstant(NoAliasFunctionsFixer::class, 'SETS');
  227. /** @var array<string, array<string, string>> $allAliases */
  228. $allAliases = $reflectionConstant->getValue();
  229. $sets = $allAliases;
  230. unset($sets['@time']); // Tested manually
  231. $sets = array_keys($sets);
  232. foreach ($sets as $set) {
  233. $aliases = $allAliases[$set];
  234. $cases = [];
  235. foreach ($aliases as $alias => $master) {
  236. // valid cases
  237. $cases[] = ["<?php \$smth->{$alias}(\$a);"];
  238. $cases[] = ["<?php {$alias}Smth(\$a);"];
  239. $cases[] = ["<?php smth_{$alias}(\$a);"];
  240. $cases[] = ["<?php new {$alias}(\$a);"];
  241. $cases[] = ["<?php new Smth\\{$alias}(\$a);"];
  242. $cases[] = ["<?php Smth\\{$alias}(\$a);"];
  243. $cases[] = ["<?php namespace\\{$alias}(\$a);"];
  244. $cases[] = ["<?php Smth::{$alias}(\$a);"];
  245. $cases[] = ["<?php new {$alias}\\smth(\$a);"];
  246. $cases[] = ["<?php {$alias}::smth(\$a);"];
  247. $cases[] = ["<?php {$alias}\\smth(\$a);"];
  248. $cases[] = ['<?php "SELECT ... '.$alias.'(\$a) ...";'];
  249. $cases[] = ['<?php "SELECT ... '.strtoupper($alias).'($a) ...";'];
  250. $cases[] = ["<?php 'test'.'{$alias}' . 'in concatenation';"];
  251. $cases[] = ['<?php "test" . "'.$alias.'"."in concatenation";'];
  252. $cases[] = [
  253. '<?php
  254. class '.ucfirst($alias).'ing
  255. {
  256. const '.$alias.' = 1;
  257. public function '.$alias.'($'.$alias.')
  258. {
  259. if (defined("'.$alias.'") || $'.$alias.' instanceof '.$alias.') {
  260. echo '.$alias.';
  261. }
  262. }
  263. }
  264. class '.$alias.' extends '.ucfirst($alias).'ing{
  265. const '.$alias.' = "'.$alias.'";
  266. }
  267. ',
  268. ];
  269. // cases to be fixed
  270. $cases[] = [
  271. "<?php {$master}(\$a);",
  272. "<?php {$alias}(\$a);",
  273. ];
  274. $cases[] = [
  275. "<?php \\{$master}(\$a);",
  276. "<?php \\{$alias}(\$a);",
  277. ];
  278. $cases[] = [
  279. "<?php {$master}
  280. (\$a);",
  281. "<?php {$alias}
  282. (\$a);",
  283. ];
  284. $cases[] = [
  285. "<?php /* foo */ {$master} /** bar */ (\$a);",
  286. "<?php /* foo */ {$alias} /** bar */ (\$a);",
  287. ];
  288. $cases[] = [
  289. "<?php a({$master}());",
  290. "<?php a({$alias}());",
  291. ];
  292. $cases[] = [
  293. "<?php a(\\{$master}());",
  294. "<?php a(\\{$alias}());",
  295. ];
  296. }
  297. yield $set => $cases;
  298. }
  299. }
  300. }