123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- /*
- * 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\Tests\Test\AbstractFixerTestCase;
- /**
- * @author SpacePossum
- *
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
- */
- final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param string $expected PHP code
- * @param null|string $input PHP code
- *
- * @group legacy
- * @dataProvider provideDefaultConfigurationTestCases
- * @expectedDeprecation Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.
- */
- public function testLegacyFixWithDefaultConfiguration($expected, $input = null)
- {
- $this->fixer->configure(null);
- $this->doTest($expected, $input);
- }
- /**
- * @param string $expected PHP code
- * @param null|string $input PHP code
- *
- * @dataProvider provideDefaultConfigurationTestCases
- */
- public function testFixWithDefaultConfiguration($expected, $input = null)
- {
- $this->fixer->configure([]);
- $this->doTest($expected, $input);
- }
- public function provideDefaultConfigurationTestCases()
- {
- return [
- [
- '<?php interface A{/** @return $this */public function test();}',
- '<?php interface A{/** @return this */public function test();}',
- ],
- [
- '<?php interface B{/** @return self|int */function test();}',
- '<?php interface B{/** @return $SELF|int */function test();}',
- ],
- [
- '<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
- ],
- [
- '<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
- ],
- ];
- }
- /**
- * @param string $expected PHP code
- * @param null|string $input PHP code
- *
- * @group legacy
- * @dataProvider provideTestCases
- * @expectedDeprecation Passing "replacements" at the root of the configuration for rule "phpdoc_return_self_reference" is deprecated and will not be supported in 3.0, use "replacements" => array(...) option instead.
- */
- public function testLegacyFix($expected, $input = null, array $configuration = [])
- {
- $this->fixer->configure($configuration);
- $this->doTest($expected, $input);
- }
- /**
- * @param string $expected PHP code
- * @param null|string $input PHP code
- *
- * @dataProvider provideTestCases
- */
- public function testFix($expected, $input = null, array $configuration = [])
- {
- $this->fixer->configure(['replacements' => $configuration]);
- $this->doTest($expected, $input);
- }
- public function provideTestCases()
- {
- return [
- [
- '<?php interface C{/** @return $self|int */function test();}',
- null,
- ['$static' => 'static'],
- ],
- ];
- }
- /**
- * @param string $expected
- * @param string $input
- *
- * @dataProvider provideGeneratedFixCases
- */
- public function testGeneratedFix($expected, $input)
- {
- $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 hint
- */
- 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 hint
- */
- class F
- {
- /**
- * @param %s
- *
- * @return %s
- */
- public function AB($self)
- {
- return $this; // %s
- }
- }
- ', $input, $input, $input, $input);
- $this->doTest($expected, $input);
- }
- /**
- * Expected after fixing, return type to fix.
- *
- * @return array<array<string, string>
- */
- public function provideGeneratedFixCases()
- {
- return [
- ['$this', 'this'],
- ['$this', '@this'],
- ['self', '$self'],
- ['self', '@self'],
- ['static', '$static'],
- ['static', '@STATIC'],
- ];
- }
- /**
- * @param string $message
- *
- * @dataProvider provideInvalidConfigurationCases
- */
- public function testInvalidConfiguration(array $configuration, $message)
- {
- $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageRegExp(sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
- $this->fixer->configure($configuration);
- }
- public function provideInvalidConfigurationCases()
- {
- return [
- [
- ['replacements' => [1 => 'a']],
- 'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static", "@static".',
- ],
- [
- ['replacements' => [
- 'this' => 'foo',
- ]],
- 'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static", "self".',
- ],
- ];
- }
- /**
- * @requires PHP 7.0
- */
- public function testAnonymousClassFixing()
- {
- $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() {}
- };
- }
- }
- '
- );
- }
- }
|