OrderedTypesFixerTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer>
  22. *
  23. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedTypesFixer
  24. */
  25. final class OrderedTypesFixerTest extends AbstractFixerTestCase
  26. {
  27. /**
  28. * @dataProvider provideFixCases
  29. *
  30. * @param _AutogeneratedInputConfiguration $config
  31. */
  32. public function testFix(string $expected, ?string $input = null, ?array $config = null): void
  33. {
  34. if (null !== $config) {
  35. $this->fixer->configure($config);
  36. }
  37. $this->doTest($expected, $input);
  38. }
  39. /**
  40. * @return iterable<string, array{string, 1?: ?string, 2?: array<string, mixed>}>
  41. */
  42. public static function provideFixCases(): iterable
  43. {
  44. yield 'catch with default, no spaces, with both leading slash' => [
  45. '<?php
  46. try {
  47. $this->foo();
  48. } catch (\LogicException|\RuntimeException $e) {
  49. // $e
  50. }
  51. ',
  52. '<?php
  53. try {
  54. $this->foo();
  55. } catch (\RuntimeException|\LogicException $e) {
  56. // $e
  57. }
  58. ',
  59. ];
  60. yield 'catch with default, with spaces, with both leading slash' => [
  61. '<?php
  62. try {
  63. $this->foo();
  64. } catch (\LogicException|\RuntimeException $e) {
  65. // $e
  66. }
  67. ',
  68. '<?php
  69. try {
  70. $this->foo();
  71. } catch (\RuntimeException | \LogicException $e) {
  72. // $e
  73. }
  74. ',
  75. ];
  76. yield 'catch with default, no spaces, with no leading slash' => [
  77. '<?php
  78. try {
  79. cache()->save();
  80. } catch (CacheException|SimpleCacheException $e) {
  81. // $e
  82. }
  83. ',
  84. '<?php
  85. try {
  86. cache()->save();
  87. } catch (SimpleCacheException|CacheException $e) {
  88. // $e
  89. }
  90. ',
  91. ];
  92. yield 'catch with default, with spaces, with one leading slash' => [
  93. '<?php
  94. try {
  95. cache()->save();
  96. } catch (CacheException|\RuntimeException $e) {
  97. // $e
  98. }
  99. ',
  100. '<?php
  101. try {
  102. cache()->save();
  103. } catch (\RuntimeException | CacheException $e) {
  104. // $e
  105. }
  106. ',
  107. ];
  108. yield 'catch with no sorting' => [
  109. '<?php
  110. try {
  111. $this->foo();
  112. } catch (\RuntimeException|\LogicException $e) {
  113. // $e
  114. }
  115. ',
  116. null,
  117. ['sort_algorithm' => 'none'],
  118. ];
  119. yield 'nothing to fix' => [
  120. '<?php
  121. try {
  122. $this->foo();
  123. } catch (\LogicException $e) {
  124. // $e
  125. }
  126. ',
  127. ];
  128. yield 'already fixed' => [
  129. '<?php
  130. try {
  131. $this->foo();
  132. } catch (LogicException|RuntimeException $e) {
  133. // $e
  134. }
  135. ',
  136. ];
  137. }
  138. /**
  139. * @dataProvider provideFix80Cases
  140. *
  141. * @param _AutogeneratedInputConfiguration $config
  142. *
  143. * @requires PHP 8.0
  144. */
  145. public function testFix80(string $expected, ?string $input = null, ?array $config = null): void
  146. {
  147. if (null !== $config) {
  148. $this->fixer->configure($config);
  149. }
  150. $this->doTest($expected, $input);
  151. }
  152. /**
  153. * @return iterable<int|string, array{string, 1?: ?string, 2?: array<string, mixed>}>
  154. */
  155. public static function provideFix80Cases(): iterable
  156. {
  157. yield 'sort alpha, null none' => [
  158. "<?php\nclass Foo\n{\n public A|null|Z \$bar;\n}\n",
  159. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  160. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  161. ];
  162. yield 'sort alpha, null first' => [
  163. "<?php\nclass Foo\n{\n public null|A|Z \$bar;\n}\n",
  164. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  165. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_first'],
  166. ];
  167. yield 'sort alpha, null last' => [
  168. "<?php\nclass Foo\n{\n public A|Z|null \$bar;\n}\n",
  169. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  170. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  171. ];
  172. yield 'sort none, null first' => [
  173. "<?php\nclass Foo\n{\n public null|Z|A \$bar;\n}\n",
  174. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  175. ['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
  176. ];
  177. yield 'sort none, null last' => [
  178. "<?php\nclass Foo\n{\n public Z|A|null \$bar;\n}\n",
  179. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  180. ['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
  181. ];
  182. yield 'sort none, null none' => [
  183. "<?php\nclass Foo\n{\n public Z|null|A \$bar;\n}\n",
  184. null,
  185. ['sort_algorithm' => 'none', 'null_adjustment' => 'none'],
  186. ];
  187. yield [
  188. "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n",
  189. "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n",
  190. ];
  191. yield [
  192. "<?php\nclass Foo\n{\n public null|A|B \$foo = null;\n}\n",
  193. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  194. ];
  195. yield [
  196. "<?php\nclass Foo\n{\n public null|\\A|B \$foo = null;\n}\n",
  197. "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n",
  198. ];
  199. yield [
  200. "<?php\nclass Foo\n{\n public null|\\A|\\B \$foo = null;\n}\n",
  201. "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n",
  202. ];
  203. yield [
  204. "<?php\nclass Foo\n{\n public null|int|string/* array */ \$bar = null;\n}\n",
  205. "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n",
  206. ];
  207. yield [
  208. "<?php\nclass Foo\n{\n public /* int */null|A|B \$foo = null;\n}\n",
  209. "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n",
  210. ];
  211. yield [
  212. "<?php\nclass Foo\n{\n public null|A|B \$foo = null;\n}\n",
  213. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  214. ];
  215. yield [
  216. "<?php\nclass Foo\n{\n private function bar(null|array|callable|int \$cb) {}\n}\n",
  217. "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n",
  218. ];
  219. yield [
  220. "<?php\nclass Foo\n{\n private function bar(\$cb): null|array|callable|int {}\n}\n",
  221. "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n",
  222. ];
  223. yield [
  224. "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n",
  225. "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n",
  226. ];
  227. yield [
  228. "<?php\nclass Foo\n{\n public function bar(null|string \$str, null|array|int \$arr) {}\n}\n",
  229. "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n",
  230. ];
  231. yield [
  232. "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n",
  233. "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n",
  234. ];
  235. yield [
  236. '<?php function foo(null|int|string $bar): null|\Stringable {}',
  237. '<?php function foo(string|int|null $bar): \Stringable|null {}',
  238. ];
  239. yield [
  240. '<?php fn (null|\Countable|int $number): null|int => $number;',
  241. '<?php fn (int|\Countable|null $number): int|null => $number;',
  242. ];
  243. yield [
  244. "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n",
  245. "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n",
  246. ];
  247. yield [
  248. '<?php
  249. class Foo
  250. {
  251. public ?string $foo = null;
  252. public /* int|string */ $bar;
  253. private null|array $baz = null;
  254. public function baz(): null|string {}
  255. protected function bar(string $str, ?array $config = null): callable {}
  256. }
  257. try {
  258. (new Foo)->baz();
  259. } catch (Exception $e) {
  260. return $e;
  261. }
  262. ',
  263. ];
  264. yield [
  265. "<?php\nclass Foo\n{\n public int|string|null \$bar = null;\n}\n",
  266. "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n",
  267. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  268. ];
  269. yield [
  270. "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n",
  271. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  272. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  273. ];
  274. yield [
  275. "<?php\nclass Foo\n{\n public \\A|B|null \$foo = null;\n}\n",
  276. "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n",
  277. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  278. ];
  279. yield [
  280. "<?php\nclass Foo\n{\n public \\A|\\B|null \$foo = null;\n}\n",
  281. "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n",
  282. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  283. ];
  284. yield [
  285. "<?php\nclass Foo\n{\n public int|string|null/* array */ \$bar = null;\n}\n",
  286. "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n",
  287. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  288. ];
  289. yield [
  290. "<?php\nclass Foo\n{\n public /* int */A|B|null \$foo = null;\n}\n",
  291. "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n",
  292. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  293. ];
  294. yield [
  295. "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n",
  296. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  297. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  298. ];
  299. yield [
  300. "<?php\nclass Foo\n{\n private function bar(array|callable|int|null \$cb) {}\n}\n",
  301. "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n",
  302. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  303. ];
  304. yield [
  305. "<?php\nclass Foo\n{\n private function bar(\$cb): array|callable|int|null {}\n}\n",
  306. "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n",
  307. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  308. ];
  309. yield [
  310. "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n",
  311. "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n",
  312. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  313. ];
  314. yield [
  315. "<?php\nclass Foo\n{\n public function bar(string|null \$str, array|int|null \$arr) {}\n}\n",
  316. "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n",
  317. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  318. ];
  319. yield [
  320. "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n",
  321. "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n",
  322. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  323. ];
  324. yield [
  325. '<?php function foo(int|string|null $bar): \Stringable|null {}',
  326. '<?php function foo(string|int|null $bar): \Stringable|null {}',
  327. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  328. ];
  329. yield [
  330. '<?php fn (\Countable|int|null $number): int|null => $number;',
  331. '<?php fn (int|\Countable|null $number): int|null => $number;',
  332. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  333. ];
  334. yield [
  335. "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n",
  336. "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n",
  337. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  338. ];
  339. yield [
  340. '<?php
  341. class Foo
  342. {
  343. public ?string $foo = null;
  344. public /* int|string */ $bar;
  345. private array|null $baz = null;
  346. public function baz(): string|null {}
  347. protected function bar(string $str, ?array $config = null): callable {}
  348. }
  349. try {
  350. (new Foo)->baz();
  351. } catch (Exception $e) {
  352. return $e;
  353. }
  354. ',
  355. null,
  356. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
  357. ];
  358. yield [
  359. "<?php\nclass Foo\n{\n public int|null|string \$bar = null;\n}\n",
  360. "<?php\nclass Foo\n{\n public string|null|int \$bar = null;\n}\n",
  361. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  362. ];
  363. yield [
  364. "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n",
  365. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  366. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  367. ];
  368. yield [
  369. "<?php\nclass Foo\n{\n public \\A|B|null \$foo = null;\n}\n",
  370. "<?php\nclass Foo\n{\n public B|\\A|null \$foo = null;\n}\n",
  371. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  372. ];
  373. yield [
  374. "<?php\nclass Foo\n{\n public \\A|\\B|null \$foo = null;\n}\n",
  375. "<?php\nclass Foo\n{\n public \\B|\\A|null \$foo = null;\n}\n",
  376. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  377. ];
  378. yield [
  379. "<?php\nclass Foo\n{\n public int|null|string/* array */ \$bar = null;\n}\n",
  380. "<?php\nclass Foo\n{\n public string|null|int/* array */ \$bar = null;\n}\n",
  381. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  382. ];
  383. yield [
  384. "<?php\nclass Foo\n{\n public /* int */A|B|null \$foo = null;\n}\n",
  385. "<?php\nclass Foo\n{\n public /* int */B|A|null \$foo = null;\n}\n",
  386. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  387. ];
  388. yield [
  389. "<?php\nclass Foo\n{\n public A|B|null \$foo = null;\n}\n",
  390. "<?php\nclass Foo\n{\n public B|A|null \$foo = null;\n}\n",
  391. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  392. ];
  393. yield [
  394. "<?php\nclass Foo\n{\n private function bar(array|callable|int|null \$cb) {}\n}\n",
  395. "<?php\nclass Foo\n{\n private function bar(array|int|callable|null \$cb) {}\n}\n",
  396. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  397. ];
  398. yield [
  399. "<?php\nclass Foo\n{\n private function bar(\$cb): array|callable|int|null {}\n}\n",
  400. "<?php\nclass Foo\n{\n private function bar(\$cb): array|int|callable|null {}\n}\n",
  401. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  402. ];
  403. yield [
  404. "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n",
  405. "<?php\nclass Foo\n{\n private function bar(\$cb): static|null {}\n}\n",
  406. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  407. ];
  408. yield [
  409. "<?php\nclass Foo\n{\n public function bar(null|string \$str, array|int|null \$arr) {}\n}\n",
  410. "<?php\nclass Foo\n{\n public function bar(string|null \$str, int|array|null \$arr) {}\n}\n",
  411. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  412. ];
  413. yield [
  414. "<?php\nclass Foo\n{\n public function bar(\\JsonSerializable|\\Stringable \$obj): array|int {}\n}\n",
  415. "<?php\nclass Foo\n{\n public function bar(\\Stringable|\\JsonSerializable \$obj): int|array {}\n}\n",
  416. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  417. ];
  418. yield [
  419. '<?php function foo(int|null|string $bar): null|\Stringable {}',
  420. '<?php function foo(string|int|null $bar): \Stringable|null {}',
  421. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  422. ];
  423. yield [
  424. '<?php fn (\Countable|int|null $number): int|null => $number;',
  425. '<?php fn (int|\Countable|null $number): int|null => $number;',
  426. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  427. ];
  428. yield [
  429. "<?php\ntry {\n foo();\n} catch (\\Error|\\TypeError) {\n}\n",
  430. "<?php\ntry {\n foo();\n} catch (\\TypeError|\\Error) {\n}\n",
  431. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  432. ];
  433. yield [
  434. '<?php
  435. class Foo
  436. {
  437. public ?string $foo = null;
  438. public /* int|string */ $bar;
  439. private array|null $baz = null;
  440. public function baz(): null|string {}
  441. protected function bar(string $str, ?array $config = null): callable {}
  442. }
  443. try {
  444. (new Foo)->baz();
  445. } catch (Exception $e) {
  446. return $e;
  447. }
  448. ',
  449. null,
  450. ['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
  451. ];
  452. yield [
  453. "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n",
  454. "<?php\nclass Foo\n{\n public string | null | int \$bar = null;\n}\n",
  455. ];
  456. yield [
  457. "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n",
  458. "<?php\nclass Foo\n{\n public string | null | int \$bar = null;\n}\n",
  459. ];
  460. yield [
  461. "<?php\nclass Foo\n{\n public null|int|string \$bar = null;\n}\n",
  462. "<?php\nclass Foo\n{\n public string |/* array| */null|int \$bar = null;\n}\n",
  463. ];
  464. yield [
  465. "<?php\nclass Foo\n{\n private function bar(\$cb): null|static {}\n}\n",
  466. "<?php\nclass Foo\n{\n private function bar(\$cb): static /* |int */ | null {}\n}\n",
  467. ];
  468. yield 'case sensitive cases' => [
  469. "<?php\nclass Foo\n{\n public null|AAa|Aa \$bar = null;\n}\n",
  470. "<?php\nclass Foo\n{\n public Aa|AAa|null \$bar = null;\n}\n",
  471. ['case_sensitive' => true],
  472. ];
  473. }
  474. /**
  475. * @dataProvider provideFix81Cases
  476. *
  477. * @param _AutogeneratedInputConfiguration $config
  478. *
  479. * @requires PHP 8.1
  480. */
  481. public function testFix81(string $expected, ?string $input = null, ?array $config = null): void
  482. {
  483. if (null !== $config) {
  484. $this->fixer->configure($config);
  485. }
  486. $this->doTest($expected, $input);
  487. }
  488. /**
  489. * @return iterable<array{string, 1: ?string, 2?: array<string, mixed>}>
  490. */
  491. public static function provideFix81Cases(): iterable
  492. {
  493. yield [
  494. "<?php\nclass Foo\n{\n public A&B \$bar;\n}\n",
  495. "<?php\nclass Foo\n{\n public B&A \$bar;\n}\n",
  496. ];
  497. yield [
  498. "<?php\nclass Foo\n{\n public Ae&At \$bar;\n}\n",
  499. "<?php\nclass Foo\n{\n public At&Ae \$bar;\n}\n",
  500. ];
  501. yield [
  502. "<?php\nclass Foo\n{\n public readonly null|A|B \$bar;\n}\n",
  503. "<?php\nclass Foo\n{\n public readonly B|null|A \$bar;\n}\n",
  504. ];
  505. yield [
  506. "<?php\nclass Foo\n{\n public readonly A|B|null \$bar;\n}\n",
  507. "<?php\nclass Foo\n{\n public readonly B|null|A \$bar;\n}\n",
  508. ['null_adjustment' => 'always_last'],
  509. ];
  510. yield [
  511. "<?php\nclass Foo\n{\n public readonly A|null|X \$bar;\n}\n",
  512. "<?php\nclass Foo\n{\n public readonly X|A|null \$bar;\n}\n",
  513. ['null_adjustment' => 'none'],
  514. ];
  515. yield [
  516. "<?php\nclass Foo\n{\n public B&A \$bar;\n}\n",
  517. null,
  518. ['sort_algorithm' => 'none'],
  519. ];
  520. }
  521. /**
  522. * Provisional support for PHP 8.2's Disjunctive Normal Form (DNF) Types.
  523. *
  524. * @dataProvider provideFix82Cases
  525. *
  526. * @param _AutogeneratedInputConfiguration $config
  527. *
  528. * @requires PHP 8.2
  529. */
  530. public function testFix82(string $expected, ?string $input = null, ?array $config = null): void
  531. {
  532. if (null !== $config) {
  533. $this->fixer->configure($config);
  534. }
  535. $this->doTest($expected, $input);
  536. }
  537. /**
  538. * @return iterable<array{string, 1: ?string, 2?: array<string, mixed>}>
  539. */
  540. public static function provideFix82Cases(): iterable
  541. {
  542. yield [
  543. "<?php\nclass Foo\n{\n public null|array|(At&Bz)|string \$bar = null;\n}\n",
  544. "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n",
  545. ];
  546. yield [
  547. "<?php\nclass Foo\n{\n public array|(At&Bz)|string|null \$bar = null;\n}\n",
  548. "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n",
  549. ['null_adjustment' => 'always_last'],
  550. ];
  551. yield [
  552. "<?php\nclass Foo\n{\n public array|(At&Bz)|null|string \$bar = null;\n}\n",
  553. "<?php\nclass Foo\n{\n public string|(Bz&At)|array|null \$bar = null;\n}\n",
  554. ['null_adjustment' => 'none'],
  555. ];
  556. yield [
  557. "<?php\nclass Foo\n{\n public (A&B)|(A&C)|(B&D)|(C&D) \$bar;\n}\n",
  558. "<?php\nclass Foo\n{\n public (D&B)|(C&A)|(B&A)|(D&C) \$bar;\n}\n",
  559. ['sort_algorithm' => 'alpha'],
  560. ];
  561. yield [
  562. "<?php\nclass Foo\n{\n public (A&B)|(\\A&C)|(B&\\D)|(C&D) \$bar;\n}\n",
  563. "<?php\nclass Foo\n{\n public (\\D&B)|(C&\\A)|(B&A)|(D&C) \$bar;\n}\n",
  564. ['sort_algorithm' => 'alpha'],
  565. ];
  566. yield [
  567. "<?php\nclass Foo\n{\n public (A&C)|(B&D) \$bar;\n}\n",
  568. "<?php\nclass Foo\n{\n public (D&B)|(C&A) \$bar;\n}\n",
  569. ['sort_algorithm' => 'alpha'],
  570. ];
  571. yield [
  572. "<?php\nclass Foo\n{\n public (\\A&\\C)|(\\B&\\D) \$bar;\n}\n",
  573. "<?php\nclass Foo\n{\n public (\\D&\\B)|(\\C&\\A) \$bar;\n}\n",
  574. ['sort_algorithm' => 'alpha'],
  575. ];
  576. }
  577. }