* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Tokenizer\Analyzer; use PhpCsFixer\Tests\TestCase; use PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer; use PhpCsFixer\Tokenizer\Tokens; /** * @internal * * @covers \PhpCsFixer\Tokenizer\Analyzer\SwitchAnalyzer */ final class SwitchAnalyzerTest extends TestCase { /** * @param list $indices * * @dataProvider provideColonCases */ public function testColon(string $code, array $indices): void { $tokens = Tokens::fromCode($code); for ($index = $tokens->count() - 1; $index >= 0; --$index) { self::assertSame( \in_array($index, $indices, true), SwitchAnalyzer::belongsToSwitch($tokens, $index), \sprintf('Index %d failed check.', $index) ); } } /** * @return iterable}> */ public static function provideColonCases(): iterable { yield 'ternary operator' => [ ' [ ' [ ' [ ' [ 'clearAt(2); $tokensWithoutEmpty = clone $tokensWithEmpty; $tokensWithoutEmpty->clearEmptyTokens(); self::assertStringNotContainsString('to remove', $tokensWithEmpty->generateCode()); self::assertStringNotContainsString('to remove', $tokensWithoutEmpty->generateCode()); self::assertSame( $tokensWithEmpty->count(), $tokensWithoutEmpty->count() + 1 ); self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithEmpty, 12)); self::assertTrue(SwitchAnalyzer::belongsToSwitch($tokensWithoutEmpty, 11)); } }