123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * 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 <s.stok@rollerscapes.net>
- *
- * @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 [
- '<?php echo (new Process())->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 [
- '<?php echo (new Process())::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 [
- '<?php return foo()->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 [
- '<?php $foo[0](new Foo())->bar();',
- [
- 5 => '(',
- 9 => '(',
- 10 => ')',
- 11 => ')',
- 14 => '(',
- 15 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php $foo(new Foo())->bar();',
- [
- 2 => '(',
- 6 => '(',
- 7 => ')',
- 8 => ')',
- 11 => '(',
- 12 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php $$foo(new Foo())->bar();',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 9 => ')',
- 12 => '(',
- 13 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php if ($foo){}(new Foo)->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 [
- '<?php echo (((new \stdClass()))->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 [
- '<?php $foo = array(new Foo());',
- [
- 6 => '(',
- 10 => '(',
- 11 => ')',
- 12 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php if (new Foo()) { } elseif (new Bar()) { } else if (new Baz()) { }',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 9 => ')',
- 17 => '(',
- 21 => '(',
- 22 => ')',
- 23 => ')',
- 33 => '(',
- 37 => '(',
- 38 => ')',
- 39 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php switch (new Foo()) { }',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 9 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php for (new Foo();;) { }',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 11 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php foreach (new Foo() as $foo) { }',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 13 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php while (new Foo()) { }',
- [
- 3 => '(',
- 7 => '(',
- 8 => ')',
- 9 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php do { } while (new Foo());',
- [
- 9 => '(',
- 13 => '(',
- 14 => ')',
- 15 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php $static = new static(new \SplFileInfo(__FILE__));',
- [
- 8 => '(',
- 13 => '(',
- 15 => ')',
- 16 => ')',
- ],
- [
- '(',
- ')',
- '(',
- ')',
- ],
- ];
- yield [
- '<?php $foo = new class(new \stdClass()) {};',
- [
- 8 => '(',
- 13 => '(',
- 14 => ')',
- 15 => ')',
- ],
- [
- '(',
- ')',
- ],
- ];
- yield [
- '<?php $foo = (new class(new \stdClass()) {});',
- [
- 5 => 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 [
- '<?php $fn = fn() => null;',
- [
- 6 => '(',
- 7 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php $result = ($function)(new Argument());',
- [
- 5 => '(',
- 7 => ')',
- 8 => '(',
- 12 => '(',
- 13 => ')',
- 14 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- yield [
- '<?php $result = (new Invokable())(new Argument1());',
- [
- 5 => 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,
- ],
- '<?php $a = (new (foo()));',
- ];
- yield [
- [
- 5 => 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,
- ],
- '<?php $a = (new #[Foo]
- class{}) ?>',
- ];
- }
- /**
- * @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,
- ],
- '<?php
- class Test {
- public function __construct(
- public $prop = (new Foo),
- ) {}
- }
- function test(
- $foo = (new A),
- $baz = (new C(x: 2)),
- ) {
- }
- static $x = new (Foo);
- const C = new (Foo);
- function test2($param = (new Foo)) {}
- ',
- ];
- }
- /**
- * @param _TransformerTestExpectedTokens $expectedTokens
- * @param _TransformerTestObservedKindsOrPrototypes $observedKinds
- *
- * @dataProvider provideProcessPrePhp84Cases
- *
- * @requires PHP <8.4
- */
- public function testProcessPrePhp84(string $source, array $expectedTokens, array $observedKinds = []): void
- {
- $this->doTest(
- $source,
- $expectedTokens,
- $observedKinds
- );
- }
- /**
- * @return iterable<array{string, _TransformerTestExpectedTokens, _TransformerTestObservedKindsOrPrototypes}>
- */
- public static function provideProcessPrePhp84Cases(): iterable
- {
- yield [
- '<?php $foo{0}(new Foo())->bar();',
- [
- 5 => '(',
- 9 => '(',
- 10 => ')',
- 11 => ')',
- 14 => '(',
- 15 => ')',
- ],
- [
- '(',
- ')',
- CT::T_BRACE_CLASS_INSTANTIATION_OPEN,
- CT::T_BRACE_CLASS_INSTANTIATION_CLOSE,
- ],
- ];
- }
- }
|