NoUnsetOnPropertyFixerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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\LanguageConstruct;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Gert de Pagter <BackEndTea@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\LanguageConstruct\NoUnsetOnPropertyFixer
  20. */
  21. final class NoUnsetOnPropertyFixerTest 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. public static function provideFixCases(): iterable
  31. {
  32. yield 'It replaces an unset on a property with = null' => [
  33. '<?php $foo->bar = null;',
  34. '<?php unset($foo->bar);',
  35. ];
  36. yield 'It replaces an unset on a property with = null II' => [
  37. '<?php $foo->bar = null ;',
  38. '<?php unset($foo->bar );',
  39. ];
  40. yield 'It replaces an unset on a static property with = null' => [
  41. '<?php TestClass::$bar = null;',
  42. '<?php unset(TestClass::$bar);',
  43. ];
  44. yield 'It does not replace unset on a variable with = null' => [
  45. '<?php $b->a; unset($foo);',
  46. ];
  47. yield 'It replaces multiple unsets on variables with = null' => [
  48. '<?php $foo->bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;',
  49. '<?php unset($foo->bar, $bar->foo, $bar->baz, $a->ba);',
  50. ];
  51. yield 'It replaces multiple unsets, but not those that arent properties' => [
  52. '<?php $foo->bar = null; $bar->foo = null; unset($bar);',
  53. '<?php unset($foo->bar, $bar->foo, $bar);',
  54. ];
  55. yield 'It replaces multiple unsets, but not those that arent properties in multiple places' => [
  56. '<?php unset($foo); $bar->foo = null; unset($bar);',
  57. '<?php unset($foo, $bar->foo, $bar);',
  58. ];
  59. yield 'It replaces $this -> and self:: replacements' => [
  60. '<?php $this->bar = null; self::$foo = null; unset($bar);',
  61. '<?php unset($this->bar, self::$foo, $bar);',
  62. ];
  63. yield 'It does not replace unsets on arrays' => [
  64. '<?php unset($bar->foo[0]);',
  65. ];
  66. yield 'It works in a more complex unset' => [
  67. '<?php unset($bar->foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);',
  68. '<?php unset($bar->foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b);',
  69. ];
  70. yield 'It works with consecutive unsets' => [
  71. '<?php $foo->bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;',
  72. '<?php unset($foo->bar, $foo, $bar, $baz, $this->ab);',
  73. ];
  74. yield 'It works when around messy whitespace' => [
  75. '<?php
  76. unset($a); $this->b = null;
  77. $this->a = null; unset($b);
  78. ',
  79. '<?php
  80. unset($a, $this->b);
  81. unset($this->a, $b);
  82. ',
  83. ];
  84. yield 'It works with weirdly placed comments' => [
  85. '<?php unset/*foo*/(/*bar*/$bar->foo[0]); self::$foo = null/*baz*/; /*hello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);
  86. unset/*foo*/(/*bar*/$bar);',
  87. '<?php unset/*foo*/(/*bar*/$bar->foo[0], self::$foo/*baz*/, /*hello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b);
  88. unset/*foo*/(/*bar*/$bar);',
  89. ];
  90. yield 'It does not mess with consecutive unsets' => [
  91. '<?php unset($a, $b, $c);
  92. $this->a = null;',
  93. '<?php unset($a, $b, $c);
  94. unset($this->a);',
  95. ];
  96. yield 'It does not replace function call with class constant inside' => [
  97. '<?php unset($foos[array_search(BadFoo::NAME, $foos)]);',
  98. ];
  99. yield 'It does not replace function call with class constant and property inside' => [
  100. '<?php unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)]);',
  101. ];
  102. if (\PHP_VERSION_ID < 8_00_00) {
  103. yield 'It does not replace unsets on arrays with special notation' => [
  104. '<?php unset($bar->foo{0});',
  105. ];
  106. }
  107. yield 'It does not break complex expressions' => [
  108. '<?php
  109. unset(a()[b()["a"]]);
  110. unset(a()[b()]);
  111. unset(a()["a"]);
  112. unset(c($a)->a);
  113. ',
  114. ];
  115. yield 'It replaces an unset on a property with = null 1' => [
  116. '<?php $foo->bar = null;',
  117. '<?php unset($foo->bar,);',
  118. ];
  119. yield 'It replaces multiple unsets, but not those that arent properties 1' => [
  120. '<?php $foo->bar = null; $bar->foo = null; unset($bar,);',
  121. '<?php unset($foo->bar, $bar->foo, $bar,);',
  122. ];
  123. yield 'It replaces an unset on a static property with = null 1' => [
  124. '<?php TestClass::$bar = null;',
  125. '<?php unset(TestClass::$bar,);',
  126. ];
  127. yield 'It does not replace unset on a variable with = null 1' => [
  128. '<?php $b->a; unset($foo,);',
  129. ];
  130. yield 'It replaces multiple unsets on variables with = null 1' => [
  131. '<?php $foo->bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;',
  132. '<?php unset($foo->bar, $bar->foo, $bar->baz, $a->ba,);',
  133. ];
  134. yield 'It replaces multiple unsets, but not those that arent properties in multiple places 1' => [
  135. '<?php unset($foo); $bar->foo = null; unset($bar,);',
  136. '<?php unset($foo, $bar->foo, $bar,);',
  137. ];
  138. yield 'It replaces $this -> and self:: replacements 1' => [
  139. '<?php $this->bar = null; self::$foo = null; unset($bar,);',
  140. '<?php unset($this->bar, self::$foo, $bar,);',
  141. ];
  142. yield 'It does not replace unsets on arrays 1' => [
  143. '<?php unset($bar->foo[0],);',
  144. ];
  145. yield 'It works in a more complex unset 1' => [
  146. '<?php unset($bar->foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,);',
  147. '<?php unset($bar->foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b,);',
  148. ];
  149. yield 'It works with consecutive unsets 1' => [
  150. '<?php $foo->bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;',
  151. '<?php unset($foo->bar, $foo, $bar, $baz, $this->ab,);',
  152. ];
  153. yield 'It works when around messy whitespace 1' => [
  154. '<?php
  155. unset($a); $this->b = null;
  156. $this->a = null; unset($b,);
  157. ',
  158. '<?php
  159. unset($a, $this->b,);
  160. unset($this->a, $b,);
  161. ',
  162. ];
  163. yield 'It works with weirdly placed comments 11' => [
  164. '<?php unset/*foo*/(/*bar*/$bar->foo[0]); self::$foo = null/*baz*/; /*hello*/\Test\Baz::$fooBar = null/*comment*/; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b,);
  165. unset/*foo*/(/*bar*/$bar,);',
  166. '<?php unset/*foo*/(/*bar*/$bar->foo[0], self::$foo/*baz*/, /*hello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b,);
  167. unset/*foo*/(/*bar*/$bar,);',
  168. ];
  169. yield 'It does not mess with consecutive unsets 1' => [
  170. '<?php unset($a, $b, $c,);
  171. $this->a = null;',
  172. '<?php unset($a, $b, $c,);
  173. unset($this->a,);',
  174. ];
  175. yield 'It does not replace function call with class constant inside 1' => [
  176. '<?php unset($foos[array_search(BadFoo::NAME, $foos)],);',
  177. ];
  178. yield 'It does not replace function call with class constant and property inside 1' => [
  179. '<?php unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)],);',
  180. ];
  181. yield [
  182. '<?php $foo->bar = null ;',
  183. '<?php unset($foo->bar, );',
  184. ];
  185. yield [
  186. '<?php $foo->bar = null ;',
  187. '<?php unset($foo->bar ,);',
  188. ];
  189. yield [
  190. '<?php $foo->bar = null ;',
  191. '<?php unset($foo->bar , );',
  192. ];
  193. if (\PHP_VERSION_ID < 8_00_00) {
  194. yield 'It does not replace unsets on arrays with special notation 1' => [
  195. '<?php unset($bar->foo{0},);',
  196. ];
  197. }
  198. }
  199. /**
  200. * @dataProvider provideFixPre80Cases
  201. *
  202. * @requires PHP <8.0
  203. */
  204. public function testFixPre80(string $expected, string $input = null): void
  205. {
  206. $this->doTest($expected, $input);
  207. }
  208. public static function provideFixPre80Cases(): iterable
  209. {
  210. yield 'It does not break curly access expressions' => [
  211. '<?php unset(a(){"a"});',
  212. ];
  213. }
  214. }