123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- <?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\FunctionNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer>
- */
- final class SingleLineThrowFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixCases(): iterable
- {
- yield ['<?php throw new Exception; foo(
- "Foo"
- );'];
- yield ['<?php throw new $exceptionName; foo(
- "Foo"
- );'];
- yield ['<?php throw $exception; foo(
- "Foo"
- );'];
- yield ['<?php throw new Exception("Foo.", 0);'];
- yield [
- '<?php throw new Exception("Foo.", 0);',
- '<?php throw new Exception(
- "Foo.",
- 0
- );',
- ];
- yield [
- '<?php throw new Exception("Foo." . "Bar");',
- '<?php throw new Exception(
- "Foo."
- .
- "Bar"
- );',
- ];
- yield [
- '<?php throw new Exception(new ExceptionReport("Foo"), 0);',
- '<?php throw new Exception(
- new
- ExceptionReport("Foo"),
- 0
- );',
- ];
- yield [
- '<?php throw new Exception(sprintf(\'Error with number "%s".\', 42));',
- '<?php throw new Exception(sprintf(
- \'Error with number "%s".\',
- 42
- ));',
- ];
- yield [
- '<?php throw new SomeVendor\Exception("Foo.");',
- '<?php throw new SomeVendor\Exception(
- "Foo."
- );',
- ];
- yield [
- '<?php throw new \SomeVendor\Exception("Foo.");',
- '<?php throw new \SomeVendor\Exception(
- "Foo."
- );',
- ];
- yield [
- '<?php throw $this->exceptionFactory->createAnException("Foo");',
- '<?php throw $this
- ->exceptionFactory
- ->createAnException(
- "Foo"
- );',
- ];
- yield [
- '<?php throw $this->getExceptionFactory()->createAnException("Foo");',
- '<?php throw $this
- ->getExceptionFactory()
- ->createAnException(
- "Foo"
- );',
- ];
- yield [
- '<?php throw $this->getExceptionFactory()->createAnException(function ($x, $y) { return $x === $y + 2; });',
- '<?php throw $this
- ->getExceptionFactory()
- ->createAnException(
- function
- (
- $x,
- $y
- )
- {
- return $x === $y + 2
- ;
- }
- );',
- ];
- yield [
- '<?php throw ExceptionFactory::createAnException("Foo");',
- '<?php throw ExceptionFactory
- ::
- createAnException(
- "Foo"
- );',
- ];
- yield [
- '<?php throw new Exception("Foo.", 0);',
- '<?php throw
- new
- Exception
- (
- "Foo."
- ,
- 0
- );',
- ];
- yield [
- '<?php throw new $exceptionName("Foo.");',
- '<?php throw new $exceptionName(
- "Foo."
- );',
- ];
- yield [
- '<?php throw new $exceptions[4];',
- '<?php throw new $exceptions[
- 4
- ];',
- ];
- yield [
- '<?php throw clone $exceptionName("Foo.");',
- '<?php throw clone $exceptionName(
- "Foo."
- );',
- ];
- yield [
- '<?php throw new WeirdException("Foo.", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
- '<?php throw new WeirdException("Foo.", -20, "An elephant",
- 1,
- 2,
- 3, 4, 5, 6, 7, 8
- );',
- ];
- yield [
- '<?php
- if ($foo) {
- throw new Exception("It is foo.", 1);
- } else {
- throw new \Exception("It is not foo.", 0);
- }
- ',
- '<?php
- if ($foo) {
- throw new Exception(
- "It is foo.",
- 1
- );
- } else {
- throw new \Exception(
- "It is not foo.", 0
- );
- }
- ',
- ];
- yield [
- '<?php throw new Exception( /* A */"Foo", /* 1 */0 /* 2 */); //3',
- '<?php throw new Exception( // A
- "Foo", // 1
- 0 // 2
- ); //3',
- ];
- yield [
- '<?php throw new Exception( /* 0123 */ "Foo", /* 1 */0 /* 2 */); //3',
- '<?php throw new Exception( /* 0123 */
- "Foo", // 1
- 0 // 2
- ); //3',
- ];
- yield [
- '<?php throw new Exception( /* X */ "Foo", /* 1 */0 /* 2 */); //3',
- '<?php throw new Exception( /* X
- */
- "Foo", // 1
- 0 // 2
- ); //3',
- ];
- yield [
- '<?php throw new Exception( 0, /* 1 2 3 */ /*4*/ "Foo", /*5*/ /*6*/0 /*7*/);',
- '<?php throw new Exception( 0, /*
- 1
- 2
- 3
- */
- /*4*/ "Foo", /*5*/
- /*6*/0 /*7*/);',
- ];
- yield [
- '<?php throw new Exception( /* 0 */${"Foo" /* a */}, /*b */fnx/*c */(/*d */0/*e */)/*f */);',
- '<?php throw new Exception( // 0
- ${"Foo"
- # a
- }, #b
- fnx#c
- (#d
- 0#e
- )#f
- );',
- ];
- yield [
- "<?php throw new Exception('Message.'. 1);",
- "<?php throw new Exception('Message.'.
- 1
- );",
- ];
- yield [
- '<?php throw new class() extends Exception
- {
- protected $message = "Custom message";
- }
- ;',
- '<?php throw
- new class()
- extends Exception
- {
- protected $message = "Custom message";
- }
- ;',
- ];
- yield [
- '<?php throw new class extends Exception
- {
- protected $message = "Custom message";
- }
- ;',
- '<?php throw
- new class
- extends Exception
- {
- protected $message = "Custom message";
- }
- ;',
- ];
- yield [
- '<?php throw new Exception("Foo.", 0)?>',
- '<?php throw new Exception(
- "Foo.",
- 0
- )?>',
- ];
- }
- /**
- * @dataProvider provideFix80Cases
- *
- * @requires PHP 8.0
- */
- public function testFix80(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFix80Cases(): iterable
- {
- yield [
- '<?php throw $this?->getExceptionFactory()?->createAnException("Foo");',
- '<?php throw $this
- ?->getExceptionFactory()
- ?->createAnException(
- "Foo"
- );',
- ];
- yield [
- '<?php
- match ($number) {
- 1 => $function->one(),
- 2 => $function->two(),
- default => throw new \NotOneOrTwo()
- };
- ',
- ];
- yield [
- '<?php
- match ($number) {
- 1 => $function->one(),
- 2 => throw new Exception("Number 2 is not allowed."),
- 1 => $function->three(),
- default => throw new \NotOneOrTwo()
- };
- ',
- ];
- yield [
- '<?php throw new Exception(match ($a) { 1 => "a", 3 => "b" });',
- '<?php throw new Exception(match ($a) {
- 1 => "a",
- 3 => "b"
- });',
- ];
- yield [
- '<?php
- $var = [
- $something[1] ?? throw new Exception(123)
- ];
- ',
- '<?php
- $var = [
- $something[1] ?? throw new Exception(
- 123
- )
- ];
- ',
- ];
- yield [
- '<?php
- $var = [
- $something[1] ?? throw new Exception()
- ];
- ',
- ];
- }
- }
|