NoUnsetOnPropertyFixerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. return [
  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 static property with = null' => [
  40. '<?php TestClass::$bar = null;',
  41. '<?php unset(TestClass::$bar);',
  42. ],
  43. 'It does not replace unset on a variable with = null' => [
  44. '<?php $b->a; unset($foo);',
  45. ],
  46. 'It replaces multiple unsets on variables with = null' => [
  47. '<?php $foo->bar = null; $bar->foo = null; $bar->baz = null; $a->ba = null;',
  48. '<?php unset($foo->bar, $bar->foo, $bar->baz, $a->ba);',
  49. ],
  50. 'It replaces multiple unsets, but not those that arent properties' => [
  51. '<?php $foo->bar = null; $bar->foo = null; unset($bar);',
  52. '<?php unset($foo->bar, $bar->foo, $bar);',
  53. ],
  54. 'It replaces multiple unsets, but not those that arent properties in multiple places' => [
  55. '<?php unset($foo); $bar->foo = null; unset($bar);',
  56. '<?php unset($foo, $bar->foo, $bar);',
  57. ],
  58. 'It replaces $this -> and self:: replacements' => [
  59. '<?php $this->bar = null; self::$foo = null; unset($bar);',
  60. '<?php unset($this->bar, self::$foo, $bar);',
  61. ],
  62. 'It does not replace unsets on arrays' => [
  63. '<?php unset($bar->foo[0]);',
  64. ],
  65. 'It works in a more complex unset' => [
  66. '<?php unset($bar->foo[0]); self::$foo = null; \Test\Baz::$fooBar = null; unset($bar->foo[0]); $this->foo = null; unset($a); unset($b);',
  67. '<?php unset($bar->foo[0], self::$foo, \Test\Baz::$fooBar, $bar->foo[0], $this->foo, $a, $b);',
  68. ],
  69. 'It works with consecutive unsets' => [
  70. '<?php $foo->bar = null; unset($foo); unset($bar); unset($baz); $this->ab = null;',
  71. '<?php unset($foo->bar, $foo, $bar, $baz, $this->ab);',
  72. ],
  73. 'It does not replace unsets on arrays with special notation' => [
  74. '<?php unset($bar->foo{0});',
  75. ],
  76. 'It works when around messy whitespace' => [
  77. '<?php
  78. unset($a); $this->b = null;
  79. $this->a = null; unset($b);
  80. ',
  81. '<?php
  82. unset($a, $this->b);
  83. unset($this->a, $b);
  84. ',
  85. ],
  86. 'It works with weirdly placed comments' => [
  87. '<?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);
  88. unset/*foo*/(/*bar*/$bar);',
  89. '<?php unset/*foo*/(/*bar*/$bar->foo[0], self::$foo/*baz*/, /*ello*/\Test\Baz::$fooBar/*comment*/, $bar->foo[0], $this->foo, $a, $b);
  90. unset/*foo*/(/*bar*/$bar);',
  91. ],
  92. 'It does not mess with consecutive unsets' => [
  93. '<?php unset($a, $b, $c);
  94. $this->a = null;',
  95. '<?php unset($a, $b, $c);
  96. unset($this->a);',
  97. ],
  98. 'It does not replace function call with class constant inside' => [
  99. '<?php unset($foos[array_search(BadFoo::NAME, $foos)]);',
  100. ],
  101. 'It does not replace function call with class constant and property inside' => [
  102. '<?php unset($this->property[array_search(\Types::TYPE_RANDOM, $this->property)]);',
  103. ],
  104. ];
  105. }
  106. /**
  107. * @param string $expected
  108. * @param null|string $input
  109. *
  110. * @dataProvider provideFix70Cases
  111. * @requires PHP 7.0
  112. */
  113. public function testFix70($expected, $input = null)
  114. {
  115. $this->doTest($expected, $input);
  116. }
  117. public function provideFix70Cases()
  118. {
  119. return [
  120. 'It does not break complex expressions' => [
  121. '<?php
  122. unset(a()[b()["a"]]);
  123. unset(a()[b()]);
  124. unset(a()["a"]);
  125. unset(a(){"a"});
  126. unset(c($a)->a);
  127. ',
  128. ],
  129. ];
  130. }
  131. }