NewWithBracesFixerTest.php 11 KB

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