123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465 |
- <?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;
- use PhpCsFixer\Tokenizer\Tokens;
- use PhpCsFixer\Tokenizer\Transformer\SquareBraceTransformer;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\Transformer\SquareBraceTransformer
- *
- * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
- */
- final class SquareBraceTransformerTest extends AbstractTransformerTestCase
- {
- /**
- * @param list<int> $inspectIndexes
- *
- * @dataProvider provideIsShortArrayCases
- */
- public function testIsShortArray(string $source, array $inspectIndexes, bool $expected): void
- {
- $transformer = new SquareBraceTransformer();
- $tokens = Tokens::fromCode($source);
- foreach ($inspectIndexes as $index) {
- self::assertTrue($tokens->offsetExists($index), \sprintf('Index %d does not exist.', $index));
- }
- foreach ($tokens as $index => $token) {
- if (\in_array($index, $inspectIndexes, true)) {
- self::assertSame('[', $tokens[$index]->getContent(), \sprintf('Token @ index %d must have content \']\'', $index));
- $exp = $expected;
- } elseif ('[' === $tokens[$index]->getContent()) {
- $exp = !$expected;
- } else {
- continue;
- }
- self::assertSame(
- $expected,
- \Closure::bind(static fn (SquareBraceTransformer $transformer): bool => $transformer->isShortArray($tokens, $index), null, SquareBraceTransformer::class)($transformer),
- \sprintf('Excepted token "%s" @ index %d %sto be detected as short array.', $tokens[$index]->toJson(), $index, $exp ? '' : 'not ')
- );
- }
- }
- public static function provideIsShortArrayCases(): iterable
- {
- yield ['<?php $a=[];', [3], false];
- yield ['<?php [$a] = [$b];', [7], false];
- yield ['<?php [$a] = $b;', [1], false];
- yield ['<?php [$a] = [$b] = [$b];', [1], false];
- yield ['<?php function A(){}[$a] = [$b] = [$b];', [8], false];
- yield ['<?php [$foo, $bar] = [$baz, $bat] = [$a, $b];', [10], false];
- yield ['<?php [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];', [1], false];
- yield ['<?php ["a" => $a, "b" => $b, "c" => $c] = $array;', [1], false];
- yield ['<?php [$a, $b,, [$c, $d]] = $a;', [1, 9], false];
- }
- /**
- * @param _TransformerTestExpectedTokens $expectedTokens
- *
- * @dataProvider provideProcessCases
- */
- public function testProcess(string $source, array $expectedTokens = []): void
- {
- $this->doTest(
- $source,
- $expectedTokens,
- [
- CT::T_ARRAY_SQUARE_BRACE_OPEN,
- CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ]
- );
- }
- public static function provideProcessCases(): iterable
- {
- yield 'Array offset only.' => [
- '<?php $a = array(); $a[] = 0; $a[1] = 2;',
- ];
- yield 'Short array construction.' => [
- '<?php $b = [1, 2, 3];',
- [
- 5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 13 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php function foo(array $c = [ ]) {}',
- [
- 11 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 13 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 2 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [1, "foo"];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 6 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [[]];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 2 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 3 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- 4 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php ["foo", ["bar", "baz"]];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 10 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- 11 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php (array) [1, 2];',
- [
- 3 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 8 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [1,2][$x];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 5 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php $a[] = []?>',
- [
- 7 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 8 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php $b = [1];',
- [
- 5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 7 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php $c[] = 2?>',
- ];
- yield [
- '<?php $d[3] = 4;',
- ];
- yield [
- '<?php $e = [];',
- [
- 5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 6 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php array();',
- ];
- yield [
- '<?php $x[] = 1;',
- ];
- yield [
- '<?php $x[1];',
- ];
- yield [
- '<?php $x [ 1 ];',
- ];
- yield [
- '<?php ${"x"}[1];',
- ];
- yield [
- '<?php FOO[1];',
- ];
- yield [
- '<?php array("foo")[1];',
- ];
- yield [
- '<?php foo()[1];',
- ];
- yield [
- '<?php "foo"[1];//[]',
- ];
- yield [
- '<?php
- class Test
- {
- public function updateAttributeKey($key, $value)
- {
- $this->{camel_case($attributes)}[$key] = $value;
- }
- }',
- ];
- yield [
- '<?php [$a, $b, $c] = [1, 2, 3];',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 9 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 13 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 21 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php ["a" => $a, "b" => $b, "c" => $c] = $array;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 21 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$e] = $d; if ($a){}[$a, $b] = b();',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 3 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 22 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php $a = [$x] = [$y] = [$z] = [];', // this sample makes no sense, however is in valid syntax
- [
- 5 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 11 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 19 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 23 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 24 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$$a, $b] = $array;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$a, $b,, [$c, $d]] = $a;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 9 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 14 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 15 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield 'nested I' => [
- '<?php [$a[]] = $b;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 5 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield 'nested II (with array offset)' => [
- '<?php [$a[1]] = $b;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 6 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield 'nested III' => [
- '<?php [$a[1], [$b], $c[2]] = $d;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 8 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 10 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [[[$a]/**/], $b[1], [/**/[$c]] /** */ ] = $d[1][2][3];',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 2 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 3 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 5 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 16 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 18 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 20 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 21 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 25 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php foreach ($z as [$a, $b]) {}',
- [
- 8 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php foreach ($a as $key => [$x, $y]) {}',
- [
- 12 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$key => [$x, $y]];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 6 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 11 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- 12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php array($key => [$x, $y]);',
- [
- 7 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$key => [$x, $y] = foo()];',
- [
- 1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
- 6 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 11 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 18 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
- ],
- ];
- }
- /**
- * @param _TransformerTestExpectedTokens $expectedTokens
- *
- * @dataProvider provideProcess72Cases
- */
- public function testProcess72(string $source, array $expectedTokens): void
- {
- $this->doTest(
- $source,
- $expectedTokens,
- [
- CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ]
- );
- }
- public static function provideProcess72Cases(): iterable
- {
- yield [
- '<?php [&$a, $b] = $a;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [$a, &$b] = $a;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [&$a, &$b] = $a;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 8 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- yield [
- '<?php [[ [&$a, &$b], [&$c] ], [&$d/* */]] = $e;',
- [
- 1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 2 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 4 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 11 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 14 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 19 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 22 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
- 26 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- 27 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
- ],
- ];
- }
- }
|