NewWithBracesFixerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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\Operator;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Operator\NewWithBracesFixer
  20. */
  21. final class NewWithBracesFixerTest 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 function provideFixCases(): \Generator
  31. {
  32. yield from [
  33. [
  34. '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
  35. ],
  36. [
  37. '<?php $static = new self(new \SplFileInfo(__FILE__));',
  38. ],
  39. [
  40. '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
  41. ],
  42. [
  43. '<?php $x = new X();',
  44. '<?php $x = new X;',
  45. ],
  46. [
  47. '<?php $y = new Y() ;',
  48. '<?php $y = new Y ;',
  49. ],
  50. [
  51. '<?php $x = new Z() /**/;//',
  52. '<?php $x = new Z /**/;//',
  53. ],
  54. [
  55. '<?php $foo = new $foo();',
  56. '<?php $foo = new $foo;',
  57. ],
  58. [
  59. '<?php
  60. $bar1 = new $foo[0]->bar();
  61. $bar2 = new $foo[0][1]->bar();
  62. ',
  63. ],
  64. [
  65. '<?php $xyz = new X(new Y(new Z()));',
  66. '<?php $xyz = new X(new Y(new Z));',
  67. ],
  68. [
  69. '<?php $foo = (new $bar())->foo;',
  70. '<?php $foo = (new $bar)->foo;',
  71. ],
  72. [
  73. '<?php $foo = (new $bar((new Foo())->bar))->foo;',
  74. '<?php $foo = (new $bar((new Foo)->bar))->foo;',
  75. ],
  76. [
  77. '<?php $self = new self();',
  78. '<?php $self = new self;',
  79. ],
  80. [
  81. '<?php $static = new static();',
  82. '<?php $static = new static;',
  83. ],
  84. [
  85. '<?php $a = array( "key" => new DateTime(), );',
  86. '<?php $a = array( "key" => new DateTime, );',
  87. ],
  88. [
  89. '<?php $a = array( "key" => new DateTime() );',
  90. '<?php $a = array( "key" => new DateTime );',
  91. ],
  92. [
  93. '<?php $a = new $b[$c]();',
  94. '<?php $a = new $b[$c];',
  95. ],
  96. [
  97. '<?php $a = new $b[$c][0]();',
  98. '<?php $a = new $b[$c][0];',
  99. ],
  100. [
  101. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
  102. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
  103. ],
  104. [
  105. '<?php $a = new $b[\'class\']();',
  106. '<?php $a = new $b[\'class\'];',
  107. ],
  108. [
  109. '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
  110. ],
  111. [
  112. '<?php $a = new $b[\'class\'] () ;',
  113. ],
  114. [
  115. '<?php $a = new $b[$c] ($hello[$world]) ;',
  116. ],
  117. [
  118. "<?php \$a = new \$b['class']()\r\n\t ;",
  119. "<?php \$a = new \$b['class']\r\n\t ;",
  120. ],
  121. [
  122. '<?php $a = $b ? new DateTime() : $b;',
  123. '<?php $a = $b ? new DateTime : $b;',
  124. ],
  125. [
  126. '<?php new self::$adapters[$name]["adapter"]();',
  127. '<?php new self::$adapters[$name]["adapter"];',
  128. ],
  129. [
  130. '<?php $a = new \Exception()?> <?php echo 1;',
  131. '<?php $a = new \Exception?> <?php echo 1;',
  132. ],
  133. [
  134. '<?php $b = new \StdClass() /**/?>',
  135. '<?php $b = new \StdClass /**/?>',
  136. ],
  137. [
  138. '<?php $a = new Foo() instanceof Foo;',
  139. '<?php $a = new Foo instanceof Foo;',
  140. ],
  141. [
  142. '<?php
  143. $a = new Foo() + 1;
  144. $a = new Foo() - 1;
  145. $a = new Foo() * 1;
  146. $a = new Foo() / 1;
  147. $a = new Foo() % 1;
  148. ',
  149. '<?php
  150. $a = new Foo + 1;
  151. $a = new Foo - 1;
  152. $a = new Foo * 1;
  153. $a = new Foo / 1;
  154. $a = new Foo % 1;
  155. ',
  156. ],
  157. [
  158. '<?php
  159. $a = new Foo() & 1;
  160. $a = new Foo() | 1;
  161. $a = new Foo() ^ 1;
  162. $a = new Foo() << 1;
  163. $a = new Foo() >> 1;
  164. ',
  165. '<?php
  166. $a = new Foo & 1;
  167. $a = new Foo | 1;
  168. $a = new Foo ^ 1;
  169. $a = new Foo << 1;
  170. $a = new Foo >> 1;
  171. ',
  172. ],
  173. [
  174. '<?php
  175. $a = new Foo() and 1;
  176. $a = new Foo() or 1;
  177. $a = new Foo() xor 1;
  178. $a = new Foo() && 1;
  179. $a = new Foo() || 1;
  180. ',
  181. '<?php
  182. $a = new Foo and 1;
  183. $a = new Foo or 1;
  184. $a = new Foo xor 1;
  185. $a = new Foo && 1;
  186. $a = new Foo || 1;
  187. ',
  188. ],
  189. [
  190. '<?php
  191. if (new DateTime() > $this->startDate) {}
  192. if (new DateTime() >= $this->startDate) {}
  193. if (new DateTime() < $this->startDate) {}
  194. if (new DateTime() <= $this->startDate) {}
  195. if (new DateTime() == $this->startDate) {}
  196. if (new DateTime() != $this->startDate) {}
  197. if (new DateTime() <> $this->startDate) {}
  198. if (new DateTime() === $this->startDate) {}
  199. if (new DateTime() !== $this->startDate) {}
  200. ',
  201. '<?php
  202. if (new DateTime > $this->startDate) {}
  203. if (new DateTime >= $this->startDate) {}
  204. if (new DateTime < $this->startDate) {}
  205. if (new DateTime <= $this->startDate) {}
  206. if (new DateTime == $this->startDate) {}
  207. if (new DateTime != $this->startDate) {}
  208. if (new DateTime <> $this->startDate) {}
  209. if (new DateTime === $this->startDate) {}
  210. if (new DateTime !== $this->startDate) {}
  211. ',
  212. ],
  213. [
  214. '<?php $a = new \stdClass() ? $b : $c;',
  215. '<?php $a = new \stdClass ? $b : $c;',
  216. ],
  217. [
  218. '<?php foreach (new Collection() as $x) {}',
  219. '<?php foreach (new Collection as $x) {}',
  220. ],
  221. [
  222. '<?php $a = [(string) new Foo() => 1];',
  223. '<?php $a = [(string) new Foo => 1];',
  224. ],
  225. [
  226. '<?php $a = [ "key" => new DateTime(), ];',
  227. '<?php $a = [ "key" => new DateTime, ];',
  228. ],
  229. [
  230. '<?php $a = [ "key" => new DateTime() ];',
  231. '<?php $a = [ "key" => new DateTime ];',
  232. ],
  233. [
  234. '<?php
  235. $a = new Foo() ** 1;
  236. ',
  237. '<?php
  238. $a = new Foo ** 1;
  239. ',
  240. ],
  241. ];
  242. yield from [
  243. [
  244. '<?php
  245. $a = new Foo() <=> 1;
  246. ',
  247. '<?php
  248. $a = new Foo <=> 1;
  249. ',
  250. ],
  251. [
  252. '<?php
  253. $a = new class() {use SomeTrait;};
  254. $a = new class() implements Foo{};
  255. $a = new class() /**/ extends Bar1{};
  256. $a = new class() extends Bar2 implements Foo{};
  257. $a = new class() extends Bar3 implements Foo, Foo2{};
  258. $a = new class() {}?>
  259. ',
  260. '<?php
  261. $a = new class {use SomeTrait;};
  262. $a = new class implements Foo{};
  263. $a = new class /**/ extends Bar1{};
  264. $a = new class extends Bar2 implements Foo{};
  265. $a = new class extends Bar3 implements Foo, Foo2{};
  266. $a = new class {}?>
  267. ',
  268. ],
  269. [
  270. '<?php
  271. class A {
  272. public function B() {
  273. $static = new static(new class(){});
  274. }
  275. }
  276. ',
  277. '<?php
  278. class A {
  279. public function B() {
  280. $static = new static(new class{});
  281. }
  282. }
  283. ',
  284. ],
  285. ];
  286. }
  287. /**
  288. * @dataProvider provideFixPre80Cases
  289. * @requires PHP <8.0
  290. */
  291. public function testFixPre80(string $expected, string $input = null): void
  292. {
  293. $this->doTest($expected, $input);
  294. }
  295. public function provideFixPre80Cases(): \Generator
  296. {
  297. yield [
  298. '<?php $a = new $b{$c}();',
  299. '<?php $a = new $b{$c};',
  300. ];
  301. yield [
  302. '<?php $a = new $b{$c}{0}{1}() ?>',
  303. '<?php $a = new $b{$c}{0}{1} ?>',
  304. ];
  305. yield [
  306. '<?php $a = new $b{$c}[1]{0}[2]();',
  307. '<?php $a = new $b{$c}[1]{0}[2];',
  308. ];
  309. }
  310. /**
  311. * @dataProvider provideFix80Cases
  312. * @requires PHP 8.0
  313. */
  314. public function testFix80(string $expected, ?string $input = null): void
  315. {
  316. $this->doTest($expected, $input);
  317. }
  318. public function provideFix80Cases(): \Generator
  319. {
  320. yield [
  321. '<?php $a = new (foo());',
  322. ];
  323. yield [
  324. '<?php
  325. class Bar {
  326. public function __construct(int $a = null) {
  327. echo $a;
  328. }
  329. };
  330. $foo = "B";
  331. $a = new ($foo."ar");',
  332. ];
  333. yield [
  334. '<?php
  335. $bar1 = new $foo[0]?->bar();
  336. $bar2 = new $foo[0][1]?->bar();
  337. ',
  338. ];
  339. yield [
  340. '<?php $a = new
  341. #[Internal]
  342. class(){};
  343. ',
  344. '<?php $a = new
  345. #[Internal]
  346. class{};
  347. ',
  348. ];
  349. }
  350. /**
  351. * @dataProvider provideFix81Cases
  352. * @requires PHP 8.1
  353. */
  354. public function testFix81(string $expected, ?string $input = null): void
  355. {
  356. $this->doTest($expected, $input);
  357. }
  358. public function provideFix81Cases(): \Generator
  359. {
  360. yield [
  361. '<?php
  362. function test(
  363. $foo = new A(),
  364. $baz = new C(x: 2),
  365. ) {
  366. }
  367. class Test {
  368. public function __construct(
  369. public $prop = new Foo(),
  370. ) {}
  371. }
  372. static $x = new Foo();
  373. const C = new Foo();
  374. function test2($param = new Foo()) {}
  375. ',
  376. '<?php
  377. function test(
  378. $foo = new A,
  379. $baz = new C(x: 2),
  380. ) {
  381. }
  382. class Test {
  383. public function __construct(
  384. public $prop = new Foo,
  385. ) {}
  386. }
  387. static $x = new Foo;
  388. const C = new Foo;
  389. function test2($param = new Foo) {}
  390. ',
  391. ];
  392. }
  393. }