123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- <?php
- declare(strict_types=1);
- namespace PhpCsFixer\Tests\Fixer\Whitespace;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\WhitespacesFixerConfig;
- final class NoExtraBlankLinesFixerTest extends AbstractFixerTestCase
- {
- private string $template = <<<'EOF'
- <?php
- use \DateTime;
- use \stdClass;
- use \InvalidArgumentException;
- class Test {
- public function testThrow($a)
- {
- if ($a) {
- throw new InvalidArgumentException('test.'); // test
- }
- $date = new DateTime();
- $class = new stdClass();
- $class = (string) $class;
- $e = new InvalidArgumentException($class.$date->format('Y'));
- throw $e;
- }
- public function testBreak($a)
- {
- switch($a) {
- case 1:
- echo $a;
- break;
- case 2:
- echo 'test';
- break;
- }
- }
- protected static function testContinueAndReturn($a, $b)
- {
- while($a < 100) {
- if ($b < time()) {
- continue;
- }
- return $b;
- }
- return $a;
- }
- private function test(){
- // comment
- }
- private function test123(){
- // comment
- }
- }
- EOF;
- /**
- * @param list<int> $lineNumberRemoved Line numbers expected to be removed after fixing
- * @param _AutogeneratedInputConfiguration['tokens'] $configTokens
- *
- * @dataProvider provideWithConfigCases
- */
- public function testWithConfig(array $lineNumberRemoved, array $configTokens): void
- {
- $this->fixer->configure(['tokens' => $configTokens]);
- $this->doTest($this->removeLinesFromString($this->template, $lineNumberRemoved), $this->template);
- }
- public static function provideWithConfigCases(): iterable
- {
- $tests = [
- [
- [9, 14, 21, 43, 45, 49, 53, 57],
- ['curly_brace_block'],
- ],
- [
- [3, 5],
- ['use'],
- ],
- [
- [23, 24],
- ['extra'],
- ],
- [
- [49, 53],
- ['return'],
- ],
- [
- [45],
- ['continue'],
- ],
- [
- [32],
- ['break'],
- ],
- [
- [14, 21],
- ['throw'],
- ],
- ];
- yield from $tests;
- $all = [[], []];
- foreach ($tests as $test) {
- $all[0] = array_merge($test[0], $all[0]);
- $all[1] = array_merge($test[1], $all[1]);
- }
- yield $all;
- }
- public function testFix(): void
- {
- $expected = <<<'EOF'
- <?php
- $a = new Bar();
- $a = new FooBaz();
- EOF;
- $input = <<<'EOF'
- <?php
- $a = new Bar();
- $a = new FooBaz();
- EOF;
- $this->doTest($expected, $input);
- }
- public function testFixWithManyEmptyLines(): void
- {
- $expected = <<<'EOF'
- <?php
- $a = new Bar();
- $a = new FooBaz();
- EOF;
- $input = <<<'EOF'
- <?php
- $a = new Bar();
- $a = new FooBaz();
- EOF;
- $this->doTest($expected, $input);
- }
- public function testFixWithHeredoc(): void
- {
- $expected = '
- <?php
- $b = <<<TEXT
- Foo TEXT
- Bar
- FooFoo
- TEXT;
- ';
- $this->doTest($expected);
- }
- public function testFixWithNowdoc(): void
- {
- $expected = '
- <?php
- $b = <<<\'TEXT\'
- Foo TEXT;
- Bar1}
- FooFoo
- TEXT;
- ';
- $this->doTest($expected);
- }
- public function testFixWithEncapsulatedNowdoc(): void
- {
- $expected = '
- <?php
- $b = <<<\'TEXT\'
- Foo TEXT
- Bar
- <<<\'TEMPLATE\'
- BarFooBar TEMPLATE
- TEMPLATE;
- FooFoo
- TEXT;
- ';
- $this->doTest($expected);
- }
- public function testFixWithMultilineString(): void
- {
- $expected = <<<'EOF'
- <?php
- $a = 'Foo
- Bar';
- EOF;
- $this->doTest($expected);
- }
- public function testFixWithTrickyMultilineStrings(): void
- {
- $expected = <<<'EOF'
- <?php
- $a = 'Foo';
- $b = 'Bar
- Here\'s an escaped quote '
- .
- '
- FooFoo';
- EOF;
- $input = <<<'EOF'
- <?php
- $a = 'Foo';
- $b = 'Bar
- Here\'s an escaped quote '
- .
- '
- FooFoo';
- EOF;
- $this->doTest($expected, $input);
- }
- public function testFixWithCommentWithQuote(): void
- {
- $expected = <<<'EOF'
- <?php
- $a = 'foo';
- // my comment's must have a quote
- $b = 'foobar';
- $c = 'bar';
- EOF;
- $input = <<<'EOF'
- <?php
- $a = 'foo';
- // my comment's must have a quote
- $b = 'foobar';
- $c = 'bar';
- EOF;
- $this->doTest($expected, $input);
- }
- public function testFixWithTrailingInlineBlock(): void
- {
- $expected = "
- <?php
- echo 'hello';
- ?>
- \$a = 0;
- //a
- <?php
- \$a = 0;
- \$b = 1;
- //a
- ?>
- ";
- $this->doTest($expected);
- }
- /**
- * @dataProvider provideFixWithCommentsCases
- */
- public function testFixWithComments(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixWithCommentsCases(): iterable
- {
- yield [
- <<<'EOF'
- <?php
- //class Test
- $a; //
- $b;
- /***/
- $c;
- //
- $d;
- EOF,
- <<<'EOF'
- <?php
- //class Test
- $a; //
- $b;
- /***/
- $c;
- //
- $d;
- EOF,
- ];
- yield [
- "<?php\n//a\n\n\$a =1;",
- "<?php\n//a\n\n\n\n\$a =1;",
- ];
- }
- /**
- * @dataProvider provideFixWithLineBreaksCases
- */
- public function testFixWithLineBreaks(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixWithLineBreaksCases(): iterable
- {
- $input = '<?php //
- $a = 1;
- $b = 1;
- ';
- $expected = '<?php //
- $a = 1;
- $b = 1;
- ';
- yield [
- "<?php\r\n//a\r\n\r\n\$a =1;",
- "<?php\r\n//a\r\n\r\n\r\n\r\n\$a =1;",
- ];
- yield [
- $expected,
- $input,
- ];
- yield [
- str_replace("\n", "\r\n", $expected),
- str_replace("\n", "\r\n", $input),
- ];
- yield [
- str_replace("\n", "\r", $input),
- ];
- yield [
- str_replace("\n", "\r", $expected),
- ];
- }
- public function testWrongConfig(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('/^\[no_extra_blank_lines\] Invalid configuration: The option "tokens" .*\.$/');
- $this->fixer->configure(['tokens' => ['__TEST__']]); // @phpstan-ignore-line
- }
- /**
- * @dataProvider provideBetweenUseCases
- */
- public function testBetweenUse(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['tokens' => ['use']]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideBetweenUseCases(): iterable
- {
- yield ['<?php use A\B;'];
- yield ['<?php use A\B?>'];
- yield ['<?php use A\B;use A\D; return 1;'];
- yield ["<?php use A\\B?>\n\n<?php use D\\E\\F?>"];
- yield ['<?php use Y\B;use A\D; return 1;'];
- yield [
- '<?php
- use A\B;
- use A\C;',
- '<?php
- use A\B;
- use A\C;',
- ];
- yield [
- '<?php use A\E;use A\Z;
- use C;
- return 1;
- ',
- '<?php use A\E;use A\Z;
- use C;
- return 1;
- ',
- ];
- yield [
- '<?php
- class Test {
- use A;
- use B;
- }',
- ];
- yield [
- '<?php
- $example = function () use ($message) { var_dump($message); };
- $example = function () use ($message) { var_dump($message); };
- ',
- ];
- yield [
- '<?php
- use function A; use function B;
- echo 1;',
- ];
- yield [
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\a\{ConstA,ConstB,ConstC
- ,
- };
- use const some\Z\{ConstX,ConstY,ConstZ,};
- ',
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C,};
- use function some\a\{fn_a, fn_b, fn_c,};
- use const some\a\{ConstA,ConstB,ConstC
- ,
- };
- '.'
- use const some\Z\{ConstX,ConstY,ConstZ,};
- ',
- ];
- }
- /**
- * @dataProvider provideRemoveLinesBetweenUseStatementsCases
- */
- public function testRemoveLinesBetweenUseStatements(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['tokens' => ['use']]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideRemoveLinesBetweenUseStatementsCases(): iterable
- {
- yield [
- <<<'EOF'
- <?php
- use Zxy\Qux;
- use Zoo\Bar as Bar2;
- use Foo\Bar as Bar1;
- use Foo\Zar\Baz;
- $c = 1;
- use Foo\Quxx as Quxx1;
- use Foo\Zar\Quxx;
- $a = new Bar1();
- $a = new Bar2();
- $a = new Baz();
- $a = new Qux();
- EOF,
- <<<'EOF'
- <?php
- use Zxy\Qux;
- use Zoo\Bar as Bar2;
- use Foo\Bar as Bar1;
- use Foo\Zar\Baz;
- $c = 1;
- use Foo\Quxx as Quxx1;
- use Foo\Zar\Quxx;
- $a = new Bar1();
- $a = new Bar2();
- $a = new Baz();
- $a = new Qux();
- EOF,
- ];
- yield [
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C};
- use function some\a\{fn_a, fn_b, fn_c};
- use const some\a\{ConstA, ConstB, ConstC};
- ',
- '<?php
- use some\a\{ClassA, ClassB, ClassC as C};
- use function some\a\{fn_a, fn_b, fn_c};
- use const some\a\{ConstA, ConstB, ConstC};
- ',
- ];
- }
- /**
- * @dataProvider provideWithoutUsesCases
- */
- public function testWithoutUses(string $expected): void
- {
- $this->fixer->configure(['tokens' => ['use']]);
- $this->doTest($expected);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideWithoutUsesCases(): iterable
- {
- yield [
- '<?php
- $c = 1;
- $a = new Baz();
- $a = new Qux();',
- ];
- yield [
- '<?php use A\B;',
- ];
- yield [
- '<?php use A\B?>',
- ];
- yield [
- '<?php use A\B;?>',
- ];
- }
- /**
- * @dataProvider provideRemoveBetweenUseTraitsCases
- *
- * @group legacy
- */
- public function testRemoveBetweenUseTraits(string $expected, string $input): void
- {
- $this->expectDeprecation('Option "tokens: use_trait" used in `no_extra_blank_lines` rule is deprecated, use the rule `class_attributes_separation` with `elements: trait_import` instead.');
- $this->fixer->configure(['tokens' => ['use_trait']]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideRemoveBetweenUseTraitsCases(): iterable
- {
- yield [
- '<?php
- class Foo
- {
- use Z; // 123
- use Bar;/* */use Baz;
- public function baz() {}
- use Bar1; use Baz1;
- public function baz1() {}
- }
- ',
- '<?php
- class Foo
- {
- use Z; // 123
- use Bar;/* */use Baz;
- public function baz() {}
- use Bar1; use Baz1;
- public function baz1() {}
- }
- ',
- ];
- yield [
- '<?php
- class Foo
- {
- use Bar;use Baz;
- use Bar1;use Baz1;
- public function baz() {}
- }
- ',
- '<?php
- class Foo
- {
- use Bar;use Baz;
- use Bar1;use Baz1;
- public function baz() {}
- }
- ',
- ];
- yield [
- '<?php
- namespace T\A;
- use V;
- use W;
- class Test {
- use A;
- use B;
- private function test($b) {
- $a = function() use ($b) { echo $b;};
- $b = function() use ($b) { echo $b;};
- }
- }',
- '<?php
- namespace T\A;
- use V;
- use W;
- class Test {
- use A;
- use B;
- private function test($b) {
- $a = function() use ($b) { echo $b;};
- $b = function() use ($b) { echo $b;};
- }
- }',
- ];
- }
- /**
- * @dataProvider provideOneOrInLineCases
- */
- public function testOneOrInLine(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['tokens' => [
- 'break',
- 'continue',
- 'return',
- 'throw',
- 'curly_brace_block',
- 'square_brace_block',
- 'parenthesis_brace_block',
- ]]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideOneOrInLineCases(): iterable
- {
- yield [
- "<?php\n\n\$a = function() use (\$b) { while(3<1)break; \$c = \$b[1]; while(\$b<1)continue; if (true) throw \$e; return 1; };\n\n",
- ];
- yield [
- "<?php throw new \\Exception('do not import.');\n",
- "<?php throw new \\Exception('do not import.');\n\n",
- ];
- yield [
- "<?php\n\n\$a = \$b[0];\n\n",
- ];
- yield [
- "<?php\n\n\$a->{'Test'};\nfunction test(){}\n",
- ];
- yield [
- "<?php\n\n\$a = new class { public function a () { while(4<1)break; while(3<1)continue; if (true) throw \$e; return 1; }};\n\n",
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideBracesCases
- */
- public function testBraces(array $config, string $expected, ?string $input = null): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideBracesCases(): iterable
- {
- yield [
- ['tokens' => ['curly_brace_block']],
- "<?php function test()\n\n{}\n\necho 789;",
- ];
- yield [
- ['tokens' => ['curly_brace_block']],
- "<?php switch(\$a){\ncase 1:echo 789;}",
- "<?php switch(\$a){\n \ncase 1:echo 789;}",
- ];
- yield [
- ['tokens' => ['parenthesis_brace_block']],
- '<?php
- is_int(
- 1);
- function test(
- $a,
- $b,
- $c
- )
- {
- }',
- '<?php
- is_int(
- 1);
- function test(
- $a,
- $b,
- $c
- )
- {
- }',
- ];
- yield [
- ['tokens' => ['parenthesis_brace_block']],
- "<?php array(\n1,\n2,\n3,\n);",
- "<?php array(\n \n1,\n2,\n3,\n\n\n);",
- ];
- yield [
- ['tokens' => ['parenthesis_brace_block']],
- '<?php
- function a()
- {
- $b->d(e(
- ));
- foreach ($a as $x) {
- }
- }',
- ];
- yield [
- ['tokens' => ['return']],
- '<?php
- class Foo
- {
- public function bar() {return 1;}
- public function baz() {return 2;
- }
- }',
- '<?php
- class Foo
- {
- public function bar() {return 1;}
- public function baz() {return 2;
- }
- }',
- ];
- yield [
- ['tokens' => ['square_brace_block']],
- "<?php \$c = \$b[0];\n\n\n\$a = [\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
- "<?php \$c = \$b[0];\n\n\n\$a = [\n\n 1,\n2];\necho 1;\n\$b = [];\n\n\n//a\n",
- ];
- }
- /**
- * @dataProvider provideFixPre80Cases
- *
- * @requires PHP <8.0
- */
- public function testFixPre80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, 1?: string}>
- */
- public static function provideFixPre80Cases(): iterable
- {
- yield [
- "<?php\n\n\$a = \$b{0};\n\n",
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideMessyWhitespacesCases
- */
- public function testMessyWhitespaces(array $config, string $expected, ?string $input = null): void
- {
- $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideMessyWhitespacesCases(): iterable
- {
- yield [
- [],
- "<?php\r\nuse AAA;\r\n\r\nuse BBB;\r\n\r\n",
- "<?php\r\nuse AAA;\r\n\r\n\r\n\r\nuse BBB;\r\n\r\n",
- ];
- yield [
- ['tokens' => ['parenthesis_brace_block']],
- "<?php is_int(\r\n1);",
- "<?php is_int(\r\n\r\n\r\n\r\n1);",
- ];
- yield [
- ['tokens' => ['square_brace_block']],
- "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n",
- "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n 1,\r\n2];\r\necho 1;\r\n\$b = [];\r\n\r\n\r\n//a\r\n",
- ];
- yield [
- ['tokens' => ['square_brace_block']],
- "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\t1,\r\n2];",
- "<?php \$c = \$b[0];\r\n\r\n\r\n\$a = [\r\n\r\n\t1,\r\n2];",
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration['tokens'] $configTokens
- *
- * @dataProvider provideInSwitchStatementCases
- */
- public function testInSwitchStatement(array $configTokens, string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['tokens' => $configTokens]);
- $this->doTest($expected, $input);
- }
- public static function provideInSwitchStatementCases(): iterable
- {
- yield [
- [
- 'break',
- 'continue',
- 'extra',
- 'return',
- 'throw',
- ],
- '<?php
- /** a */
- switch ($a) {
- case 1:
- break;
- case 2:
- continue;
- case 3:
- return 1;
- case 4:
- throw $e;
- case 5:
- throw new \Exception();
- case Token::STRING_TYPE:
- echo 123;
- return new ConstantNode($token->getValue());
- case 7:
- return new ConstantNode($token->getValue());
- case 8:
- return 8;
- default:
- echo 1;
- }',
- '<?php
- /** a */
- switch ($a) {
- case 1:
- break;
- case 2:
- continue;
- case 3:
- return 1;
- case 4:
- throw $e;
- case 5:
- throw new \Exception();
- case Token::STRING_TYPE:
- echo 123;
- return new ConstantNode($token->getValue());
- case 7:
- return new ConstantNode($token->getValue());
- '.'
- case 8:
- return 8;
- '.'
- default:
- echo 1;
- }',
- ];
- yield [
- [
- 'switch',
- 'case',
- 'default',
- ],
- '<?php
- switch($a) {
- case 0:
- case 1:
- default:
- return 1;
- }',
- '<?php
- switch($a) {
- case 0:
- case 1:
- default:
- return 1;
- }',
- ];
- yield [
- [
- 'switch',
- 'case',
- 'default',
- ],
- '<?php
- switch($a) { case 2: echo 3;
- default: return 1;}
- // above stays empty',
- '<?php
- switch($a) { case 2: echo 3;
- default: return 1;}
- // above stays empty',
- ];
- }
- public function testRemovingEmptyLinesAfterOpenTag(): void
- {
- $this->doTest(
- '<?php
- class Foo {}',
- '<?php
- class Foo {}'
- );
- }
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(array $config, string $expected, ?string $input = null): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideFix80Cases(): iterable
- {
- yield [
- ['tokens' => ['throw']],
- '<?php
- $nullCoalescingOperator1 = $bar ?? throw new \Exception();
- $nullCoalescingOperator2 = $bar ?? throw new \Exception();
- $nullCoalescingOperator3 = $bar ?? throw new \Exception();
- $ternaryOperator1 = $bar ? 42 : throw new \Exception();
- $ternaryOperator2 = $bar ? 42 : throw new \Exception();
- $ternaryOperator3 = $bar ? 42 : throw new \Exception();
- $orOperator1 = $bar || throw new \Exception();
- $orOperator2 = $bar || throw new \Exception();
- $orOperator3 = $bar || throw new \Exception();
- ',
- ];
- yield [
- ['tokens' => ['throw']],
- '<?php
- $a = $bar ?? throw new \Exception();
- // Now, we are going to use it!
- var_dump($a);
- ',
- ];
- yield [
- ['tokens' => ['attribute']],
- '<?php
- #[Attr]
- #[AttrFoo1]
- #[AttrFoo2]
- function foo(){}
- ',
- '<?php
- #[Attr]
- #[AttrFoo1]
- #[AttrFoo2]
- function foo(){}
- ',
- ];
- yield [
- ['tokens' => ['attribute']],
- '<?php class Foo
- {
- protected function f1(string $x): string { return "r1"; }
- protected function f2(string $x): string { return "r1"; }
- protected function f3(#[Attr] string $x): string { return "r1"; }
- protected function f4(string $x): string { return "r1"; }
- }',
- ];
- yield [
- ['tokens' => ['attribute']],
- '<?php abstract class Foo
- {
- abstract protected function f1(string $x): string;
- abstract protected function f2(string $x): string;
- abstract protected function f3(#[Attr] string $x): string;
- abstract protected function f4(string $x): string;
- }',
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['tokens' => ['case']]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield [
- '<?php
- enum test
- {
- case Baz;
- public function foo() {
- switch (bar()) {
- case 1: echo 1; break;
- case 2: echo 2; break;
- }
- }
- case Bad;
- }
- ',
- '<?php
- enum test
- {
- case Baz;
- public function foo() {
- switch (bar()) {
- case 1: echo 1; break;
- case 2: echo 2; break;
- }
- }
- case Bad;
- }
- ',
- ];
- $expectedTemplate = '<?php
- enum Foo
- {
- case CASE_1;
- %s
- }';
- $enumAttributes = [
- 'case CASE_2;',
- 'const CONST_1 = self::CASE_1;',
- 'private const CONST_1 = self::CASE_1;',
- 'public function bar(): void {}',
- 'protected function bar(): void {}',
- 'private function bar(): void {}',
- 'static function bar(): void {}',
- 'final function bar(): void {}',
- ];
- foreach ($enumAttributes as $enumAttribute) {
- yield [
- \sprintf($expectedTemplate, $enumAttribute),
- ];
- }
- }
- /**
- * @param list<int> $lineNumbers
- */
- private function removeLinesFromString(string $input, array $lineNumbers): string
- {
- sort($lineNumbers);
- $lines = explode("\n", $input);
- foreach ($lineNumbers as $lineNumber) {
- --$lineNumber;
- unset($lines[$lineNumber]);
- }
- return implode("\n", $lines);
- }
- }
|