PhpdocArrayTypeFixerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\Phpdoc;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocArrayTypeFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocArrayTypeFixer>
  20. */
  21. final class PhpdocArrayTypeFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. <<<'PHP'
  37. <?php
  38. /** @tagNotSupportingTypes string[] */
  39. /** @var array{'a', 'b[]', 'c'} */
  40. /** @var 'x|T1[][]|y' */
  41. /** @var "x|T2[][]|y" */
  42. PHP,
  43. ];
  44. yield [
  45. '<?php /** @var array<int> */',
  46. '<?php /** @var int[] */',
  47. ];
  48. yield [
  49. '<?php /** @param array<array<array<array<int>>>> $x */',
  50. '<?php /** @param int[][][][] $x */',
  51. ];
  52. yield [
  53. '<?php /** @param array<array<array<array<int>>>> $x */',
  54. '<?php /** @param int [][ ][] [] $x */',
  55. ];
  56. yield [
  57. '<?php /** @return iterable<array<int>> */',
  58. '<?php /** @return iterable<int[]> */',
  59. ];
  60. yield [
  61. '<?php /** @var array<Foo\Bar> */',
  62. '<?php /** @var Foo\Bar[] */',
  63. ];
  64. yield [
  65. '<?php /** @var array<Foo_Bar> */',
  66. '<?php /** @var Foo_Bar[] */',
  67. ];
  68. yield [
  69. '<?php /** @var array<bool>|array<float>|array<int>|array<string> */',
  70. '<?php /** @var array<bool>|float[]|array<int>|string[] */',
  71. ];
  72. yield [
  73. <<<'PHP'
  74. <?php
  75. /**
  76. * @param array{foo?: array<int>} $foo
  77. * @param callable(array<int>): array<int> $bar
  78. */
  79. PHP,
  80. <<<'PHP'
  81. <?php
  82. /**
  83. * @param array{foo?: int[]} $foo
  84. * @param callable(int[]): int[] $bar
  85. */
  86. PHP,
  87. ];
  88. yield [
  89. <<<'PHP'
  90. <?php
  91. /**
  92. * @var array<array{'a'}>
  93. * @var \Closure(iterable<\DateTime>): array<void>
  94. * @var array<Yes>|'No[][]'|array<array<Yes>>|'No[]'
  95. */
  96. PHP,
  97. <<<'PHP'
  98. <?php
  99. /**
  100. * @var array{'a'}[]
  101. * @var \Closure(iterable<\DateTime>): void[]
  102. * @var Yes[]|'No[][]'|Yes[][]|'No[]'
  103. */
  104. PHP,
  105. ];
  106. yield [
  107. <<<'PHP'
  108. <?php
  109. /**
  110. * @param ?array<type> $a
  111. * @param ?array<array<type>> $b
  112. * @param ?'literal[]' $c
  113. */
  114. PHP,
  115. <<<'PHP'
  116. <?php
  117. /**
  118. * @param ?type[] $a
  119. * @param ?type[][] $b
  120. * @param ?'literal[]' $c
  121. */
  122. PHP,
  123. ];
  124. yield [
  125. '<?php /** @var array<Foo|Bar> */',
  126. '<?php /** @var (Foo|Bar)[] */',
  127. ];
  128. yield [
  129. '<?php /** @var (Foo&Bar)|array<Baz> */',
  130. '<?php /** @var (Foo&Bar)|Baz[] */',
  131. ];
  132. $expected = $input = 'string';
  133. for ($i = 0; $i < 128; ++$i) {
  134. $expected = 'array<'.$expected.'>';
  135. $input .= '[]';
  136. }
  137. yield [
  138. \sprintf('<?php /** @var %s */', $expected),
  139. \sprintf('<?php /** @var %s */', $input),
  140. ];
  141. }
  142. }