StringLengthToEmptyFixerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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\StringNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\StringNotation\StringLengthToEmptyFixer>
  20. */
  21. final class StringLengthToEmptyFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<int|string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php $a = \'\' === $b;',
  37. '<?php $a = 0 === strlen($b);',
  38. ];
  39. yield 'casing' => [
  40. '<?php $a1 = \'\' === $b ?>',
  41. '<?php $a1 = 0 === \STRlen($b) ?>',
  42. ];
  43. yield 'nested' => [
  44. '<?php $a2 = \'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === foo(\'\' === $b))))));',
  45. '<?php $a2 = 0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen(foo(0 === strlen($b)))))))))))));',
  46. ];
  47. yield [
  48. '<?php $a3 = \'\' !== $b;',
  49. '<?php $a3 = 0 !== strlen($b);',
  50. ];
  51. yield [
  52. '<?php $a4 = 0 <= strlen($b);',
  53. ];
  54. yield [
  55. '<?php $a5 = \'\' === $b;',
  56. '<?php $a5 = 0 >= strlen($b);',
  57. ];
  58. yield [
  59. '<?php $a6 = \'\' !== $b;',
  60. '<?php $a6 = 0 < strlen($b);',
  61. ];
  62. yield [
  63. '<?php $a7 = 0 > strlen($b);',
  64. ];
  65. yield [
  66. '<?php $a8 = 1 === strlen($b);',
  67. ];
  68. yield [
  69. '<?php $a9 = 1 !== strlen($b);',
  70. ];
  71. yield [
  72. '<?php $a10 = \'\' !== $b;',
  73. '<?php $a10 = 1 <= strlen($b);',
  74. ];
  75. yield [
  76. '<?php $a11 = 1 >= strlen($b);',
  77. ];
  78. yield [
  79. '<?php $a12 = 1 < strlen($b);',
  80. ];
  81. yield [
  82. '<?php $a13 = \'\' === $b;',
  83. '<?php $a13 = 1 > strlen($b);',
  84. ];
  85. yield [
  86. '<?php $a14 = $b === \'\';',
  87. '<?php $a14 = strlen($b) === 0;',
  88. ];
  89. yield [
  90. '<?php $a15 = $b !== \'\';',
  91. '<?php $a15 = strlen($b) !== 0;',
  92. ];
  93. yield [
  94. '<?php $a16 = $b === \'\';',
  95. '<?php $a16 = strlen($b) <= 0;',
  96. ];
  97. yield [
  98. '<?php $a17 = strlen($b) >= 0;',
  99. ];
  100. yield [
  101. '<?php $a18 = strlen($b) < 0;',
  102. ];
  103. yield [
  104. '<?php $a19 = $b !== \'\';',
  105. '<?php $a19 = strlen($b) > 0;',
  106. ];
  107. yield [
  108. '<?php $a20 = strlen($b) === 1;',
  109. ];
  110. yield [
  111. '<?php $a21 = strlen($b) !== 1;',
  112. ];
  113. yield [
  114. '<?php $a22 = strlen($b) <= 1;',
  115. ];
  116. yield [
  117. '<?php $a23 = $b !== \'\';',
  118. '<?php $a23 = strlen($b) >= 1;',
  119. ];
  120. yield [
  121. '<?php $a24 = $b === \'\';',
  122. '<?php $a24 = strlen($b) < 1;',
  123. ];
  124. yield [
  125. '<?php $a25 = strlen($b) > 1;',
  126. ];
  127. yield [
  128. '<?php $e = 0 === foo() ? -1 : \'\' === $a;',
  129. '<?php $e = 0 === foo() ? -1 : 0 === strlen($a);',
  130. ];
  131. yield [
  132. '<?php $x = /* 1 */ $b /* 2 */ ->a !== \'\';',
  133. '<?php $x = strlen(/* 1 */ $b /* 2 */ ->a) >= 1;',
  134. ];
  135. yield [
  136. '<?php $y = $b[0] === \'\';',
  137. '<?php $y = strlen($b[0]) < 1;',
  138. ];
  139. yield [
  140. '<?php $y1 = $b[0]->$a[$a++](1) /* 1 */ === \'\';',
  141. '<?php $y1 = strlen($b[0]->$a[$a++](1) /* 1 */ ) < 1;',
  142. ];
  143. yield [
  144. '<?php $z = \'\' === $b[1]->foo(++$i, static function () use ($z){ return $z + 1;});',
  145. '<?php $z = 0 === strlen($b[1]->foo(++$i, static function () use ($z){ return $z + 1;}));',
  146. ];
  147. yield [
  148. '<?php if ((string) $node !== \'\') { echo 1; }',
  149. '<?php if (\strlen((string) $node) > 0) { echo 1; }',
  150. ];
  151. yield 'do not fix' => [
  152. '<?php
  153. //-----------------------------------
  154. // operator precedence
  155. $a01 = 0 === strlen($b) ** $c;
  156. $a03 = 0 === strlen($b) % $c;
  157. $a04 = 0 === strlen($b) / $c;
  158. $a05 = 0 === strlen($b) * $c;
  159. $a06 = 0 === strlen($b) + $c;
  160. $a07 = 0 === strlen($b) - $c;
  161. $a08 = 0 === strlen($b) . $c;
  162. $a09 = 0 === strlen($b) >> $c;
  163. $a10 = 0 === strlen($b) << $c;
  164. $a01n = strlen($b) === 0 ** $c;
  165. $a03n = strlen($b) === 0 % $c;
  166. $a04n = strlen($b) === 0 / $c;
  167. $a05n = strlen($b) === 0 * $c;
  168. $a06n = strlen($b) === 0 + $c;
  169. $a07n = strlen($b) === 0 - $c;
  170. $a08n = strlen($b) === 0 . $c;
  171. $a09n = strlen($b) === 0 >> $c;
  172. $a10n = strlen($b) === 0 << $c;
  173. $b = "a";
  174. $c = 0 === strlen($b) - 1;
  175. var_dump($c);
  176. $c = "" === $b - 1;
  177. var_dump($c);
  178. //-----------------------------------
  179. // type juggle
  180. $d = false;
  181. $e = 0 === strlen($d) ? -1 : 0;
  182. var_dump($e);
  183. $e = "" === $d ? -1 : 0;
  184. var_dump($e);
  185. //-----------------------------------
  186. // wrong argument count
  187. $f = strlen(1,2);
  188. $g = \strlen(1,2,3);
  189. //-----------------------------------
  190. // others
  191. $h = 0 === (string) strlen($b);
  192. $i = 0 === @strlen($b);
  193. $j = 0 === !strlen($b);
  194. $jj = 2 === strlen($b);
  195. $jk = __DIR__ === strlen($b);
  196. $jl = \'X\' !== strlen($b);
  197. $jj = strlen($b) === 2;
  198. $jk = strlen($b) === __DIR__;
  199. $jl = strlen($b) !== \'X\';
  200. //-----------------------------------
  201. // not global calls
  202. $k = 0 === $a->strlen($b);
  203. $l = 0 === Foo::strlen($b);
  204. //-----------------------------------
  205. // comments
  206. // $a = 0 === strlen($b);
  207. # $a = 0 === strlen($b);
  208. /* $a = 0 === strlen($b); */
  209. /** $a = 0 === strlen($b); */
  210. ',
  211. ];
  212. // cases where `(` and `)` must be kept
  213. yield [
  214. '<?php $e = ($a = trim($b)) !== \'\';',
  215. '<?php $e = \strlen($a = trim($b)) > 0;',
  216. ];
  217. yield [
  218. '<?php if (\'\' === ($value = foo())) { echo 2; }',
  219. '<?php if (0 === \strlen($value = foo())) { echo 2; }',
  220. ];
  221. yield [
  222. '<?php
  223. $a02 = 0 === strlen($b) instanceof stdClass;
  224. $a02n = strlen($b) === 0 instanceof stdClass;',
  225. ];
  226. }
  227. }