123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?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\Fixer\Whitespace;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @author Javier Spagnoletti <phansys@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer
- */
- final class NoSpacesAroundOffsetFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixSpaceInsideOffsetCases
- */
- public function testFixSpaceInsideOffset(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideFixSpaceOutsideOffsetCases
- */
- public function testFixSpaceOutsideOffset(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public function testLeaveNewLinesAlone(): void
- {
- $expected = <<<'EOF'
- <?php
- class Foo
- {
- private function bar()
- {
- if ([1, 2, 3] && [
- 'foo',
- 'bar' ,
- 'baz'// a comment just to mix things up
- ]) {
- return 1;
- };
- }
- }
- EOF;
- $this->doTest($expected);
- }
- /**
- * @dataProvider provideCommentsCases
- */
- public function testComments(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideCommentsCases(): iterable
- {
- yield [
- '<?php
- $withComments[0] // here is a comment
- [1] // and here is another
- [2] = 3;',
- ];
- yield [
- '<?php
- $a = $b[# z
- 1#z
- ];',
- '<?php
- $a = $b[ # z
- 1#z
- ];',
- ];
- }
- public function testLeaveComplexString(): void
- {
- $expected = <<<'EOF'
- <?php
- echo "I am printing some spaces here {$foo->bar[1]} {$foo->bar[1]}.";
- EOF;
- $this->doTest($expected);
- }
- public function testLeaveFunctions(): void
- {
- $expected = <<<'EOF'
- <?php
- function someFunc() { $someVar = []; }
- EOF;
- $this->doTest($expected);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixSpaceOutsideOffsetCases(): iterable
- {
- yield [
- '<?php
- $a = $b[0] ;',
- '<?php
- $a = $b [0] ;',
- ];
- yield [
- '<?php
- $a = array($b[0] , $b[0] );',
- '<?php
- $a = array($b [0] , $b [0] );',
- ];
- yield [
- '<?php
- $withComments[0] // here is a comment
- [1] // and here is another
- [2][3] = 4;',
- '<?php
- $withComments [0] // here is a comment
- [1] // and here is another
- [2] [3] = 4;',
- ];
- yield [
- '<?php
- $c = SOME_CONST[0][1][2];',
- '<?php
- $c = SOME_CONST [0] [1] [2];',
- ];
- yield [
- '<?php
- $f = someFunc()[0][1][2];',
- '<?php
- $f = someFunc() [0] [1] [2];',
- ];
- yield [
- '<?php
- $foo[][0][1][2] = 3;',
- '<?php
- $foo [] [0] [1] [2] = 3;',
- ];
- yield [
- '<?php
- $foo[0][1][2] = 3;',
- '<?php
- $foo [0] [1] [2] = 3;',
- ];
- yield [
- '<?php
- $bar = $foo[0][1][2];',
- '<?php
- $bar = $foo [0] [1] [2];',
- ];
- yield [
- '<?php
- $baz[0][1][2] = 3;',
- '<?php
- $baz [0]
- [1]
- [2] = 3;',
- ];
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixSpaceInsideOffsetCases(): iterable
- {
- yield [
- '<?php
- $foo = array(1, 2, 3);
- $var = $foo[1];',
- '<?php
- $foo = array(1, 2, 3);
- $var = $foo[ 1 ];',
- ];
- yield [
- '<?php
- $arr = [2, 2 , ];
- $var = $arr[0];',
- '<?php
- $arr = [2, 2 , ];
- $var = $arr[ 0 ];',
- ];
- yield [
- '<?php
- $arr[2] = 3;',
- '<?php
- $arr[ 2 ] = 3;',
- ];
- yield [
- '<?php
- $arr[] = 3;',
- '<?php
- $arr[ ] = 3;',
- ];
- yield [
- '<?php
- $arr[]["some_offset"][] = 3;',
- '<?php
- $arr[ ][ "some_offset" ][ ] = 3;',
- ];
- yield [
- '<?php
- $arr[]["some offset with spaces"][] = 3;',
- '<?php
- $arr[ ][ "some offset with spaces" ][ ] = 3;',
- ];
- yield [
- '<?php
- $var = $arr[0];',
- '<?php
- $var = $arr[ 0 ];',
- ];
- yield [
- '<?php
- $var = $arr[0][0];',
- '<?php
- $var = $arr[ 0 ][ 0 ];',
- ];
- yield [
- '<?php
- $var = $arr[$a[$b]];',
- '<?php
- $var = $arr[ $a [ $b ] ];',
- ];
- yield [
- '<?php
- $var = $arr[$a[$b]];',
- '<?php
- $var = $arr[ $a [ $b ] ];',
- ];
- yield [
- '<?php
- $var = $arr[0][
- 0];',
- '<?php
- $var = $arr[0][
- 0 ];',
- ];
- yield [
- '<?php
- $var = $arr[0][0
- ];',
- '<?php
- $var = $arr[0][ 0
- ];',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixWithConfigurationCases
- */
- public function testFixWithConfiguration(string $expected, string $input, array $configuration): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string, _AutogeneratedInputConfiguration}>
- */
- public static function provideFixWithConfigurationCases(): iterable
- {
- yield 'Config "default".' => [
- '<?php [ $a ] = $a;
- if ($controllerName = $request->attributes->get(1)) {
- return false;
- }
- [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName);
- $a = $b[0];
- ',
- '<?php [ $a ] = $a;
- if ($controllerName = $request->attributes->get(1)) {
- return false;
- }
- [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName);
- $a = $b [0];
- ',
- ['positions' => ['inside', 'outside']],
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- public static function provideFixPre80Cases(): iterable
- {
- yield [
- '<?php
- $foo{0}{1}{2} = 3;',
- '<?php
- $foo {0} {1} {2} = 3;',
- ];
- yield [
- '<?php
- $foobar = $foo{0}[1]{2};',
- '<?php
- $foobar = $foo {0} [1] {2};',
- ];
- yield [
- '<?php
- $var = $arr[0]{0
- };',
- '<?php
- $var = $arr[0]{ 0
- };',
- ];
- yield from self::provideMultiDimensionalArrayCases();
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input, array $configuration): void
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string, _AutogeneratedInputConfiguration}>
- */
- public static function provideFix80Cases(): iterable
- {
- foreach (self::provideMultiDimensionalArrayCases() as $index => $test) {
- $test[0] = str_replace('{', '[', $test[0]);
- $test[0] = str_replace('}', ']', $test[0]);
- $test[1] = str_replace('{', '[', $test[1]);
- $test[1] = str_replace('}', ']', $test[1]);
- yield $index => $test;
- }
- }
- public function testWrongConfig(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('/^\[no_spaces_around_offset\] Invalid configuration: The option "positions" .*\.$/');
- $this->fixer->configure(['positions' => ['foo']]); // @phpstan-ignore-line
- }
- /**
- * @return iterable<array{string, string, _AutogeneratedInputConfiguration}>
- */
- private static function provideMultiDimensionalArrayCases(): iterable
- {
- yield [
- <<<'EOT'
- <?php
- $arr1[] ["some_offset"] [] {"foo"} = 3;
- EOT,
- <<<'EOT'
- <?php
- $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3;
- EOT,
- ['positions' => ['inside']],
- ];
- yield [
- <<<'EOT'
- <?php
- $arr1[ ][ "some_offset" ][ ]{ "foo" } = 3;
- EOT,
- <<<'EOT'
- <?php
- $arr1[ ] [ "some_offset" ] [ ] { "foo" } = 3;
- EOT,
- ['positions' => ['outside']],
- ];
- }
- }
|