123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?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\Phpdoc;
- use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
- */
- final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
- {
- /**
- * @dataProvider provideFixWithDefaultConfigurationCases
- */
- public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void
- {
- $this->fixer->configure([]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixWithDefaultConfigurationCases(): iterable
- {
- yield [
- '<?php interface A{/** @return $this */public function test();}',
- '<?php interface A{/** @return this */public function test();}',
- ];
- yield [
- '<?php interface B{/** @return self|int */function test();}',
- '<?php interface B{/** @return $SELF|int */function test();}',
- ];
- yield [
- '<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
- ];
- yield [
- '<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
- ];
- yield [
- '<?php
- trait SomeTrait
- {
- /** @return $this */
- public function someTest(): self
- {
- return $this;
- }
- }
- // class Foo { use Bla; } $a = (new Foo())->someTest();',
- '<?php
- trait SomeTrait
- {
- /** @return this */
- public function someTest(): self
- {
- return $this;
- }
- }
- // class Foo { use Bla; } $a = (new Foo())->someTest();',
- ];
- }
- /**
- * @param _AutogeneratedInputConfiguration['replacements'] $configurationReplacements
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $configurationReplacements = []): void
- {
- $this->fixer->configure(['replacements' => $configurationReplacements]);
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield [
- '<?php interface C{/** @return $self|int */function test();}',
- null,
- ['$static' => 'static'],
- ];
- }
- /**
- * @dataProvider provideGeneratedFixCases
- */
- public function testGeneratedFix(string $expected, string $input): void
- {
- $config = ['replacements' => [$input => $expected]];
- $this->fixer->configure($config);
- $expected = \sprintf('<?php
- /**
- * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
- */
- class F
- {
- /**
- * @param %s
- *
- * @return %s
- */
- public function AB($self)
- {
- return $this; // %s
- }
- }
- ', $input, $input, $expected, $input);
- $input = \sprintf('<?php
- /**
- * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
- */
- class F
- {
- /**
- * @param %s
- *
- * @return %s
- */
- public function AB($self)
- {
- return $this; // %s
- }
- }
- ', $input, $input, $input, $input);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideGeneratedFixCases(): iterable
- {
- yield ['$this', 'this'];
- yield ['$this', '@this'];
- yield ['self', '$self'];
- yield ['self', '@self'];
- yield ['static', '$static'];
- yield ['static', '@STATIC'];
- }
- /**
- * @param _AutogeneratedInputConfiguration $configuration
- *
- * @dataProvider provideInvalidConfigurationCases
- */
- public function testInvalidConfiguration(array $configuration, string $message): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches(\sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
- $this->fixer->configure($configuration);
- }
- public static function provideInvalidConfigurationCases(): iterable
- {
- yield [
- ['replacements' => [1 => 'a']],
- 'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static" and "@static".',
- ];
- yield [
- ['replacements' => [
- 'this' => 'foo',
- ]],
- 'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static" and "self".',
- ];
- }
- public function testAnonymousClassFixing(): void
- {
- $this->doTest(
- '<?php
- $a = new class() {
- /** @return $this */
- public function a() {
- }
- };
- class C
- {
- public function A()
- {
- $a = new class() {
- /** @return $this */
- public function a() {}
- };
- }
- }
- ',
- '<?php
- $a = new class() {
- /** @return @this */
- public function a() {
- }
- };
- class C
- {
- public function A()
- {
- $a = new class() {
- /** @return @this */
- public function a() {}
- };
- }
- }
- '
- );
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, string $input): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield [
- '<?php
- enum Foo {
- case CAT;
- /** @return $this */
- public function test(): self {
- return $this;
- }
- }
- var_dump(Foo::CAT->test());
- ',
- '<?php
- enum Foo {
- case CAT;
- /** @return this */
- public function test(): self {
- return $this;
- }
- }
- var_dump(Foo::CAT->test());
- ',
- ];
- }
- }
|