* 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\Transformer; use PhpCsFixer\Tests\Test\AbstractTransformerTestCase; use PhpCsFixer\Tokenizer\CT; /** * @author Sebastiaans Stok * * @internal * * @covers \PhpCsFixer\Tokenizer\Transformer\BraceClassInstantiationTransformer * * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase * @phpstan-import-type _TransformerTestObservedKindsOrPrototypes from AbstractTransformerTestCase */ final class BraceClassInstantiationTransformerTest extends AbstractTransformerTestCase { /** * @param _TransformerTestExpectedTokens $expectedTokens * @param _TransformerTestObservedKindsOrPrototypes $observedKinds * * @dataProvider provideProcessCases */ public function testProcess(string $source, array $expectedTokens, array $observedKinds = []): void { $this->doTest( $source, $expectedTokens, $observedKinds ); } public static function provideProcessCases(): iterable { yield [ 'getOutput();', [ 3 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 9 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 9 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'bar(new Foo())->bar();', [ 4 => '(', 5 => ')', 8 => '(', 12 => '(', 13 => ')', 14 => ')', 17 => '(', 18 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'bar();', [ 5 => '(', 9 => '(', 10 => ')', 11 => ')', 14 => '(', 15 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'bar();', [ 2 => '(', 6 => '(', 7 => ')', 8 => ')', 11 => '(', 12 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'bar();', [ 3 => '(', 7 => '(', 8 => ')', 9 => ')', 12 => '(', 13 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'foo();', [ 8 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 12 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ 'a);', [ 5 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 12 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 10 => '(', 11 => ')', 12 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => '(', 8 => ')', 9 => ')', 17 => '(', 21 => '(', 22 => ')', 23 => ')', 33 => '(', 37 => '(', 38 => ')', 39 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => '(', 8 => ')', 9 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => '(', 8 => ')', 11 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => '(', 8 => ')', 13 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => '(', 8 => ')', 9 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 13 => '(', 14 => ')', 15 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 13 => '(', 15 => ')', 16 => ')', ], [ '(', ')', '(', ')', ], ]; yield [ ' '(', 13 => '(', 14 => ')', 15 => ')', ], [ '(', ')', ], ]; yield [ ' CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 20 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' null;', [ 6 => '(', 7 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' '(', 7 => ')', 8 => '(', 12 => '(', 13 => ')', 14 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; yield [ ' CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 9 => '(', 10 => ')', 11 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, 12 => '(', 16 => '(', 17 => ')', 18 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; } /** * @param _TransformerTestExpectedTokens $expectedTokens * @param _TransformerTestObservedKindsOrPrototypes $observedKinds * * @dataProvider provideProcessPhp80Cases * * @requires PHP 8.0 */ public function testProcessPhp80(array $expectedTokens, array $observedKinds, string $source): void { $this->doTest( $source, $expectedTokens, $observedKinds ); } public static function provideProcessPhp80Cases(): iterable { yield [ [ 5 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 8 => '(', 10 => '(', 11 => ')', 12 => ')', 13 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ' CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 15 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], '', ]; } /** * @param _TransformerTestExpectedTokens $expectedTokens * @param _TransformerTestObservedKindsOrPrototypes $observedKinds * * @dataProvider provideProcessPhp81Cases * * @requires PHP 8.1 */ public function testProcessPhp81(array $expectedTokens, array $observedKinds, string $source): void { $this->doTest( $source, $expectedTokens, $observedKinds ); } public static function provideProcessPhp81Cases(): iterable { yield [ [ 20 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 24 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, 43 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 47 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, 54 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 64 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, 107 => CT::T_BRACE_CLASS_INSTANTIATION_OPEN, 111 => CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], [ CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], 'doTest( $source, $expectedTokens, $observedKinds ); } /** * @return iterable */ public static function provideProcessPrePhp84Cases(): iterable { yield [ 'bar();', [ 5 => '(', 9 => '(', 10 => ')', 11 => ')', 14 => '(', 15 => ')', ], [ '(', ')', CT::T_BRACE_CLASS_INSTANTIATION_OPEN, CT::T_BRACE_CLASS_INSTANTIATION_CLOSE, ], ]; } }