* 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 */ final class NoPhp4ConstructorFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } 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 {}?> ', ]; } /** * @dataProvider provideSimpleClassCases */ public function testSimpleClass(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } public static function provideSimpleClassCases(): iterable { yield [ <<<'EOF' doTest($expected); } public function testNamespaces2(): void { $expected = <<<'EOF' doTest($expected); } public function testNamespaceGlobal(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testPhp5Only(): void { $expected = <<<'EOF' doTest($expected); } public function testPhp4Only(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testBothTheRightWay1(): void { $expected = <<<'EOF' __construct(); } public function bar() { var_dump(3); } } EOF; $this->doTest($expected, $input); } public function testBothTheRightWay2(): void { $expected = <<<'EOF' __construct($bar); } public function bar() { var_dump(3); } } EOF; $this->doTest($expected, $input); } public function testBothTheRightWay3(): void { $expected = <<<'EOF' __construct($bar, $baz); } public function bar() { var_dump(3); } } EOF; $this->doTest($expected, $input); } public function testBothTheOtherWayAround(): void { $expected = <<<'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; $this->doTest($expected, $input); } public function testPhp4Parent(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testPhp4ParentInit(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testMixedParent(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testMixedParent2(): void { $expected = <<<'EOF' FooParenT(1); var_dump(9); } function bar() { var_dump(3); } } EOF; $this->doTest($expected, $input); } public function testParentOther(): void { $expected = <<<'EOF' FooParent(1); var_dump(9); } function bar() { var_dump(3); } } EOF; $this->doTest($expected, $input); } public function testParentOther2(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testClassWithAnonymous(): void { $expected = <<<'EOF' bar = function () {}; } } EOF; $input = <<<'EOF' bar = function () {}; } } EOF; $this->doTest($expected, $input); } public function testClassWithComments(): void { $expected = <<<'EOF' doTest($expected, $input); } public function testAlphaBeta(): void { $expected = <<<'EOF' doTest($expected); } public function testAlphaBetaTrick1(): void { $expected = <<<'EOF' __construct() echo 'alpha'; } public function __construct() { echo 'beta'; } } EOF; $this->doTest($expected); } public function testAlphaBetaTrick2(): void { $expected = <<<'EOF' Foo() echo 'beta'; } } EOF; $this->doTest($expected); } public function testAlphaBetaTrick3(): void { $expected = <<<'EOF' __construct(); } public function __construct() { echo 'beta'; } } EOF; $this->doTest($expected); } public function testAlphaBetaTrick4WithAnotherClass(): void { $expected = <<<'EOF' Foo(); // Do something more! echo 'beta'; } } Class Bar { function __construct() { $this->foo = 1; } } EOF; $input = <<<'EOF' Foo(); // Do something more! echo 'beta'; } } Class Bar { function bar() { $this->foo = 1; } } EOF; $this->doTest($expected, $input); } public function testAbstract(): void { $expected = <<<'EOF' doTest($expected); } public function testAbstractTrick(): void { $expected = <<<'EOF' __construct(); } public function __construct() { $this->baz = 1; } } EOF; $this->doTest($expected); } public function testParentMultipleClasses(): void { $expected = <<<'EOF' EOF; $input = <<<'EOF' Parent1(); echo "something"; } } class Class2 extends Parent2 { function __construct($foo) { echo "something"; } } ?> EOF; $this->doTest($expected, $input); } public function testInfiniteRecursion(): void { $expected = <<<'EOF' EOF; $input = <<<'EOF' __construct(); echo "something"; } } ?> EOF; $this->doTest($expected, $input); } /** * @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 [ <<<'EOF' __construct(); } }', '__construct(); } }', ]; yield [ 'Bar(); } }', ]; yield [ '__construct($bar, $baz); } } ', ]; } }