* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\ClassNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Matteo Beccati * * @internal * * @covers \PhpCsFixer\Fixer\ClassNotation\NoPhp4ConstructorFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\NoPhp4ConstructorFixer> */ final class NoPhp4ConstructorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { yield [ '', ]; yield [ ' 1; $a = new Foo <=> 1; $a = new class() {}; $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() {}; $a = new class {}; $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 {}?> ', ]; yield 'simple class 1' => [ <<<'EOF' [ <<<'EOF' [<<<'EOF' [<<<'EOF' [ <<<'EOF' [<<<'EOF' [ <<<'EOF' [ <<<'EOF' __construct(); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the right way 2' => [ <<<'EOF' __construct($bar); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the right way 3' => [ <<<'EOF' __construct($bar, $baz); } public function bar() { var_dump(3); } } EOF, ]; yield 'both the other way around 1' => [ <<<'EOF' Foo($bar); } /** * PHP-4 Constructor. * * This is the real constructor. It's the one that most likely contains any meaningful info in the docblock. */ private function Foo($bar) { var_dump(1); } function bar() { var_dump(3); } } EOF, ]; yield 'PHP 4 parent' => [ <<<'EOF' [ <<<'EOF' [ <<<'EOF' [ <<<'EOF' FooParenT(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'parent other' => [ <<<'EOF' FooParent(1); var_dump(9); } function bar() { var_dump(3); } } EOF, ]; yield 'parent other 2' => [ <<<'EOF' [ <<<'EOF' bar = function () {}; } } EOF, <<<'EOF' bar = function () {}; } } EOF, ]; yield 'class with comments' => [ <<<'EOF' [<<<'EOF' [<<<'EOF' __construct() echo 'alpha'; } public function __construct() { echo 'beta'; } } EOF]; yield 'alpha beta trick 2' => [<<<'EOF' Foo() echo 'beta'; } } EOF]; yield 'alpha beta trick 3' => [<<<'EOF' __construct(); } public function __construct() { echo 'beta'; } } EOF]; yield 'alpha beta trick 4 with another class' => [ <<<'EOF' Foo(); // Do something more! echo 'beta'; } } Class Bar { function __construct() { $this->foo = 1; } } EOF, <<<'EOF' Foo(); // Do something more! echo 'beta'; } } Class Bar { function bar() { $this->foo = 1; } } EOF, ]; yield 'abstract' => [<<<'EOF' [<<<'EOF' __construct(); } public function __construct() { $this->baz = 1; } } EOF]; yield 'parent multiple classes' => [ <<<'EOF' EOF, <<<'EOF' Parent1(); echo "something"; } } class Class2 extends Parent2 { function __construct($foo) { echo "something"; } } ?> EOF, ]; yield 'infinite recursion' => [ <<<'EOF' EOF, <<<'EOF' __construct(); echo "something"; } } ?> EOF, ]; } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { yield [ <<<'EOF' __construct(); } }', '__construct(); } }', ]; yield [ 'Bar(); } }', ]; yield [ '__construct($bar, $baz); } } ', ]; } }