NewWithBracesFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /**
  31. * @dataProvider provideFix70Cases
  32. * @requires PHP 7.0
  33. */
  34. public function testFix70(string $expected, ?string $input = null): void
  35. {
  36. $this->doTest($expected, $input);
  37. }
  38. public function provideFixCases()
  39. {
  40. $tests = [
  41. [
  42. '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
  43. ],
  44. [
  45. '<?php $static = new self(new \SplFileInfo(__FILE__));',
  46. ],
  47. [
  48. '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
  49. ],
  50. [
  51. '<?php $x = new X();',
  52. '<?php $x = new X;',
  53. ],
  54. [
  55. '<?php $y = new Y() ;',
  56. '<?php $y = new Y ;',
  57. ],
  58. [
  59. '<?php $x = new Z() /**/;//',
  60. '<?php $x = new Z /**/;//',
  61. ],
  62. [
  63. '<?php $foo = new $foo();',
  64. '<?php $foo = new $foo;',
  65. ],
  66. [
  67. '<?php
  68. $bar1 = new $foo[0]->bar();
  69. $bar2 = new $foo[0][1]->bar();
  70. ',
  71. ],
  72. [
  73. '<?php $xyz = new X(new Y(new Z()));',
  74. '<?php $xyz = new X(new Y(new Z));',
  75. ],
  76. [
  77. '<?php $foo = (new $bar())->foo;',
  78. '<?php $foo = (new $bar)->foo;',
  79. ],
  80. [
  81. '<?php $foo = (new $bar((new Foo())->bar))->foo;',
  82. '<?php $foo = (new $bar((new Foo)->bar))->foo;',
  83. ],
  84. [
  85. '<?php $self = new self();',
  86. '<?php $self = new self;',
  87. ],
  88. [
  89. '<?php $static = new static();',
  90. '<?php $static = new static;',
  91. ],
  92. [
  93. '<?php $a = array( "key" => new DateTime(), );',
  94. '<?php $a = array( "key" => new DateTime, );',
  95. ],
  96. [
  97. '<?php $a = array( "key" => new DateTime() );',
  98. '<?php $a = array( "key" => new DateTime );',
  99. ],
  100. [
  101. '<?php $a = new $b[$c]();',
  102. '<?php $a = new $b[$c];',
  103. ],
  104. [
  105. '<?php $a = new $b[$c][0]();',
  106. '<?php $a = new $b[$c][0];',
  107. ],
  108. [
  109. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
  110. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
  111. ],
  112. [
  113. '<?php $a = new $b[\'class\']();',
  114. '<?php $a = new $b[\'class\'];',
  115. ],
  116. [
  117. '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
  118. ],
  119. [
  120. '<?php $a = new $b[\'class\'] () ;',
  121. ],
  122. [
  123. '<?php $a = new $b[$c] ($hello[$world]) ;',
  124. ],
  125. [
  126. "<?php \$a = new \$b['class']()\r\n\t ;",
  127. "<?php \$a = new \$b['class']\r\n\t ;",
  128. ],
  129. [
  130. '<?php $a = $b ? new DateTime() : $b;',
  131. '<?php $a = $b ? new DateTime : $b;',
  132. ],
  133. [
  134. '<?php new self::$adapters[$name]["adapter"]();',
  135. '<?php new self::$adapters[$name]["adapter"];',
  136. ],
  137. [
  138. '<?php $a = new \Exception()?> <?php echo 1;',
  139. '<?php $a = new \Exception?> <?php echo 1;',
  140. ],
  141. [
  142. '<?php $b = new \StdClass() /**/?>',
  143. '<?php $b = new \StdClass /**/?>',
  144. ],
  145. [
  146. '<?php $a = new Foo() instanceof Foo;',
  147. '<?php $a = new Foo instanceof Foo;',
  148. ],
  149. [
  150. '<?php
  151. $a = new Foo() + 1;
  152. $a = new Foo() - 1;
  153. $a = new Foo() * 1;
  154. $a = new Foo() / 1;
  155. $a = new Foo() % 1;
  156. ',
  157. '<?php
  158. $a = new Foo + 1;
  159. $a = new Foo - 1;
  160. $a = new Foo * 1;
  161. $a = new Foo / 1;
  162. $a = new Foo % 1;
  163. ',
  164. ],
  165. [
  166. '<?php
  167. $a = new Foo() & 1;
  168. $a = new Foo() | 1;
  169. $a = new Foo() ^ 1;
  170. $a = new Foo() << 1;
  171. $a = new Foo() >> 1;
  172. ',
  173. '<?php
  174. $a = new Foo & 1;
  175. $a = new Foo | 1;
  176. $a = new Foo ^ 1;
  177. $a = new Foo << 1;
  178. $a = new Foo >> 1;
  179. ',
  180. ],
  181. [
  182. '<?php
  183. $a = new Foo() and 1;
  184. $a = new Foo() or 1;
  185. $a = new Foo() xor 1;
  186. $a = new Foo() && 1;
  187. $a = new Foo() || 1;
  188. ',
  189. '<?php
  190. $a = new Foo and 1;
  191. $a = new Foo or 1;
  192. $a = new Foo xor 1;
  193. $a = new Foo && 1;
  194. $a = new Foo || 1;
  195. ',
  196. ],
  197. [
  198. '<?php
  199. if (new DateTime() > $this->startDate) {}
  200. if (new DateTime() >= $this->startDate) {}
  201. if (new DateTime() < $this->startDate) {}
  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. ',
  209. '<?php
  210. if (new DateTime > $this->startDate) {}
  211. if (new DateTime >= $this->startDate) {}
  212. if (new DateTime < $this->startDate) {}
  213. if (new DateTime <= $this->startDate) {}
  214. if (new DateTime == $this->startDate) {}
  215. if (new DateTime != $this->startDate) {}
  216. if (new DateTime <> $this->startDate) {}
  217. if (new DateTime === $this->startDate) {}
  218. if (new DateTime !== $this->startDate) {}
  219. ',
  220. ],
  221. [
  222. '<?php $a = new \stdClass() ? $b : $c;',
  223. '<?php $a = new \stdClass ? $b : $c;',
  224. ],
  225. [
  226. '<?php foreach (new Collection() as $x) {}',
  227. '<?php foreach (new Collection as $x) {}',
  228. ],
  229. [
  230. '<?php $a = [(string) new Foo() => 1];',
  231. '<?php $a = [(string) new Foo => 1];',
  232. ],
  233. [
  234. '<?php $a = [ "key" => new DateTime(), ];',
  235. '<?php $a = [ "key" => new DateTime, ];',
  236. ],
  237. [
  238. '<?php $a = [ "key" => new DateTime() ];',
  239. '<?php $a = [ "key" => new DateTime ];',
  240. ],
  241. [
  242. '<?php
  243. $a = new Foo() ** 1;
  244. ',
  245. '<?php
  246. $a = new Foo ** 1;
  247. ',
  248. ],
  249. ];
  250. foreach ($tests as $index => $test) {
  251. yield $index => $test;
  252. }
  253. if (\PHP_VERSION_ID < 80000) {
  254. yield [
  255. '<?php $a = new $b{$c}();',
  256. '<?php $a = new $b{$c};',
  257. ];
  258. yield [
  259. '<?php $a = new $b{$c}{0}{1}() ?>',
  260. '<?php $a = new $b{$c}{0}{1} ?>',
  261. ];
  262. yield [
  263. '<?php $a = new $b{$c}[1]{0}[2]();',
  264. '<?php $a = new $b{$c}[1]{0}[2];',
  265. ];
  266. }
  267. }
  268. public function provideFix70Cases()
  269. {
  270. return [
  271. [
  272. '<?php
  273. $a = new Foo() <=> 1;
  274. ',
  275. '<?php
  276. $a = new Foo <=> 1;
  277. ',
  278. ],
  279. [
  280. '<?php
  281. $a = new class() {use SomeTrait;};
  282. $a = new class() implements Foo{};
  283. $a = new class() /**/ extends Bar1{};
  284. $a = new class() extends Bar2 implements Foo{};
  285. $a = new class() extends Bar3 implements Foo, Foo2{};
  286. $a = new class() {}?>
  287. ',
  288. '<?php
  289. $a = new class {use SomeTrait;};
  290. $a = new class implements Foo{};
  291. $a = new class /**/ extends Bar1{};
  292. $a = new class extends Bar2 implements Foo{};
  293. $a = new class extends Bar3 implements Foo, Foo2{};
  294. $a = new class {}?>
  295. ',
  296. ],
  297. [
  298. '<?php
  299. class A {
  300. public function B() {
  301. $static = new static(new class(){});
  302. }
  303. }
  304. ',
  305. '<?php
  306. class A {
  307. public function B() {
  308. $static = new static(new class{});
  309. }
  310. }
  311. ',
  312. ],
  313. ];
  314. }
  315. /**
  316. * @dataProvider provideFix80Cases
  317. * @requires PHP 8.0
  318. */
  319. public function testFix80(string $expected, ?string $input = null): void
  320. {
  321. $this->doTest($expected, $input);
  322. }
  323. public function provideFix80Cases()
  324. {
  325. yield [
  326. '<?php $a = new (foo());',
  327. ];
  328. yield [
  329. '<?php
  330. class Bar {
  331. public function __construct(int $a = null) {
  332. echo $a;
  333. }
  334. };
  335. $foo = "B";
  336. $a = new ($foo."ar");',
  337. ];
  338. yield [
  339. '<?php
  340. $bar1 = new $foo[0]?->bar();
  341. $bar2 = new $foo[0][1]?->bar();
  342. ',
  343. ];
  344. }
  345. }