|
@@ -26,16 +26,19 @@ use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
|
|
|
final class NewWithBracesFixerTest extends AbstractFixerTestCase
|
|
|
{
|
|
|
/**
|
|
|
- * @dataProvider provideFixCases
|
|
|
+ * @dataProvider provideNamedWithDefaultConfigurationCases
|
|
|
*/
|
|
|
- public function testFix(string $expected, ?string $input = null): void
|
|
|
+ public function testFixNamedWithDefaultConfiguration(string $expected, ?string $input = null): void
|
|
|
{
|
|
|
$this->doTest($expected, $input);
|
|
|
}
|
|
|
|
|
|
- public function provideFixCases(): \Generator
|
|
|
+ public function provideNamedWithDefaultConfigurationCases(): \Generator
|
|
|
{
|
|
|
yield from [
|
|
|
+ ['<?php $x = new X(foo(/**/));'],
|
|
|
+ ['<?php $xyz = new X(new Y(new Z(/**/ foo())));'],
|
|
|
+ ['<?php $self = new self(a);'],
|
|
|
[
|
|
|
'<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
|
|
|
],
|
|
@@ -244,9 +247,306 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
|
|
|
$a = new Foo ** 1;
|
|
|
',
|
|
|
],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() <=> 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo <=> 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class']/* */()\r\n\t ;",
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class'] /* */()\r\n\t ;",
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class']()/* */;",
|
|
|
+ "<?php \$a = new \$b['class']/* */;",
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class']() /* */;",
|
|
|
+ "<?php \$a = new \$b['class'] /* */;",
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dataProvider provideNamedWithoutBracesCases
|
|
|
+ */
|
|
|
+ public function testFixNamedWithoutBraces(string $expected, ?string $input = null): void
|
|
|
+ {
|
|
|
+ $this->fixer->configure(['named_class' => false]);
|
|
|
+ $this->doTest($expected, $input);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function provideNamedWithoutBracesCases(): \Generator
|
|
|
+ {
|
|
|
+ yield from [
|
|
|
+ ['<?php $x = new X(foo(/**/));'],
|
|
|
+ ['<?php $xyz = new X(new Y(new Z(/**/ foo())));'],
|
|
|
+ ['<?php $self = new self(a);'],
|
|
|
+ [
|
|
|
+ '<?php $bar1 = new $foo->bar["baz"];',
|
|
|
+ '<?php $bar1 = new $foo->bar["baz"]();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php class A { public function B(){ $static = new static(new \SplFileInfo(__FILE__)); }}',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $static = new self(new \SplFileInfo(__FILE__));',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ /**/ /**/ /**//**//**/ /**//**//**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
|
|
|
+ '<?php $x = new X/**/ /**/ /**//**//**/ /**//**/ (/**/ /**/ /**//**//**/ /**//**/)/**/ /**/ /**//**//**/ /**//**/;/**/ /**/ /**//**//**/ /**//**/',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $x = new X;',
|
|
|
+ '<?php $x = new X();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $y = new Y ;',
|
|
|
+ '<?php $y = new Y() ;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $x = new Z /**/;//',
|
|
|
+ '<?php $x = new Z() /**/;//',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $foo = new $foo;',
|
|
|
+ '<?php $foo = new $foo();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $xyz = new X(new Y(new Z));',
|
|
|
+ '<?php $xyz = new X(new Y(new Z()));',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $foo = (new $bar)->foo;',
|
|
|
+ '<?php $foo = (new $bar())->foo;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $foo = (new $bar((new Foo)->bar))->foo;',
|
|
|
+ '<?php $foo = (new $bar((new Foo())->bar))->foo;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $self = new self;',
|
|
|
+ '<?php $self = new self();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $static = new static;',
|
|
|
+ '<?php $static = new static();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = array( "key" => new DateTime, );',
|
|
|
+ '<?php $a = array( "key" => new DateTime(), );',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = array( "key" => new DateTime );',
|
|
|
+ '<?php $a = array( "key" => new DateTime() );',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[$c];',
|
|
|
+ '<?php $a = new $b[$c]();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[$c][0];',
|
|
|
+ '<?php $a = new $b[$c][0]();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]];',
|
|
|
+ '<?php $a = new $b[$c[$d ? foo() : bar("bar[...]") - 1]]();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[\'class\'];',
|
|
|
+ '<?php $a = new $b[\'class\']();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[\'class\'] ($foo[\'bar\']);',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[\'class\'] ;',
|
|
|
+ '<?php $a = new $b[\'class\'] () ;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new $b[$c] ($hello[$world]) ;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ "<?php \$a = new \$b['class']\r\n\t ;",
|
|
|
+ "<?php \$a = new \$b['class']()\r\n\t ;",
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = $b ? new DateTime : $b;',
|
|
|
+ '<?php $a = $b ? new DateTime() : $b;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php new self::$adapters[$name]["adapter"];',
|
|
|
+ '<?php new self::$adapters[$name]["adapter"]();',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new \Exception?> <?php echo 1;',
|
|
|
+ '<?php $a = new \Exception()?> <?php echo 1;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $b = new \StdClass /**/?>',
|
|
|
+ '<?php $b = new \StdClass() /**/?>',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new Foo instanceof Foo;',
|
|
|
+ '<?php $a = new Foo() instanceof Foo;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo + 1;
|
|
|
+ $a = new Foo - 1;
|
|
|
+ $a = new Foo * 1;
|
|
|
+ $a = new Foo / 1;
|
|
|
+ $a = new Foo % 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() + 1;
|
|
|
+ $a = new Foo() - 1;
|
|
|
+ $a = new Foo() * 1;
|
|
|
+ $a = new Foo() / 1;
|
|
|
+ $a = new Foo() % 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo & 1;
|
|
|
+ $a = new Foo | 1;
|
|
|
+ $a = new Foo ^ 1;
|
|
|
+ $a = new Foo << 1;
|
|
|
+ $a = new Foo >> 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() & 1;
|
|
|
+ $a = new Foo() | 1;
|
|
|
+ $a = new Foo() ^ 1;
|
|
|
+ $a = new Foo() << 1;
|
|
|
+ $a = new Foo() >> 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo and 1;
|
|
|
+ $a = new Foo or 1;
|
|
|
+ $a = new Foo xor 1;
|
|
|
+ $a = new Foo && 1;
|
|
|
+ $a = new Foo || 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() and 1;
|
|
|
+ $a = new Foo() or 1;
|
|
|
+ $a = new Foo() xor 1;
|
|
|
+ $a = new Foo() && 1;
|
|
|
+ $a = new Foo() || 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ if (new DateTime > $this->startDate) {}
|
|
|
+ if (new DateTime >= $this->startDate) {}
|
|
|
+ if (new DateTime < $this->startDate) {}
|
|
|
+ if (new DateTime <= $this->startDate) {}
|
|
|
+ if (new DateTime == $this->startDate) {}
|
|
|
+ if (new DateTime != $this->startDate) {}
|
|
|
+ if (new DateTime <> $this->startDate) {}
|
|
|
+ if (new DateTime === $this->startDate) {}
|
|
|
+ if (new DateTime !== $this->startDate) {}
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ if (new DateTime() > $this->startDate) {}
|
|
|
+ if (new DateTime() >= $this->startDate) {}
|
|
|
+ if (new DateTime() < $this->startDate) {}
|
|
|
+ if (new DateTime() <= $this->startDate) {}
|
|
|
+ if (new DateTime() == $this->startDate) {}
|
|
|
+ if (new DateTime() != $this->startDate) {}
|
|
|
+ if (new DateTime() <> $this->startDate) {}
|
|
|
+ if (new DateTime() === $this->startDate) {}
|
|
|
+ if (new DateTime() !== $this->startDate) {}
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = new \stdClass ? $b : $c;',
|
|
|
+ '<?php $a = new \stdClass() ? $b : $c;',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php foreach (new Collection as $x) {}',
|
|
|
+ '<?php foreach (new Collection() as $x) {}',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = [(string) new Foo => 1];',
|
|
|
+ '<?php $a = [(string) new Foo() => 1];',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = [ "key" => new DateTime, ];',
|
|
|
+ '<?php $a = [ "key" => new DateTime(), ];',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php $a = [ "key" => new DateTime ];',
|
|
|
+ '<?php $a = [ "key" => new DateTime() ];',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo ** 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() ** 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new Foo <=> 1;
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new Foo() <=> 1;
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class']/* */\r\n\t ;",
|
|
|
+ "<?php \$a = new \$b['class']/* */()\r\n\t ;",
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class'] /* */\r\n\t ;",
|
|
|
+ "<?php \$a = new \$b['class'] /* */()\r\n\t ;",
|
|
|
];
|
|
|
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class']/* */;",
|
|
|
+ "<?php \$a = new \$b['class']()/* */;",
|
|
|
+ ];
|
|
|
+
|
|
|
+ yield [
|
|
|
+ "<?php \$a = new \$b['class'] /* */;",
|
|
|
+ "<?php \$a = new \$b['class']() /* */;",
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @dataProvider provideAnonymousWithDefaultConfigurationCases
|
|
|
+ */
|
|
|
+ public function testFixAnonymousWithDefaultConfiguration(string $expected, ?string $input = null): void
|
|
|
+ {
|
|
|
+ $this->doTest($expected, $input);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function provideAnonymousWithDefaultConfigurationCases(): \Generator
|
|
|
+ {
|
|
|
yield from [
|
|
|
+ ['<?php $a = new class($a) {use SomeTrait;};'],
|
|
|
+ ['<?php $a = new class(foo(/**/)) implements Foo{};'],
|
|
|
+ ['<?php $a = new class($c["d"]) /**/ extends Bar1{};'],
|
|
|
+ ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};'],
|
|
|
+ ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};'],
|
|
|
+ ['<?php $a = new class( $h /**/) {}?>'],
|
|
|
[
|
|
|
'<?php
|
|
|
$a = new Foo() <=> 1;
|
|
@@ -292,11 +592,66 @@ final class NewWithBracesFixerTest extends AbstractFixerTestCase
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @dataProvider provideAnonymousWithoutBracesCases
|
|
|
+ */
|
|
|
+ public function testFixAnonymousWithoutBraces(string $expected, ?string $input = null): void
|
|
|
+ {
|
|
|
+ $this->fixer->configure(['anonymous_class' => false]);
|
|
|
+ $this->doTest($expected, $input);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function provideAnonymousWithoutBracesCases(): \Generator
|
|
|
+ {
|
|
|
+ yield from [
|
|
|
+ ['<?php $a = new class($a) {use SomeTrait;};'],
|
|
|
+ ['<?php $a = new class(foo(/**/)) implements Foo{};'],
|
|
|
+ ['<?php $a = new class($c["d"]) /**/ extends Bar1{};'],
|
|
|
+ ['<?php $a = new class($e->f ) extends Bar2 implements Foo{};'],
|
|
|
+ ['<?php $a = new class( /**/ $g ) extends Bar3 implements Foo, Foo2{};'],
|
|
|
+ ['<?php $a = new class( $h /**/) {}?>'],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ $a = new class {use SomeTrait;};
|
|
|
+ $a = new class implements Foo{};
|
|
|
+ $a = new class /**/ extends Bar1{};
|
|
|
+ $a = new class extends Bar2 implements Foo{};
|
|
|
+ $a = new class extends Bar3 implements Foo, Foo2{};
|
|
|
+ $a = new class {}?>
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ $a = new class() {use SomeTrait;};
|
|
|
+ $a = new class() implements Foo{};
|
|
|
+ $a = new class() /**/ extends Bar1{};
|
|
|
+ $a = new class() extends Bar2 implements Foo{};
|
|
|
+ $a = new class() extends Bar3 implements Foo, Foo2{};
|
|
|
+ $a = new class ( ) {}?>
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ '<?php
|
|
|
+ class A {
|
|
|
+ public function B() {
|
|
|
+ $static = new static(new class{});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ',
|
|
|
+ '<?php
|
|
|
+ class A {
|
|
|
+ public function B() {
|
|
|
+ $static = new static(new class(){});
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ',
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @dataProvider provideFixPre80Cases
|
|
|
* @requires PHP <8.0
|
|
|
*/
|
|
|
- public function testFixPre80(string $expected, string $input = null): void
|
|
|
+ public function testFixPre80(string $expected, ?string $input = null): void
|
|
|
{
|
|
|
$this->doTest($expected, $input);
|
|
|
}
|