NoUnsetOnPropertyFixerTest.php 9.8 KB

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