NoAliasFunctionsFixerTest.php 8.4 KB

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