StandardizeIncrementFixerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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 ntzm
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\AbstractIncrementOperatorFixer
  20. * @covers \PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Operator\StandardizeIncrementFixer>
  23. */
  24. final class StandardizeIncrementFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield [
  39. '<?php ++$i;',
  40. '<?php $i += 1;',
  41. ];
  42. yield [
  43. '<?php ++$i;',
  44. '<?php $i+=1;',
  45. ];
  46. yield [
  47. '<?php for ($i = 0; $i < $n; ++$i) {};',
  48. '<?php for ($i = 0; $i < $n; $i += 1) {};',
  49. ];
  50. yield [
  51. '<?php ++$foo->bar;',
  52. '<?php $foo->bar += 1;',
  53. ];
  54. yield [
  55. '<?php ++$foo->$bar;',
  56. '<?php $foo->$bar += 1;',
  57. ];
  58. yield [
  59. '<?php ++$foo->$$$bar;',
  60. '<?php $foo->$$$bar += 1;',
  61. ];
  62. yield [
  63. '<?php ++$foo["bar"];',
  64. '<?php $foo["bar"] += 1;',
  65. ];
  66. yield [
  67. '<?php ++$foo[baz()];',
  68. '<?php $foo[baz()] += 1;',
  69. ];
  70. yield [
  71. '<?php ++$foo[$bar->baz];',
  72. '<?php $foo[$bar->baz] += 1;',
  73. ];
  74. yield [
  75. '<?php ++$foo[$bar];',
  76. '<?php $foo[$bar] += 1;',
  77. ];
  78. yield [
  79. '<?php ++$foo[Bar::BAZ];',
  80. '<?php $foo[Bar::BAZ] += 1;',
  81. ];
  82. yield [
  83. '<?php echo $foo[++$i];',
  84. '<?php echo $foo[$i += 1];',
  85. ];
  86. yield [
  87. '<?php echo ++$foo[$bar[$baz]];',
  88. '<?php echo $foo[$bar[$baz]] += 1;',
  89. ];
  90. yield [
  91. '<?php ++$$foo;',
  92. '<?php $$foo += 1;',
  93. ];
  94. yield [
  95. '<?php ++$$$$foo;',
  96. '<?php $$$$foo += 1;',
  97. ];
  98. yield [
  99. '<?php ++${$foo};',
  100. '<?php ${$foo} += 1;',
  101. ];
  102. yield [
  103. '<?php ++$$${$foo};',
  104. '<?php $$${$foo} += 1;',
  105. ];
  106. yield [
  107. '<?php ++$a[$b];',
  108. '<?php $a[$b] += 1;',
  109. ];
  110. yield [
  111. '<?php ++$a[++$b];',
  112. '<?php $a[$b += 1] += 1;',
  113. ];
  114. yield [
  115. '<?php foo(++$a);',
  116. '<?php foo($a += 1);',
  117. ];
  118. yield [
  119. '<?php foo(++$a, $bar);',
  120. '<?php foo($a += 1, $bar);',
  121. ];
  122. yield [
  123. '<?php ++$foo->{++$a};',
  124. '<?php $foo->{$a += 1} += 1;',
  125. ];
  126. yield [
  127. '<?php ++${++$a};',
  128. '<?php ${$a += 1} += 1;',
  129. ];
  130. yield [
  131. '<?php ++$i ?>',
  132. '<?php $i += 1 ?>',
  133. ];
  134. yield [
  135. '<?php $a = $b ? ++$c : ++$d;',
  136. '<?php $a = $b ? $c += 1 : $d += 1;',
  137. ];
  138. yield [
  139. '<?php ++$a->{++$b}[++$c];',
  140. '<?php $a->{$b += 1}[$c += 1] += 1;',
  141. ];
  142. yield [
  143. '<?php (++$i);',
  144. '<?php ($i += 1);',
  145. ];
  146. yield [
  147. '<?php (((++$i)));',
  148. '<?php ((($i += 1)));',
  149. ];
  150. yield [
  151. '<?php ++$a->b->$c;',
  152. '<?php $a->b->$c += 1;',
  153. ];
  154. yield [
  155. '<?php ++$i/* foo */;',
  156. '<?php $i +=/* foo */1;',
  157. ];
  158. yield [
  159. '<?php ++$i/* foo *//* bar */;',
  160. '<?php $i /* foo */ += /* bar */1;',
  161. ];
  162. yield [
  163. '<?php ++$i/** foo *//** bar */;',
  164. '<?php $i /** foo */ += /** bar */1;',
  165. ];
  166. yield [
  167. "<?php ++\$i// foo\n;",
  168. "<?php \$i += // foo\n1;",
  169. ];
  170. yield [
  171. '<?php --$i;',
  172. '<?php $i -= 1;',
  173. ];
  174. yield [
  175. '<?php --$i;',
  176. '<?php $i-=1;',
  177. ];
  178. yield [
  179. '<?php for ($i = 0; $i < $n; --$i) {};',
  180. '<?php for ($i = 0; $i < $n; $i -= 1) {};',
  181. ];
  182. yield [
  183. '<?php --$foo->bar;',
  184. '<?php $foo->bar -= 1;',
  185. ];
  186. yield [
  187. '<?php --$foo->$bar;',
  188. '<?php $foo->$bar -= 1;',
  189. ];
  190. yield [
  191. '<?php --$foo->$$$bar;',
  192. '<?php $foo->$$$bar -= 1;',
  193. ];
  194. yield [
  195. '<?php --$foo["bar"];',
  196. '<?php $foo["bar"] -= 1;',
  197. ];
  198. yield [
  199. '<?php --$foo[baz()];',
  200. '<?php $foo[baz()] -= 1;',
  201. ];
  202. yield [
  203. '<?php --$foo[$bar->baz];',
  204. '<?php $foo[$bar->baz] -= 1;',
  205. ];
  206. yield [
  207. '<?php --$foo[$bar];',
  208. '<?php $foo[$bar] -= 1;',
  209. ];
  210. yield [
  211. '<?php --$foo[Bar::BAZ];',
  212. '<?php $foo[Bar::BAZ] -= 1;',
  213. ];
  214. yield [
  215. '<?php echo $foo[--$i];',
  216. '<?php echo $foo[$i -= 1];',
  217. ];
  218. yield [
  219. '<?php echo --$foo->{$bar};',
  220. '<?php echo $foo->{$bar} -= 1;',
  221. ];
  222. yield [
  223. '<?php echo --$foo->{$bar->{$baz}};',
  224. '<?php echo $foo->{$bar->{$baz}} -= 1;',
  225. ];
  226. yield [
  227. '<?php echo --$foo[$bar[$baz]];',
  228. '<?php echo $foo[$bar[$baz]] -= 1;',
  229. ];
  230. yield [
  231. '<?php --$$foo;',
  232. '<?php $$foo -= 1;',
  233. ];
  234. yield [
  235. '<?php --$$$$foo;',
  236. '<?php $$$$foo -= 1;',
  237. ];
  238. yield [
  239. '<?php --${$foo};',
  240. '<?php ${$foo} -= 1;',
  241. ];
  242. yield [
  243. '<?php --$$${$foo};',
  244. '<?php $$${$foo} -= 1;',
  245. ];
  246. yield [
  247. '<?php --$a[$b];',
  248. '<?php $a[$b] -= 1;',
  249. ];
  250. yield [
  251. '<?php --$a[--$b];',
  252. '<?php $a[$b -= 1] -= 1;',
  253. ];
  254. yield [
  255. '<?php foo(--$a);',
  256. '<?php foo($a -= 1);',
  257. ];
  258. yield [
  259. '<?php foo(--$a, $bar);',
  260. '<?php foo($a -= 1, $bar);',
  261. ];
  262. yield [
  263. '<?php --$foo->{--$a};',
  264. '<?php $foo->{$a -= 1} -= 1;',
  265. ];
  266. yield [
  267. '<?php --${--$a};',
  268. '<?php ${$a -= 1} -= 1;',
  269. ];
  270. yield [
  271. '<?php --$i ?>',
  272. '<?php $i -= 1 ?>',
  273. ];
  274. yield [
  275. '<?php $a = $b ? --$c : --$d;',
  276. '<?php $a = $b ? $c -= 1 : $d -= 1;',
  277. ];
  278. yield [
  279. '<?php --$a->{--$b}[--$c];',
  280. '<?php $a->{$b -= 1}[$c -= 1] -= 1;',
  281. ];
  282. yield [
  283. '<?php (--$i);',
  284. '<?php ($i -= 1);',
  285. ];
  286. yield [
  287. '<?php (((--$i)));',
  288. '<?php ((($i -= 1)));',
  289. ];
  290. yield [
  291. '<?php --$a->b->$c;',
  292. '<?php $a->b->$c -= 1;',
  293. ];
  294. yield [
  295. '<?php --$i/* foo */;',
  296. '<?php $i -=/* foo */1;',
  297. ];
  298. yield [
  299. '<?php --$i/* foo *//* bar */;',
  300. '<?php $i /* foo */ -= /* bar */1;',
  301. ];
  302. yield [
  303. '<?php --$i/** foo *//** bar */;',
  304. '<?php $i /** foo */ -= /** bar */1;',
  305. ];
  306. yield [
  307. "<?php --\$i// foo\n;",
  308. "<?php \$i -= // foo\n1;",
  309. ];
  310. yield [
  311. '<?php $i + 1;',
  312. ];
  313. yield [
  314. '<?php $i - 1;',
  315. ];
  316. yield [
  317. '<?php $i = 1;',
  318. ];
  319. yield [
  320. '<?php $i = -1;',
  321. ];
  322. yield [
  323. '<?php $i += 1.0;',
  324. ];
  325. yield [
  326. '<?php $i += "1";',
  327. ];
  328. yield [
  329. '<?php $i -= 1.0;',
  330. ];
  331. yield [
  332. '<?php $i -= "1";',
  333. ];
  334. yield [
  335. '<?php $i += 1 * 2;',
  336. ];
  337. yield [
  338. '<?php $i += 1 ** 2;',
  339. ];
  340. yield [
  341. '<?php $i += 1 / 2;',
  342. ];
  343. yield [
  344. '<?php $i += 1 + 2;',
  345. ];
  346. yield [
  347. '<?php $i += 1 - 2;',
  348. ];
  349. yield [
  350. '<?php $i += 1 % 2;',
  351. ];
  352. yield [
  353. '<?php $i += 1 ?: 2;',
  354. ];
  355. yield [
  356. '<?php $i += 1 & 2;',
  357. ];
  358. yield [
  359. '<?php $i += 1 ^ 2;',
  360. ];
  361. yield [
  362. '<?php $i += 1 >> 2;',
  363. ];
  364. yield [
  365. '<?php $i += 1 << 2;',
  366. ];
  367. yield [
  368. '<?php $i += 1 && true;',
  369. ];
  370. yield [
  371. '<?php $i += 1 || true;',
  372. ];
  373. yield [
  374. '<?php $i += 1 and true;',
  375. ];
  376. yield [
  377. '<?php $i += 1 or true;',
  378. ];
  379. yield [
  380. '<?php $i += 1 xor true;',
  381. ];
  382. yield [
  383. '<?php $i += 1 === 2;',
  384. ];
  385. yield [
  386. '<?php $i += 1 == 2;',
  387. ];
  388. yield [
  389. '<?php $i += 1 !== 2;',
  390. ];
  391. yield [
  392. '<?php $i += 1 != 2;',
  393. ];
  394. yield [
  395. '<?php $i += 1 < 2;',
  396. ];
  397. yield [
  398. '<?php $i += 1 > 2;',
  399. ];
  400. yield [
  401. '<?php $i += 1 <= 2;',
  402. ];
  403. yield [
  404. '<?php $i += 1 >= 2;',
  405. ];
  406. yield [
  407. '<?php $i += 1 <> 2;',
  408. ];
  409. yield [
  410. '<?php $i -= 1 * 2;',
  411. ];
  412. yield [
  413. '<?php $i -= 1 ** 2;',
  414. ];
  415. yield [
  416. '<?php $i -= 1 / 2;',
  417. ];
  418. yield [
  419. '<?php $i -= 1 + 2;',
  420. ];
  421. yield [
  422. '<?php $i -= 1 - 2;',
  423. ];
  424. yield [
  425. '<?php $i -= 1 % 2;',
  426. ];
  427. yield [
  428. '<?php $i -= 1 ?: 2;',
  429. ];
  430. yield [
  431. '<?php $i -= 1 & 2;',
  432. ];
  433. yield [
  434. '<?php $i -= 1 ^ 2;',
  435. ];
  436. yield [
  437. '<?php $i -= 1 >> 2;',
  438. ];
  439. yield [
  440. '<?php $i -= 1 << 2;',
  441. ];
  442. yield [
  443. '<?php $i -= 1 && true;',
  444. ];
  445. yield [
  446. '<?php $i -= 1 || true;',
  447. ];
  448. yield [
  449. '<?php $i -= 1 and true;',
  450. ];
  451. yield [
  452. '<?php $i -= 1 or true;',
  453. ];
  454. yield [
  455. '<?php $i -= 1 xor true;',
  456. ];
  457. yield [
  458. '<?php $i -= 1 === 2;',
  459. ];
  460. yield [
  461. '<?php $i -= 1 == 2;',
  462. ];
  463. yield [
  464. '<?php $i -= 1 !== 2;',
  465. ];
  466. yield [
  467. '<?php $i -= 1 != 2;',
  468. ];
  469. yield [
  470. '<?php $i -= 1 < 2;',
  471. ];
  472. yield [
  473. '<?php $i -= 1 > 2;',
  474. ];
  475. yield [
  476. '<?php $i -= 1 <= 2;',
  477. ];
  478. yield [
  479. '<?php $i -= 1 >= 2;',
  480. ];
  481. yield [
  482. '<?php $i -= 1 <> 2;',
  483. ];
  484. yield [
  485. '<?php #1
  486. #2
  487. ++$i#3
  488. #4
  489. #5
  490. #6
  491. #7
  492. ;#8
  493. #9',
  494. '<?php #1
  495. #2
  496. $i#3
  497. #4
  498. +=#5
  499. #6
  500. 1#7
  501. ;#8
  502. #9',
  503. ];
  504. yield [
  505. '<?php $a -= ($a -= ($a -= (--$a)));',
  506. '<?php $a -= ($a -= ($a -= ($a -= 1)));',
  507. ];
  508. yield [
  509. '<?php --$a[foo($d,foo($c))];',
  510. '<?php $a[foo($d,foo($c))] -= 1;',
  511. ];
  512. yield [
  513. '<?php $i *= 1; ++$i;',
  514. '<?php $i *= 1; $i += 1;',
  515. ];
  516. yield [
  517. '<?php ++A::$b;',
  518. '<?php A::$b += 1;',
  519. ];
  520. yield [
  521. '<?php ++\A::$b;',
  522. '<?php \A::$b += 1;',
  523. ];
  524. yield [
  525. '<?php ++\A\B\C::$d;',
  526. '<?php \A\B\C::$d += 1;',
  527. ];
  528. yield [
  529. '<?php ++$a::$b;',
  530. '<?php $a::$b += 1;',
  531. ];
  532. yield [
  533. '<?php ++$a::$b->$c;',
  534. '<?php $a::$b->$c += 1;',
  535. ];
  536. yield [
  537. '<?php class Foo {
  538. public static function bar() {
  539. ++self::$v1;
  540. ++static::$v2;
  541. }
  542. }',
  543. '<?php class Foo {
  544. public static function bar() {
  545. self::$v1 += 1;
  546. static::$v2 += 1;
  547. }
  548. }',
  549. ];
  550. yield [
  551. '<?php $i -= 1 ?? 2;',
  552. ];
  553. yield [
  554. '<?php $i += 1 ?? 2;',
  555. ];
  556. yield [
  557. '<?php $i -= 1 <=> 2;',
  558. ];
  559. yield [
  560. '<?php $i += 1 <=> 2;',
  561. ];
  562. yield [
  563. '<?php ++$a::$b::$c;',
  564. '<?php $a::$b::$c += 1;',
  565. ];
  566. yield [
  567. '<?php ++$a->$b::$c;',
  568. '<?php $a->$b::$c += 1;',
  569. ];
  570. yield [
  571. '<?php ++$a::${$b}::$c;',
  572. '<?php $a::${$b}::$c += 1;',
  573. ];
  574. yield [
  575. '<?php ++$a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h;',
  576. '<?php $a->$b::$c->${$d}->${$e}::f(1 + 2 * 3)->$g::$h += 1;',
  577. ];
  578. yield [
  579. '<?php $i += 1_0;',
  580. ];
  581. }
  582. /**
  583. * @dataProvider provideFixPre80Cases
  584. *
  585. * @requires PHP <8.0
  586. */
  587. public function testFixPre80(string $expected, ?string $input = null): void
  588. {
  589. $this->doTest($expected, $input);
  590. }
  591. /**
  592. * @return iterable<array{string, string}>
  593. */
  594. public static function provideFixPre80Cases(): iterable
  595. {
  596. yield [
  597. '<?php echo ++$foo->{$bar};',
  598. '<?php echo $foo->{$bar} += 1;',
  599. ];
  600. yield [
  601. '<?php echo ++$foo->{$bar->{$baz}};',
  602. '<?php echo $foo->{$bar->{$baz}} += 1;',
  603. ];
  604. yield [
  605. '<?php ++$a{$b};',
  606. '<?php $a{$b} += 1;',
  607. ];
  608. yield [
  609. '<?php --$a{$b};',
  610. '<?php $a{$b} -= 1;',
  611. ];
  612. }
  613. }