SwitchAnalyzerTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\SwitchAnalyzer;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer
  20. */
  21. final class SwitchAnalyzerTest extends TestCase
  22. {
  23. /**
  24. * @param list<int> $indices
  25. *
  26. * @dataProvider provideColonCases
  27. */
  28. public function testColon(string $code, array $indices): void
  29. {
  30. $tokens = Tokens::fromCode($code);
  31. for ($index = $tokens->count() - 1; $index >= 0; --$index) {
  32. self::assertSame(
  33. \in_array($index, $indices, true),
  34. SwitchAnalyzer::belongsToSwitch($tokens, $index),
  35. \sprintf('Index %d failed check.', $index)
  36. );
  37. }
  38. }
  39. /**
  40. * @return iterable<array{string, list<int>}>
  41. */
  42. public static function provideColonCases(): iterable
  43. {
  44. yield 'ternary operator' => [
  45. '<?php $x ? 1 : 0;',
  46. [],
  47. ];
  48. yield 'alternative syntax' => [
  49. '<?php if(true): 3; endif;',
  50. [],
  51. ];
  52. yield 'label' => [
  53. '<?php gotoHere: echo "here";',
  54. [],
  55. ];
  56. yield 'switch' => [
  57. '<?php
  58. switch ($value1) {
  59. case 1: return 2;
  60. case 3: return 4;
  61. default: return 5;
  62. }
  63. ',
  64. [13, 23, 31],
  65. ];
  66. yield 'switch with alternative syntax' => [
  67. '<?php
  68. switch ($value1):
  69. case 1: return 2;
  70. default: return 3;
  71. case 4: return 5;
  72. endswitch;
  73. ',
  74. [7, 12, 20, 30],
  75. ];
  76. }
  77. public function testWithEmptyTokens(): void
  78. {
  79. $tokensWithEmpty = Tokens::fromCode(<<<'PHP'
  80. <?php
  81. switch/* to remove */($value1) {
  82. case 1: return 2;
  83. }
  84. PHP);
  85. $tokensWithEmpty->clearAt(2);
  86. $tokensWithoutEmpty = clone $tokensWithEmpty;
  87. $tokensWithoutEmpty->clearEmptyTokens();
  88. self::assertStringNotContainsString('to remove', $tokensWithEmpty->generateCode());
  89. self::assertStringNotContainsString('to remove', $tokensWithoutEmpty->generateCode());
  90. self::assertSame(
  91. $tokensWithEmpty->count(),
  92. $tokensWithoutEmpty->count() + 1
  93. );
  94. self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithEmpty, 12));
  95. self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithoutEmpty, 11));
  96. }
  97. }