NewWithBracesFixerTest.php 23 KB

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