NoNullPropertyInitializationFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\ClassNotation;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author ntzm
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ClassNotation\NoNullPropertyInitializationFixer
  19. */
  20. final class NoNullPropertyInitializationFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null)
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. public function provideFixCases()
  33. {
  34. return [
  35. [
  36. '<?php class Foo { public $bar; }',
  37. '<?php class Foo { public $bar = null; }',
  38. ],
  39. [
  40. '<?php class Foo { protected $bar; }',
  41. '<?php class Foo { protected $bar = null; }',
  42. ],
  43. [
  44. '<?php class Foo { private $bar; }',
  45. '<?php class Foo { private $bar = null; }',
  46. ],
  47. [
  48. '<?php class Foo { var $bar; }',
  49. '<?php class Foo { var $bar = null; }',
  50. ],
  51. [
  52. '<?php class Foo { VAR $bar; }',
  53. '<?php class Foo { VAR $bar = null; }',
  54. ],
  55. [
  56. '<?php class Foo { public $bar; }',
  57. '<?php class Foo { public $bar = NULL; }',
  58. ],
  59. [
  60. '<?php class Foo { PUblic $bar; }',
  61. '<?php class Foo { PUblic $bar = nuLL; }',
  62. ],
  63. [
  64. '<?php trait Foo { public $bar; }',
  65. '<?php trait Foo { public $bar = nuLL; }',
  66. ],
  67. [
  68. '<?php class Foo { public $bar; }',
  69. '<?php class Foo { public $bar = \null; }',
  70. ],
  71. [
  72. '<?php class Foo {/* */public/* A */$bar/* B *//** C */;/* D */}',
  73. '<?php class Foo {/* */public/* A */$bar/* B */=/** C */null;/* D */}',
  74. ],
  75. [
  76. '<?php class Foo { public $bar; protected $baz; }',
  77. '<?php class Foo { public $bar = null; protected $baz = null; }',
  78. ],
  79. [
  80. '<?php class Foo { public $bar = \'null\'; }',
  81. ],
  82. [
  83. '<?php class Foo { public function bar() { return null; } }',
  84. ],
  85. [
  86. '<?php class Foo { protected $bar, $baz, $qux; }',
  87. '<?php class Foo { protected $bar = null, $baz = null, $qux = null; }',
  88. ],
  89. [
  90. '<?php class Foo { protected $bar, $baz = \'baz\', $qux; }',
  91. '<?php class Foo { protected $bar, $baz = \'baz\', $qux = null; }',
  92. ],
  93. [
  94. '<?php trait Foo { public $bar; } abstract class Bar { protected $bar, $baz = \'baz\', $qux; }',
  95. '<?php trait Foo { public $bar = null; } abstract class Bar { protected $bar, $baz = \'baz\', $qux = null; }',
  96. ],
  97. [
  98. '<?php class Foo { public function foo() { return null; } public $bar; public function baz() { return null; } }',
  99. '<?php class Foo { public function foo() { return null; } public $bar = null; public function baz() { return null; } }',
  100. ],
  101. [
  102. '<?php class#1
  103. Foo#2
  104. {#3
  105. protected#4
  106. $bar#5
  107. #6
  108. ,#7
  109. $baz#8
  110. #9
  111. ,#10
  112. $qux#11
  113. #12
  114. ;#13
  115. }
  116. ',
  117. '<?php class#1
  118. Foo#2
  119. {#3
  120. protected#4
  121. $bar#5
  122. =#6
  123. null,#7
  124. $baz#8
  125. =#9
  126. null,#10
  127. $qux#11
  128. =#12
  129. null;#13
  130. }
  131. ',
  132. ],
  133. [
  134. '<?php class Foo { const FOO = null; }',
  135. ],
  136. ];
  137. }
  138. /**
  139. * @param string $expected
  140. * @param null|string $input
  141. *
  142. * @requires PHP 7.0
  143. * @dataProvider providePhp70Cases
  144. */
  145. public function testFixPhp70($expected, $input = null)
  146. {
  147. $this->doTest($expected, $input);
  148. }
  149. public function providePhp70Cases()
  150. {
  151. return [
  152. [
  153. '<?php new class () { public $bar; };',
  154. '<?php new class () { public $bar = null; };',
  155. ],
  156. [
  157. '<?php class Foo { public function foo() { return new class() { private $bar; }; } }',
  158. '<?php class Foo { public function foo() { return new class() { private $bar = null; }; } }',
  159. ],
  160. [
  161. '<?php class Foo { public function foo() { return new class() { private $bar; }; } } trait Baz { public $baz; }',
  162. '<?php class Foo { public function foo() { return new class() { private $bar = null; }; } } trait Baz { public $baz = null; }',
  163. ],
  164. ];
  165. }
  166. /**
  167. * @param string $expected
  168. * @param null|string $input
  169. *
  170. * @requires PHP 7.1
  171. * @dataProvider providePhp71Cases
  172. */
  173. public function testFixPhp71($expected, $input = null)
  174. {
  175. $this->doTest($expected, $input);
  176. }
  177. public function providePhp71Cases()
  178. {
  179. return [
  180. [
  181. '<?php class Foo { public const FOO = null; }',
  182. ],
  183. ];
  184. }
  185. /**
  186. * @param string $expected
  187. * @param null|string $input
  188. *
  189. * @dataProvider provideFix74Cases
  190. * @requires PHP 7.4
  191. */
  192. public function testFix74($expected, $input = null)
  193. {
  194. $this->doTest($expected, $input);
  195. }
  196. public function provideFix74Cases()
  197. {
  198. yield [
  199. '<?php class Foo { protected ?int $bar = null; }',
  200. ];
  201. yield [
  202. '<?php class Foo { protected ? string $bar = null; }',
  203. ];
  204. yield [
  205. '<?php class Foo { protected ? array $bar = null; }',
  206. ];
  207. }
  208. /**
  209. * @param string $expected
  210. * @param null|string $input
  211. *
  212. * @dataProvider provideFixPrePHP80Cases
  213. *
  214. * @requires PHP <8.0
  215. */
  216. public function testFixPrePHP80($expected, $input = null)
  217. {
  218. $this->doTest($expected, $input);
  219. }
  220. public function provideFixPrePHP80Cases()
  221. {
  222. yield [
  223. '<?php class Foo { public $bar; }',
  224. '<?php class Foo { public $bar = \ null; }',
  225. ];
  226. yield [
  227. '<?php class Foo { public $bar/* oh hai! */; }',
  228. '<?php class Foo { public $bar = \/* oh hai! */null; }',
  229. ];
  230. }
  231. /**
  232. * @requires PHP 8.0
  233. */
  234. public function testFixPhp80()
  235. {
  236. $this->doTest('<?php
  237. class Point {
  238. public function __construct(
  239. public ?float $x = null,
  240. protected ?float $y = null,
  241. private ?float $z = null,
  242. ) {}
  243. }
  244. ');
  245. }
  246. }