NewWithBracesFixerTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\NewWithBracesFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Operator\NewWithBracesFixer
  24. */
  25. final class NewWithBracesFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixNamedWithDefaultConfigurationCases
  29. */
  30. public function testFixNamedWithDefaultConfiguration(string $expected, ?string $input = null): void
  31. {
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<array{0: string, 1?: string}>
  36. */
  37. public static function provideFixNamedWithDefaultConfigurationCases(): iterable
  38. {
  39. yield ['<?php $x = new X(foo(/**/));'];
  40. yield ['<?php $xyz = new X(new Y(new Z(/**/ foo())));'];
  41. yield ['<?php $self = new self(a);'];
  42. yield [
  43. '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
  44. ];
  45. yield [
  46. '<?php $static = new self(new \SplFileInfo(__FILE__));',
  47. ];
  48. yield [
  49. '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
  50. ];
  51. yield [
  52. '<?php $x = new X();',
  53. '<?php $x = new X;',
  54. ];
  55. yield [
  56. '<?php $y = new Y() ;',
  57. '<?php $y = new Y ;',
  58. ];
  59. yield [
  60. '<?php $x = new Z() /**/;//',
  61. '<?php $x = new Z /**/;//',
  62. ];
  63. yield [
  64. '<?php $foo = new $foo();',
  65. '<?php $foo = new $foo;',
  66. ];
  67. yield [
  68. '<?php
  69. $bar1 = new $foo[0]->bar();
  70. $bar2 = new $foo[0][1]->bar();
  71. ',
  72. ];
  73. yield [
  74. '<?php $xyz = new X(new Y(new Z()));',
  75. '<?php $xyz = new X(new Y(new Z));',
  76. ];
  77. yield [
  78. '<?php $foo = (new $bar())->foo;',
  79. '<?php $foo = (new $bar)->foo;',
  80. ];
  81. yield [
  82. '<?php $foo = (new $bar((new Foo())->bar))->foo;',
  83. '<?php $foo = (new $bar((new Foo)->bar))->foo;',
  84. ];
  85. yield [
  86. '<?php $self = new self();',
  87. '<?php $self = new self;',
  88. ];
  89. yield [
  90. '<?php $static = new static();',
  91. '<?php $static = new static;',
  92. ];
  93. yield [
  94. '<?php $a = array( "key" => new DateTime(), );',
  95. '<?php $a = array( "key" => new DateTime, );',
  96. ];
  97. yield [
  98. '<?php $a = array( "key" => new DateTime() );',
  99. '<?php $a = array( "key" => new DateTime );',
  100. ];
  101. yield [
  102. '<?php $a = new $b[$c]();',
  103. '<?php $a = new $b[$c];',
  104. ];
  105. yield [
  106. '<?php $a = new $b[$c][0]();',
  107. '<?php $a = new $b[$c][0];',
  108. ];
  109. yield [
  110. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
  111. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
  112. ];
  113. yield [
  114. '<?php $a = new $b[\'class\']();',
  115. '<?php $a = new $b[\'class\'];',
  116. ];
  117. yield [
  118. '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
  119. ];
  120. yield [
  121. '<?php $a = new $b[\'class\'] () ;',
  122. ];
  123. yield [
  124. '<?php $a = new $b[$c] ($hello[$world]) ;',
  125. ];
  126. yield [
  127. "<?php \$a = new \$b['class']()\r\n\t ;",
  128. "<?php \$a = new \$b['class']\r\n\t ;",
  129. ];
  130. yield [
  131. '<?php $a = $b ? new DateTime() : $b;',
  132. '<?php $a = $b ? new DateTime : $b;',
  133. ];
  134. yield [
  135. '<?php new self::$adapters[$name]["adapter"]();',
  136. '<?php new self::$adapters[$name]["adapter"];',
  137. ];
  138. yield [
  139. '<?php $a = new \Exception()?> <?php echo 1;',
  140. '<?php $a = new \Exception?> <?php echo 1;',
  141. ];
  142. yield [
  143. '<?php $b = new \StdClass() /**/?>',
  144. '<?php $b = new \StdClass /**/?>',
  145. ];
  146. yield [
  147. '<?php $a = new Foo() instanceof Foo;',
  148. '<?php $a = new Foo instanceof Foo;',
  149. ];
  150. yield [
  151. '<?php
  152. $a = new Foo() + 1;
  153. $a = new Foo() - 1;
  154. $a = new Foo() * 1;
  155. $a = new Foo() / 1;
  156. $a = new Foo() % 1;
  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. ];
  166. yield [
  167. '<?php
  168. $a = new Foo() & 1;
  169. $a = new Foo() | 1;
  170. $a = new Foo() ^ 1;
  171. $a = new Foo() << 1;
  172. $a = new Foo() >> 1;
  173. ',
  174. '<?php
  175. $a = new Foo & 1;
  176. $a = new Foo | 1;
  177. $a = new Foo ^ 1;
  178. $a = new Foo << 1;
  179. $a = new Foo >> 1;
  180. ',
  181. ];
  182. yield [
  183. '<?php
  184. $a = new Foo() and 1;
  185. $a = new Foo() or 1;
  186. $a = new Foo() xor 1;
  187. $a = new Foo() && 1;
  188. $a = new Foo() || 1;
  189. ',
  190. '<?php
  191. $a = new Foo and 1;
  192. $a = new Foo or 1;
  193. $a = new Foo xor 1;
  194. $a = new Foo && 1;
  195. $a = new Foo || 1;
  196. ',
  197. ];
  198. yield [
  199. '<?php
  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. if (new DateTime() !== $this->startDate) {}
  209. ',
  210. '<?php
  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. if (new DateTime !== $this->startDate) {}
  220. ',
  221. ];
  222. yield [
  223. '<?php $a = new \stdClass() ? $b : $c;',
  224. '<?php $a = new \stdClass ? $b : $c;',
  225. ];
  226. yield [
  227. '<?php foreach (new Collection() as $x) {}',
  228. '<?php foreach (new Collection as $x) {}',
  229. ];
  230. yield [
  231. '<?php $a = [(string) new Foo() => 1];',
  232. '<?php $a = [(string) new Foo => 1];',
  233. ];
  234. yield [
  235. '<?php $a = [ "key" => new DateTime(), ];',
  236. '<?php $a = [ "key" => new DateTime, ];',
  237. ];
  238. yield [
  239. '<?php $a = [ "key" => new DateTime() ];',
  240. '<?php $a = [ "key" => new DateTime ];',
  241. ];
  242. yield [
  243. '<?php
  244. $a = new Foo() ** 1;
  245. ',
  246. '<?php
  247. $a = new Foo ** 1;
  248. ',
  249. ];
  250. yield [
  251. '<?php
  252. $a = new Foo() <=> 1;
  253. ',
  254. '<?php
  255. $a = new Foo <=> 1;
  256. ',
  257. ];
  258. yield [
  259. "<?php \$a = new \$b['class']/* */()\r\n\t ;",
  260. ];
  261. yield [
  262. "<?php \$a = new \$b['class'] /* */()\r\n\t ;",
  263. ];
  264. yield [
  265. "<?php \$a = new \$b['class']()/* */;",
  266. "<?php \$a = new \$b['class']/* */;",
  267. ];
  268. yield [
  269. "<?php \$a = new \$b['class']() /* */;",
  270. "<?php \$a = new \$b['class'] /* */;",
  271. ];
  272. }
  273. /**
  274. * @dataProvider provideFixNamedWithoutBracesCases
  275. */
  276. public function testFixNamedWithoutBraces(string $expected, ?string $input = null): void
  277. {
  278. $this->fixer->configure(['named_class' => false]);
  279. $this->doTest($expected, $input);
  280. }
  281. /**
  282. * @return iterable<array{0: string, 1?: string}>
  283. */
  284. public static function provideFixNamedWithoutBracesCases(): iterable
  285. {
  286. yield ['<?php $x = new X(foo(/**/));'];
  287. yield ['<?php $xyz = new X(new Y(new Z(/**/ foo())));'];
  288. yield ['<?php $self = new self(a);'];
  289. yield [
  290. '<?php $bar1 = new $foo->bar["baz"];',
  291. '<?php $bar1 = new $foo->bar["baz"]();',
  292. ];
  293. yield [
  294. '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
  295. ];
  296. yield [
  297. '<?php $static = new self(new \SplFileInfo(__FILE__));',
  298. ];
  299. yield [
  300. '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ /**/ /**/ /**//**//**/ /**//**//**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
  301. '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
  302. ];
  303. yield [
  304. '<?php $x = new X;',
  305. '<?php $x = new X();',
  306. ];
  307. yield [
  308. '<?php $y = new Y ;',
  309. '<?php $y = new Y() ;',
  310. ];
  311. yield [
  312. '<?php $x = new Z /**/;//',
  313. '<?php $x = new Z() /**/;//',
  314. ];
  315. yield [
  316. '<?php $foo = new $foo;',
  317. '<?php $foo = new $foo();',
  318. ];
  319. yield [
  320. '<?php $xyz = new X(new Y(new Z));',
  321. '<?php $xyz = new X(new Y(new Z()));',
  322. ];
  323. yield [
  324. '<?php $foo = (new $bar)->foo;',
  325. '<?php $foo = (new $bar())->foo;',
  326. ];
  327. yield [
  328. '<?php $foo = (new $bar((new Foo)->bar))->foo;',
  329. '<?php $foo = (new $bar((new Foo())->bar))->foo;',
  330. ];
  331. yield [
  332. '<?php $self = new self;',
  333. '<?php $self = new self();',
  334. ];
  335. yield [
  336. '<?php $static = new static;',
  337. '<?php $static = new static();',
  338. ];
  339. yield [
  340. '<?php $a = array( "key" => new DateTime, );',
  341. '<?php $a = array( "key" => new DateTime(), );',
  342. ];
  343. yield [
  344. '<?php $a = array( "key" => new DateTime );',
  345. '<?php $a = array( "key" => new DateTime() );',
  346. ];
  347. yield [
  348. '<?php $a = new $b[$c];',
  349. '<?php $a = new $b[$c]();',
  350. ];
  351. yield [
  352. '<?php $a = new $b[$c][0];',
  353. '<?php $a = new $b[$c][0]();',
  354. ];
  355. yield [
  356. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
  357. '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
  358. ];
  359. yield [
  360. '<?php $a = new $b[\'class\'];',
  361. '<?php $a = new $b[\'class\']();',
  362. ];
  363. yield [
  364. '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
  365. ];
  366. yield [
  367. '<?php $a = new $b[\'class\'] ;',
  368. '<?php $a = new $b[\'class\'] () ;',
  369. ];
  370. yield [
  371. '<?php $a = new $b[$c] ($hello[$world]) ;',
  372. ];
  373. yield [
  374. "<?php \$a = new \$b['class']\r\n\t ;",
  375. "<?php \$a = new \$b['class']()\r\n\t ;",
  376. ];
  377. yield [
  378. '<?php $a = $b ? new DateTime : $b;',
  379. '<?php $a = $b ? new DateTime() : $b;',
  380. ];
  381. yield [
  382. '<?php new self::$adapters[$name]["adapter"];',
  383. '<?php new self::$adapters[$name]["adapter"]();',
  384. ];
  385. yield [
  386. '<?php $a = new \Exception?> <?php echo 1;',
  387. '<?php $a = new \Exception()?> <?php echo 1;',
  388. ];
  389. yield [
  390. '<?php $b = new \StdClass /**/?>',
  391. '<?php $b = new \StdClass() /**/?>',
  392. ];
  393. yield [
  394. '<?php $a = new Foo instanceof Foo;',
  395. '<?php $a = new Foo() instanceof Foo;',
  396. ];
  397. yield [
  398. '<?php
  399. $a = new Foo + 1;
  400. $a = new Foo - 1;
  401. $a = new Foo * 1;
  402. $a = new Foo / 1;
  403. $a = new Foo % 1;
  404. ',
  405. '<?php
  406. $a = new Foo() + 1;
  407. $a = new Foo() - 1;
  408. $a = new Foo() * 1;
  409. $a = new Foo() / 1;
  410. $a = new Foo() % 1;
  411. ',
  412. ];
  413. yield [
  414. '<?php
  415. $a = new Foo & 1;
  416. $a = new Foo | 1;
  417. $a = new Foo ^ 1;
  418. $a = new Foo << 1;
  419. $a = new Foo >> 1;
  420. ',
  421. '<?php
  422. $a = new Foo() & 1;
  423. $a = new Foo() | 1;
  424. $a = new Foo() ^ 1;
  425. $a = new Foo() << 1;
  426. $a = new Foo() >> 1;
  427. ',
  428. ];
  429. yield [
  430. '<?php
  431. $a = new Foo and 1;
  432. $a = new Foo or 1;
  433. $a = new Foo xor 1;
  434. $a = new Foo && 1;
  435. $a = new Foo || 1;
  436. ',
  437. '<?php
  438. $a = new Foo() and 1;
  439. $a = new Foo() or 1;
  440. $a = new Foo() xor 1;
  441. $a = new Foo() && 1;
  442. $a = new Foo() || 1;
  443. ',
  444. ];
  445. yield [
  446. '<?php
  447. if (new DateTime > $this->startDate) {}
  448. if (new DateTime >= $this->startDate) {}
  449. if (new DateTime < $this->startDate) {}
  450. if (new DateTime <= $this->startDate) {}
  451. if (new DateTime == $this->startDate) {}
  452. if (new DateTime != $this->startDate) {}
  453. if (new DateTime <> $this->startDate) {}
  454. if (new DateTime === $this->startDate) {}
  455. if (new DateTime !== $this->startDate) {}
  456. ',
  457. '<?php
  458. if (new DateTime() > $this->startDate) {}
  459. if (new DateTime() >= $this->startDate) {}
  460. if (new DateTime() < $this->startDate) {}
  461. if (new DateTime() <= $this->startDate) {}
  462. if (new DateTime() == $this->startDate) {}
  463. if (new DateTime() != $this->startDate) {}
  464. if (new DateTime() <> $this->startDate) {}
  465. if (new DateTime() === $this->startDate) {}
  466. if (new DateTime() !== $this->startDate) {}
  467. ',
  468. ];
  469. yield [
  470. '<?php $a = new \stdClass ? $b : $c;',
  471. '<?php $a = new \stdClass() ? $b : $c;',
  472. ];
  473. yield [
  474. '<?php foreach (new Collection as $x) {}',
  475. '<?php foreach (new Collection() as $x) {}',
  476. ];
  477. yield [
  478. '<?php $a = [(string) new Foo => 1];',
  479. '<?php $a = [(string) new Foo() => 1];',
  480. ];
  481. yield [
  482. '<?php $a = [ "key" => new DateTime, ];',
  483. '<?php $a = [ "key" => new DateTime(), ];',
  484. ];
  485. yield [
  486. '<?php $a = [ "key" => new DateTime ];',
  487. '<?php $a = [ "key" => new DateTime() ];',
  488. ];
  489. yield [
  490. '<?php
  491. $a = new Foo ** 1;
  492. ',
  493. '<?php
  494. $a = new Foo() ** 1;
  495. ',
  496. ];
  497. yield [
  498. '<?php
  499. $a = new Foo <=> 1;
  500. ',
  501. '<?php
  502. $a = new Foo() <=> 1;
  503. ',
  504. ];
  505. yield [
  506. "<?php \$a = new \$b['class']/* */\r\n\t ;",
  507. "<?php \$a = new \$b['class']/* */()\r\n\t ;",
  508. ];
  509. yield [
  510. "<?php \$a = new \$b['class'] /* */\r\n\t ;",
  511. "<?php \$a = new \$b['class'] /* */()\r\n\t ;",
  512. ];
  513. yield [
  514. "<?php \$a = new \$b['class']/* */;",
  515. "<?php \$a = new \$b['class']()/* */;",
  516. ];
  517. yield [
  518. "<?php \$a = new \$b['class'] /* */;",
  519. "<?php \$a = new \$b['class']() /* */;",
  520. ];
  521. }
  522. /**
  523. * @dataProvider provideFixAnonymousWithDefaultConfigurationCases
  524. */
  525. public function testFixAnonymousWithDefaultConfiguration(string $expected, ?string $input = null): void
  526. {
  527. $this->doTest($expected, $input);
  528. }
  529. /**
  530. * @return iterable<array{0: string, 1?: string}>
  531. */
  532. public static function provideFixAnonymousWithDefaultConfigurationCases(): iterable
  533. {
  534. yield ['<?php $a = new class($a) {use SomeTrait;};'];
  535. yield ['<?php $a = new class(foo(/**/)) implements Foo{};'];
  536. yield ['<?php $a = new class($c["d"]) /**/ extends Bar1{};'];
  537. yield ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};'];
  538. yield ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};'];
  539. yield ['<?php $a = new class( $h /**/) {}?>'];
  540. yield [
  541. '<?php
  542. $a = new Foo() <=> 1;
  543. ',
  544. '<?php
  545. $a = new Foo <=> 1;
  546. ',
  547. ];
  548. yield [
  549. '<?php
  550. $a = new class() {use SomeTrait;};
  551. $a = new class() implements Foo{};
  552. $a = new class() /**/ extends Bar1{};
  553. $a = new class() extends Bar2 implements Foo{};
  554. $a = new class() extends Bar3 implements Foo, Foo2{};
  555. $a = new class() {}?>
  556. ',
  557. '<?php
  558. $a = new class {use SomeTrait;};
  559. $a = new class implements Foo{};
  560. $a = new class /**/ extends Bar1{};
  561. $a = new class extends Bar2 implements Foo{};
  562. $a = new class extends Bar3 implements Foo, Foo2{};
  563. $a = new class {}?>
  564. ',
  565. ];
  566. yield [
  567. '<?php
  568. class A {
  569. public function B() {
  570. $static = new static(new class(){});
  571. }
  572. }
  573. ',
  574. '<?php
  575. class A {
  576. public function B() {
  577. $static = new static(new class{});
  578. }
  579. }
  580. ',
  581. ];
  582. }
  583. /**
  584. * @dataProvider provideFixAnonymousWithoutBracesCases
  585. */
  586. public function testFixAnonymousWithoutBraces(string $expected, ?string $input = null): void
  587. {
  588. $this->fixer->configure(['anonymous_class' => false]);
  589. $this->doTest($expected, $input);
  590. }
  591. /**
  592. * @return iterable<array{0: string, 1?: string}>
  593. */
  594. public static function provideFixAnonymousWithoutBracesCases(): iterable
  595. {
  596. yield ['<?php $a = new class($a) {use SomeTrait;};'];
  597. yield ['<?php $a = new class(foo(/**/)) implements Foo{};'];
  598. yield ['<?php $a = new class($c["d"]) /**/ extends Bar1{};'];
  599. yield ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};'];
  600. yield ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};'];
  601. yield ['<?php $a = new class( $h /**/) {}?>'];
  602. yield [
  603. '<?php
  604. $a = new class {use SomeTrait;};
  605. $a = new class implements Foo{};
  606. $a = new class /**/ extends Bar1{};
  607. $a = new class extends Bar2 implements Foo{};
  608. $a = new class extends Bar3 implements Foo, Foo2{};
  609. $a = new class {}?>
  610. ',
  611. '<?php
  612. $a = new class() {use SomeTrait;};
  613. $a = new class() implements Foo{};
  614. $a = new class() /**/ extends Bar1{};
  615. $a = new class() extends Bar2 implements Foo{};
  616. $a = new class() extends Bar3 implements Foo, Foo2{};
  617. $a = new class ( ) {}?>
  618. ',
  619. ];
  620. yield [
  621. '<?php
  622. class A {
  623. public function B() {
  624. $static = new static(new class{});
  625. }
  626. }
  627. ',
  628. '<?php
  629. class A {
  630. public function B() {
  631. $static = new static(new class(){});
  632. }
  633. }
  634. ',
  635. ];
  636. }
  637. /**
  638. * @dataProvider provideFixPre80Cases
  639. *
  640. * @requires PHP <8.0
  641. */
  642. public function testFixPre80(string $expected, ?string $input = null): void
  643. {
  644. $this->doTest($expected, $input);
  645. }
  646. /**
  647. * @return iterable<array{string, string}>
  648. */
  649. public static function provideFixPre80Cases(): iterable
  650. {
  651. yield [
  652. '<?php $a = new $b{$c}();',
  653. '<?php $a = new $b{$c};',
  654. ];
  655. yield [
  656. '<?php $a = new $b{$c}{0}{1}() ?>',
  657. '<?php $a = new $b{$c}{0}{1} ?>',
  658. ];
  659. yield [
  660. '<?php $a = new $b{$c}[1]{0}[2]();',
  661. '<?php $a = new $b{$c}[1]{0}[2];',
  662. ];
  663. }
  664. /**
  665. * @dataProvider provideFix80Cases
  666. *
  667. * @requires PHP 8.0
  668. */
  669. public function testFix80(string $expected, ?string $input = null): void
  670. {
  671. $this->doTest($expected, $input);
  672. }
  673. /**
  674. * @return iterable<array{0: string, 1?: string}>
  675. */
  676. public static function provideFix80Cases(): iterable
  677. {
  678. yield [
  679. '<?php $a = new (foo());',
  680. ];
  681. yield [
  682. '<?php
  683. class Bar {
  684. public function __construct(int $a = null) {
  685. echo $a;
  686. }
  687. };
  688. $foo = "B";
  689. $a = new ($foo."ar");',
  690. ];
  691. yield [
  692. '<?php
  693. $bar1 = new $foo[0]?->bar();
  694. $bar2 = new $foo[0][1]?->bar();
  695. ',
  696. ];
  697. yield [
  698. '<?php $a = new
  699. #[Internal]
  700. class(){};
  701. ',
  702. '<?php $a = new
  703. #[Internal]
  704. class{};
  705. ',
  706. ];
  707. }
  708. /**
  709. * @dataProvider provideFix81Cases
  710. *
  711. * @requires PHP 8.1
  712. */
  713. public function testFix81(string $expected, ?string $input = null): void
  714. {
  715. $this->doTest($expected, $input);
  716. }
  717. /**
  718. * @return iterable<array{string, string}>
  719. */
  720. public static function provideFix81Cases(): iterable
  721. {
  722. yield [
  723. '<?php
  724. function test(
  725. $foo = new A(),
  726. $baz = new C(x: 2),
  727. ) {
  728. }
  729. class Test {
  730. public function __construct(
  731. public $prop = new Foo(),
  732. ) {}
  733. }
  734. static $x = new Foo();
  735. const C = new Foo();
  736. function test2($param = new Foo()) {}
  737. ',
  738. '<?php
  739. function test(
  740. $foo = new A,
  741. $baz = new C(x: 2),
  742. ) {
  743. }
  744. class Test {
  745. public function __construct(
  746. public $prop = new Foo,
  747. ) {}
  748. }
  749. static $x = new Foo;
  750. const C = new Foo;
  751. function test2($param = new Foo) {}
  752. ',
  753. ];
  754. }
  755. }