1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- <?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\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\WhitespacesFixerConfig;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Whitespace\StatementIndentationFixer
- */
- final class StatementIndentationFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield 'no brace block' => [
- '<?php
- foo();
- bar();',
- '<?php
- foo();
- bar();',
- ];
- yield 'simple' => [
- '<?php
- if ($foo) {
- foo();
- bar();
- }',
- '<?php
- if ($foo) {
- foo();
- bar();
- }',
- ];
- yield 'braces on same line as code' => [
- '<?php
- if ($foo) {
- foo();
- if ($bar) { bar(); }
- }',
- '<?php
- if ($foo) {
- foo();
- if ($bar) { bar(); }
- }',
- ];
- yield 'with several closing braces on same line' => [
- '<?php
- if ($foo) { foo();
- if ($bar) { bar();
- if ($baz) { baz(); }}
- foo();
- }
- foo();',
- '<?php
- if ($foo) { foo();
- if ($bar) { bar();
- if ($baz) { baz(); }}
- foo();
- }
- foo();',
- ];
- yield 'with meaningful content on closing line' => [
- '<?php
- if ($foo) {
- foo(); }
- foo();',
- '<?php
- if ($foo) {
- foo(); }
- foo();',
- ];
- // @TODO brace at line 6 should have one level of indentation
- yield 'with several opening braces on same line' => [
- '<?php
- if ($foo) { if ($foo) { foo();
- if ($bar) { if ($bar) { bar(); }
- baz();
- }
- }
- baz();
- }
- baz();',
- '<?php
- if ($foo) { if ($foo) { foo();
- if ($bar) { if ($bar) { bar(); }
- baz();
- }
- }
- baz();
- }
- baz();',
- ];
- yield 'function definition arguments' => [
- '<?php
- function foo(
- $bar,
- $baz
- ) {
- }',
- '<?php
- function foo(
- $bar,
- $baz
- ) {
- }',
- ];
- yield 'anonymous function definition arguments' => [
- '<?php
- $foo = function(
- $bar,
- $baz
- ) {
- };',
- '<?php
- $foo = function(
- $bar,
- $baz
- ) {
- };',
- ];
- yield 'interface method definition arguments' => [
- '<?php
- interface Foo {
- public function foo(
- $bar,
- $baz
- );
- }',
- '<?php
- interface Foo {
- public function foo(
- $bar,
- $baz
- );
- }',
- ];
- yield 'class method definition arguments' => [
- '<?php
- class Foo {
- public function foo(
- $bar,
- $baz
- ) {
- }
- }',
- '<?php
- class Foo {
- public function foo(
- $bar,
- $baz
- ) {
- }
- }',
- ];
- yield 'multiple class methods with many permutations of visibility modifiers' => [
- '<?php
- abstract class Test {
- final protected function test_final_protected() {}
- static private function test_static_private() {}
- private function test_private() {}
- private static function test_private_static() {}
- abstract public static function test_abstract_public_static();
- abstract static public function test_abstract_static_public();
- abstract public function test_abstract_public();
- protected abstract function test_protected_abstract();
- public abstract function test_public_abstract();
- final static protected function test_final_static_protected() {}
- final private static function test_final_private_static() {}
- public final function test_public_final() {}
- final private function test_final_private() {}
- static final public function test_static_final_public() {}
- protected abstract static function test_protected_abstract_static();
- public static abstract function test_public_static_abstract();
- protected static abstract function test_protected_static_abstract();
- static final function test_static_final() {}
- final static private function test_final_static_private() {}
- static protected abstract function test_static_protected_abstract();
- public abstract static function test_public_abstract_static();
- static final protected function test_static_final_protected() {}
- final public static function test_final_public_static() {}
- static final private function test_static_final_private() {}
- abstract protected function test_abstract_protected();
- abstract static protected function test_abstract_static_protected();
- private static final function test_private_static_final() {}
- final static function test_final_static() {}
- protected static function test_protected_static() {}
- protected function test_protected() {}
- public static function test_public_static() {}
- final function test_final() {}
- abstract protected static function test_abstract_protected_static();
- static protected function test_static_protected() {}
- static abstract function test_static_abstract();
- static abstract protected function test_static_abstract_protected();
- protected final static function test_protected_final_static() {}
- static public final function test_static_public_final() {}
- public final static function test_public_final_static() {}
- abstract static function test_abstract_static();
- public static final function test_public_static_final() {}
- static function test_static() {}
- abstract function test_abstract();
- static protected final function test_static_protected_final() {}
- static private final function test_static_private_final() {}
- private final function test_private_final() {}
- static public abstract function test_static_public_abstract();
- protected static final function test_protected_static_final() {}
- final protected static function test_final_protected_static() {}
- final static public function test_final_static_public() {}
- static public function test_static_public() {}
- function test_() {}
- static abstract public function test_static_abstract_public();
- final public function test_final_public() {}
- private final static function test_private_final_static() {}
- protected final function test_protected_final() {}
- public function test_public() {}
- }',
- '<?php
- abstract class Test {
- final protected function test_final_protected() {}
- static private function test_static_private() {}
- private function test_private() {}
- private static function test_private_static() {}
- abstract public static function test_abstract_public_static();
- abstract static public function test_abstract_static_public();
- abstract public function test_abstract_public();
- protected abstract function test_protected_abstract();
- public abstract function test_public_abstract();
- final static protected function test_final_static_protected() {}
- final private static function test_final_private_static() {}
- public final function test_public_final() {}
- final private function test_final_private() {}
- static final public function test_static_final_public() {}
- protected abstract static function test_protected_abstract_static();
- public static abstract function test_public_static_abstract();
- protected static abstract function test_protected_static_abstract();
- static final function test_static_final() {}
- final static private function test_final_static_private() {}
- static protected abstract function test_static_protected_abstract();
- public abstract static function test_public_abstract_static();
- static final protected function test_static_final_protected() {}
- final public static function test_final_public_static() {}
- static final private function test_static_final_private() {}
- abstract protected function test_abstract_protected();
- abstract static protected function test_abstract_static_protected();
- private static final function test_private_static_final() {}
- final static function test_final_static() {}
- protected static function test_protected_static() {}
- protected function test_protected() {}
- public static function test_public_static() {}
- final function test_final() {}
- abstract protected static function test_abstract_protected_static();
- static protected function test_static_protected() {}
- static abstract function test_static_abstract();
- static abstract protected function test_static_abstract_protected();
- protected final static function test_protected_final_static() {}
- static public final function test_static_public_final() {}
- public final static function test_public_final_static() {}
- abstract static function test_abstract_static();
- public static final function test_public_static_final() {}
- static function test_static() {}
- abstract function test_abstract();
- static protected final function test_static_protected_final() {}
- static private final function test_static_private_final() {}
- private final function test_private_final() {}
- static public abstract function test_static_public_abstract();
- protected static final function test_protected_static_final() {}
- final protected static function test_final_protected_static() {}
- final static public function test_final_static_public() {}
- static public function test_static_public() {}
- function test_() {}
- static abstract public function test_static_abstract_public();
- final public function test_final_public() {}
- private final static function test_private_final_static() {}
- protected final function test_protected_final() {}
- public function test_public() {}
- }',
- ];
- yield 'trait method definition arguments' => [
- '<?php
- trait Foo {
- public function foo(
- $bar,
- $baz
- ) {
- }
- }',
- '<?php
- trait Foo {
- public function foo(
- $bar,
- $baz
- ) {
- }
- }',
- ];
- yield 'function call arguments' => [
- '<?php
- foo(
- $bar,
- $baz
- );',
- '<?php
- foo(
- $bar,
- $baz
- );',
- ];
- yield 'variable function call arguments' => [
- '<?php
- $foo(
- $bar,
- $baz
- );',
- '<?php
- $foo(
- $bar,
- $baz
- );',
- ];
- yield 'chained method calls' => [
- '<?php
- if ($foo) {
- $foo
- ->bar()
- ->baz()
- ;
- }',
- '<?php
- if ($foo) {
- $foo
- ->bar()
- ->baz()
- ;
- }',
- ];
- yield 'nested arrays (long syntax)' => [
- '<?php
- if ($foo) {
- $foo = array(
- $foo,
- $bar
- ->bar()
- ,
- array($baz)
- )
- ;
- }',
- '<?php
- if ($foo) {
- $foo = array(
- $foo,
- $bar
- ->bar()
- ,
- array($baz)
- )
- ;
- }',
- ];
- yield 'nested arrays (short syntax)' => [
- '<?php
- if ($foo) {
- $foo = [
- $foo,
- $bar
- ->bar()
- ,
- [$baz]
- ]
- ;
- }',
- '<?php
- if ($foo) {
- $foo = [
- $foo,
- $bar
- ->bar()
- ,
- [$baz]
- ]
- ;
- }',
- ];
- yield 'array (long syntax) with function call' => [
- '<?php
- if ($foo) {
- $foo = array(
- foo(
- $bar,
- $baz
- )
- )
- ;
- }',
- '<?php
- if ($foo) {
- $foo = array(
- foo(
- $bar,
- $baz
- )
- )
- ;
- }',
- ];
- yield 'array (short syntax) with function call' => [
- '<?php
- if ($foo) {
- $foo = [
- foo(
- $bar,
- $baz
- )
- ]
- ;
- }',
- '<?php
- if ($foo) {
- $foo = [
- foo(
- $bar,
- $baz
- )
- ]
- ;
- }',
- ];
- yield 'array (long syntax) with class instantiation' => [
- '<?php
- if ($foo) {
- $foo = array(
- new Foo(
- $bar,
- $baz
- )
- )
- ;
- }',
- '<?php
- if ($foo) {
- $foo = array(
- new Foo(
- $bar,
- $baz
- )
- )
- ;
- }',
- ];
- yield 'array (short syntax) with class instantiation' => [
- '<?php
- if ($foo) {
- $foo = [
- new Foo(
- $bar,
- $baz
- )
- ]
- ;
- }',
- '<?php
- if ($foo) {
- $foo = [
- new Foo(
- $bar,
- $baz
- )
- ]
- ;
- }',
- ];
- yield 'implements list' => [
- '<?php
- class Foo implements
- Bar,
- Baz
- {}',
- '<?php
- class Foo implements
- Bar,
- Baz
- {}',
- ];
- yield 'extends list' => [
- '<?php
- interface Foo extends
- Bar,
- Baz
- {}',
- '<?php
- interface Foo extends
- Bar,
- Baz
- {}',
- ];
- yield 'use list' => [
- '<?php
- class Foo {
- use Bar,
- Baz;
- }',
- '<?php
- class Foo {
- use Bar,
- Baz;
- }',
- ];
- yield 'chained method call with argument' => [
- '<?php
- $foo
- ->bar(
- $baz
- );',
- '<?php
- $foo
- ->bar(
- $baz
- );',
- ];
- yield 'argument separator on its own line' => [
- '<?php
- foo(
- 1
- ,
- 2
- );',
- '<?php
- foo(
- 1
- ,
- 2
- );',
- ];
- yield 'statement end on its own line' => [
- '<?php
- if (true) {
- $foo =
- $a
- && $b
- ;
- }',
- '<?php
- if (true) {
- $foo =
- $a
- && $b
- ;
- }',
- ];
- yield 'multiline control structure conditions' => [
- '<?php
- if ($a
- && $b) {
- foo();
- }',
- '<?php
- if ($a
- && $b) {
- foo();
- }',
- ];
- yield 'switch' => [
- '<?php
- switch ($foo) {
- case 1:
- echo "foo";
- break;
- case 2:
- echo "bar";
- break;
- case 3:
- default:
- echo "baz";
- }',
- '<?php
- switch ($foo) {
- case 1:
- echo "foo";
- break;
- case 2:
- echo "bar";
- break;
- case 3:
- default:
- echo "baz";
- }',
- ];
- yield 'array (long syntax) with anonymous class' => [
- '<?php
- if ($foo) {
- $foo = array(
- new class (
- $bar,
- $baz
- ) {
- private $foo;
- public function foo(
- $foo
- ) {
- return $foo;
- }
- }
- )
- ;
- }',
- '<?php
- if ($foo) {
- $foo = array(
- new class (
- $bar,
- $baz
- ) {
- private $foo;
- public function foo(
- $foo
- ) {
- return $foo;
- }
- }
- )
- ;
- }',
- ];
- yield 'array (short syntax) with anonymous class' => [
- '<?php
- if ($foo) {
- $foo = [
- new class (
- $bar,
- $baz
- ) {
- private $foo;
- public function foo(
- $foo
- ) {
- return $foo;
- }
- }
- ]
- ;
- }',
- '<?php
- if ($foo) {
- $foo = [
- new class (
- $bar,
- $baz
- ) {
- private $foo;
- public function foo(
- $foo
- ) {
- return $foo;
- }
- }
- ]
- ;
- }',
- ];
- yield 'expression function call arguments' => [
- '<?php
- (\'foo\')(
- $bar,
- $baz
- );',
- '<?php
- (\'foo\')(
- $bar,
- $baz
- );',
- ];
- yield 'arrow function definition arguments' => [
- '<?php
- $foo = fn(
- $bar,
- $baz
- ) => null;',
- '<?php
- $foo = fn(
- $bar,
- $baz
- ) => null;',
- ];
- yield 'multiline list in foreach' => [
- '<?php
- foreach ($array as [
- "foo" => $foo,
- "bar" => $bar,
- ]) {
- }',
- ];
- yield 'switch case with control structure' => [
- '<?php
- switch ($foo) {
- case true:
- if ($bar) {
- bar();
- }
- return true;
- }',
- '<?php
- switch ($foo) {
- case true:
- if ($bar) {
- bar();
- }
- return true;
- }',
- ];
- yield 'comment in method calls chain' => [
- '<?php
- $foo
- ->baz()
- /* ->baz() */
- ;',
- ];
- yield 'if with only a comment and followed by else' => [
- '<?php
- if (true) {
- // foo
- } else {
- // bar
- }',
- ];
- yield 'multiple anonymous functions as function arguments' => [
- '<?php
- foo(function () {
- bar();
- }, function () {
- baz();
- });',
- ];
- yield 'multiple anonymous functions as method arguments' => [
- '<?php
- $this
- ->bar(function ($a) {
- echo $a;
- }, function ($b) {
- echo $b;
- })
- ;',
- ];
- yield 'semicolon on a newline inside a switch case without break statement' => [
- '<?php
- switch (true) {
- case $foo:
- $foo
- ->baz()
- ;
- }',
- ];
- yield 'alternative syntax' => [
- '<?php if (1): ?>
- <div></div>
- <?php else: ?>
- <?php if (2): ?>
- <div></div>
- <?php else: ?>
- <div></div>
- <?php endif; ?>
- <?php endif; ?>
- ',
- ];
- yield 'trait import with conflict resolution' => [
- '<?php
- class Foo {
- use Bar,
- Baz {
- Baz::baz insteadof Bar;
- }
- }',
- '<?php
- class Foo {
- use Bar,
- Baz {
- Baz::baz insteadof Bar;
- }
- }',
- ];
- yield 'multiline class definition' => [
- '<?php
- class Foo
- extends
- BaseFoo
- implements Bar,
- Baz {
- public function foo() {
- }
- }',
- '<?php
- class Foo
- extends
- BaseFoo
- implements Bar,
- Baz {
- public function foo() {
- }
- }',
- ];
- yield 'comment at end of switch case' => [
- '<?php
- switch ($foo) {
- case 1:
- // Nothing to do
- }',
- ];
- yield 'comment at end of switch default' => [
- '<?php
- switch ($foo) {
- case 1:
- break;
- case 2:
- break;
- default:
- // Nothing to do
- }',
- ];
- yield 'switch ending with empty case' => [
- '<?php
- switch ($foo) {
- case 1:
- }',
- ];
- yield 'switch ending with empty default' => [
- '<?php
- switch ($foo) {
- default:
- }',
- ];
- yield 'function ending with a comment and followed by a comma' => [
- '<?php
- foo(function () {
- bar();
- // comment
- }, );',
- ];
- yield 'multiline arguments starting with "new" keyword' => [
- '<?php
- $result1 = foo(
- new Bar1(),
- 1
- );
- $result2 = ($function)(
- new Bar2(),
- 2
- );
- $result3 = (new Argument())(
- new Bar3(),
- 3
- );',
- ];
- yield 'comments at the end of if/elseif/else blocks' => [
- '<?php
- if ($foo) {
- echo "foo";
- // foo
- } elseif ($bar) {
- echo "bar";
- // bar
- } else {
- echo "baz";
- // baz
- }',
- ];
- }
- /**
- * @dataProvider provideFixWithTabsCases
- */
- public function testFixWithTabs(string $expected, ?string $input = null): void
- {
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t"));
- $this->doTest($expected, $input);
- }
- public static function provideFixWithTabsCases(): iterable
- {
- yield 'simple' => [
- "<?php
- if (\$foo) {
- \tfoo();
- \tbar();
- }",
- '<?php
- if ($foo) {
- foo();
- bar();
- }',
- ];
- }
- /**
- * @dataProvider provideFixPhp80Cases
- *
- * @requires PHP 8.0
- */
- public function testFixPhp80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixPhp80Cases(): iterable
- {
- yield 'match expression' => [
- '<?php
- return match ($bool) {
- 0 => false,
- 1 => true,
- default => throw new Exception(),
- };',
- '<?php
- return match ($bool) {
- 0 => false,
- 1 => true,
- default => throw new Exception(),
- };',
- ];
- yield 'attribute' => [
- '<?php
- class Foo {
- #[SimpleAttribute]
- #[
- MultilineAttribute
- ]
- #[ComplexAttribute(
- foo: true,
- bar: [
- 1,
- 2,
- 3,
- ]
- )]
- public function bar()
- {
- }
- }',
- '<?php
- class Foo {
- #[SimpleAttribute]
- #[
- MultilineAttribute
- ]
- #[ComplexAttribute(
- foo: true,
- bar: [
- 1,
- 2,
- 3,
- ]
- )]
- public function bar()
- {
- }
- }',
- ];
- }
- /**
- * @dataProvider provideFixPhp81Cases
- *
- * @requires PHP 8.1
- */
- public function testFixPhp81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- public static function provideFixPhp81Cases(): iterable
- {
- yield 'simple enum' => [
- '<?php
- enum Color {
- case Red;
- case Green;
- case Blue;
- }',
- '<?php
- enum Color {
- case Red;
- case Green;
- case Blue;
- }',
- ];
- yield 'backend enum' => [
- '<?php
- enum Color: string {
- case Red = "R";
- case Green = "G";
- case Blue = "B";
- }',
- '<?php
- enum Color: string {
- case Red = "R";
- case Green = "G";
- case Blue = "B";
- }',
- ];
- yield 'enum with method' => [
- '<?php
- enum Color {
- case Red;
- case Green;
- case Blue;
- public function foo() {
- return true;
- }
- }',
- '<?php
- enum Color {
- case Red;
- case Green;
- case Blue;
- public function foo() {
- return true;
- }
- }',
- ];
- }
- }
|