GotoLabelAnalyzerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Tokenizer\Analyzer;
  13. use PhpCsFixer\Tests\TestCase;
  14. use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer
  20. */
  21. final class GotoLabelAnalyzerTest extends TestCase
  22. {
  23. /**
  24. * @param list<int> $expectedTrue
  25. *
  26. * @dataProvider provideGotoLabelCases
  27. */
  28. public function testGotoLabel(string $source, array $expectedTrue): void
  29. {
  30. $tokens = Tokens::fromCode($source);
  31. $analyzer = new GotoLabelAnalyzer();
  32. foreach ($tokens as $index => $isClassy) {
  33. self::assertSame(
  34. \in_array($index, $expectedTrue, true),
  35. $analyzer->belongsToGoToLabel($tokens, $index)
  36. );
  37. }
  38. }
  39. /**
  40. * @return iterable<array{string, list<int>}>
  41. */
  42. public static function provideGotoLabelCases(): iterable
  43. {
  44. yield 'no candidates' => [
  45. '<?php
  46. $a = \InvalidArgumentException::class;
  47. $this->fixer->configure($legacy ? [$statement] : [1]);
  48. ',
  49. [],
  50. ];
  51. yield 'after php tag' => [
  52. '<?php
  53. beginning:
  54. echo $guard?1:2;',
  55. [3],
  56. ];
  57. yield 'after closing brace' => [
  58. '<?php
  59. function A(){}
  60. beginning:
  61. echo $guard?1:2;',
  62. [11],
  63. ];
  64. yield 'after statement' => [
  65. '<?php
  66. echo 1;
  67. beginning:
  68. echo $guard?1:2;',
  69. [8],
  70. ];
  71. yield 'after opening brace' => [
  72. '<?php
  73. echo 1;
  74. {
  75. beginning:
  76. echo $guard?1:2;
  77. }
  78. ',
  79. [10],
  80. ];
  81. yield 'after use statements' => [
  82. '<?php
  83. use Bar1;
  84. use const Bar2;
  85. use function Bar3;
  86. Bar1:
  87. Bar2:
  88. Bar3:
  89. ',
  90. [21, 24, 27],
  91. ];
  92. }
  93. /**
  94. * @param list<int> $expectedTrue
  95. *
  96. * @dataProvider provideGotoLabel80Cases
  97. *
  98. * @requires PHP 8.0
  99. */
  100. public function testGotoLabel80(string $source, array $expectedTrue): void
  101. {
  102. $this->testGotoLabel($source, $expectedTrue);
  103. }
  104. /**
  105. * @return iterable<array{string, list<int>}>
  106. */
  107. public static function provideGotoLabel80Cases(): iterable
  108. {
  109. yield [
  110. '<?php array_fill(start_index: 0, num: 100, value: 50);',
  111. [],
  112. ];
  113. }
  114. }