NewWithBracesFixerTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. return [
  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 $xyz = new X(new Y(new Z()));',
  73. '<?php $xyz = new X(new Y(new Z));',
  74. ],
  75. [
  76. '<?php $foo = (new $bar())->foo;',
  77. '<?php $foo = (new $bar)->foo;',
  78. ],
  79. [
  80. '<?php $foo = (new $bar((new Foo())->bar))->foo;',
  81. '<?php $foo = (new $bar((new Foo)->bar))->foo;',
  82. ],
  83. [
  84. '<?php $self = new self();',
  85. '<?php $self = new self;',
  86. ],
  87. [
  88. '<?php $static = new static();',
  89. '<?php $static = new static;',
  90. ],
  91. [
  92. '<?php $a = array( "key" => new DateTime(), );',
  93. '<?php $a = array( "key" => new DateTime, );',
  94. ],
  95. [
  96. '<?php $a = array( "key" => new DateTime() );',
  97. '<?php $a = array( "key" => new DateTime );',
  98. ],
  99. [
  100. '<?php $a = new $b[$c]();',
  101. '<?php $a = new $b[$c];',
  102. ],
  103. [
  104. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
  105. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
  106. ],
  107. [
  108. '<?php $a = new $b[\'class\']();',
  109. '<?php $a = new $b[\'class\'];',
  110. ],
  111. [
  112. '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
  113. ],
  114. [
  115. '<?php $a = new $b[\'class\'] () ;',
  116. ],
  117. [
  118. '<?php $a = new $b[$c] ($hello[$world]) ;',
  119. ],
  120. [
  121. "<?php \$a = new \$b['class']()\r\n\t ;",
  122. "<?php \$a = new \$b['class']\r\n\t ;",
  123. ],
  124. [
  125. '<?php $a = $b ? new DateTime() : $b;',
  126. '<?php $a = $b ? new DateTime : $b;',
  127. ],
  128. [
  129. '<?php new self::$adapters[$name]["adapter"]();',
  130. '<?php new self::$adapters[$name]["adapter"];',
  131. ],
  132. [
  133. '<?php $a = new \Exception()?> <?php echo 1;',
  134. '<?php $a = new \Exception?> <?php echo 1;',
  135. ],
  136. [
  137. '<?php $b = new \StdClass() /**/?>',
  138. '<?php $b = new \StdClass /**/?>',
  139. ],
  140. [
  141. '<?php $a = new Foo() instanceof Foo;',
  142. '<?php $a = new Foo instanceof Foo;',
  143. ],
  144. [
  145. '<?php
  146. $a = new Foo() + 1;
  147. $a = new Foo() - 1;
  148. $a = new Foo() * 1;
  149. $a = new Foo() / 1;
  150. $a = new Foo() % 1;
  151. ',
  152. '<?php
  153. $a = new Foo + 1;
  154. $a = new Foo - 1;
  155. $a = new Foo * 1;
  156. $a = new Foo / 1;
  157. $a = new Foo % 1;
  158. ',
  159. ],
  160. [
  161. '<?php
  162. $a = new Foo() & 1;
  163. $a = new Foo() | 1;
  164. $a = new Foo() ^ 1;
  165. $a = new Foo() << 1;
  166. $a = new Foo() >> 1;
  167. ',
  168. '<?php
  169. $a = new Foo & 1;
  170. $a = new Foo | 1;
  171. $a = new Foo ^ 1;
  172. $a = new Foo << 1;
  173. $a = new Foo >> 1;
  174. ',
  175. ],
  176. [
  177. '<?php
  178. $a = new Foo() and 1;
  179. $a = new Foo() or 1;
  180. $a = new Foo() xor 1;
  181. $a = new Foo() && 1;
  182. $a = new Foo() || 1;
  183. ',
  184. '<?php
  185. $a = new Foo and 1;
  186. $a = new Foo or 1;
  187. $a = new Foo xor 1;
  188. $a = new Foo && 1;
  189. $a = new Foo || 1;
  190. ',
  191. ],
  192. [
  193. '<?php
  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. if (new DateTime() <> $this->startDate) {}
  201. if (new DateTime() === $this->startDate) {}
  202. if (new DateTime() !== $this->startDate) {}
  203. ',
  204. '<?php
  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. if (new DateTime !== $this->startDate) {}
  214. ',
  215. ],
  216. [
  217. '<?php $a = new \stdClass() ? $b : $c;',
  218. '<?php $a = new \stdClass ? $b : $c;',
  219. ],
  220. [
  221. '<?php foreach (new Collection() as $x) {}',
  222. '<?php foreach (new Collection as $x) {}',
  223. ],
  224. [
  225. '<?php $a = [(string) new Foo() => 1];',
  226. '<?php $a = [(string) new Foo => 1];',
  227. ],
  228. [
  229. '<?php $a = [ "key" => new DateTime(), ];',
  230. '<?php $a = [ "key" => new DateTime, ];',
  231. ],
  232. [
  233. '<?php $a = [ "key" => new DateTime() ];',
  234. '<?php $a = [ "key" => new DateTime ];',
  235. ],
  236. [
  237. '<?php
  238. $a = new Foo() ** 1;
  239. ',
  240. '<?php
  241. $a = new Foo ** 1;
  242. ',
  243. ],
  244. ];
  245. }
  246. public function provideFix70Cases()
  247. {
  248. return [
  249. [
  250. '<?php
  251. $a = new Foo() <=> 1;
  252. ',
  253. '<?php
  254. $a = new Foo <=> 1;
  255. ',
  256. ],
  257. [
  258. '<?php
  259. $a = new class() {use SomeTrait;};
  260. $a = new class() implements Foo{};
  261. $a = new class() /**/ extends Bar1{};
  262. $a = new class() extends Bar2 implements Foo{};
  263. $a = new class() extends Bar3 implements Foo, Foo2{};
  264. $a = new class() {}?>
  265. ',
  266. '<?php
  267. $a = new class {use SomeTrait;};
  268. $a = new class implements Foo{};
  269. $a = new class /**/ extends Bar1{};
  270. $a = new class extends Bar2 implements Foo{};
  271. $a = new class extends Bar3 implements Foo, Foo2{};
  272. $a = new class {}?>
  273. ',
  274. ],
  275. [
  276. '<?php
  277. class A {
  278. public function B() {
  279. $static = new static(new class(){});
  280. }
  281. }
  282. ',
  283. '<?php
  284. class A {
  285. public function B() {
  286. $static = new static(new class{});
  287. }
  288. }
  289. ',
  290. ],
  291. ];
  292. }
  293. }