PhpdocReturnSelfReferenceFixerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Fixer\Phpdoc;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer>
  21. *
  22. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
  23. */
  24. final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixWithDefaultConfigurationCases
  28. */
  29. public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void
  30. {
  31. $this->fixer->configure([]);
  32. $this->doTest($expected, $input);
  33. }
  34. /**
  35. * @return iterable<array{0: string, 1?: string}>
  36. */
  37. public static function provideFixWithDefaultConfigurationCases(): iterable
  38. {
  39. yield [
  40. '<?php interface A{/** @return $this */public function test();}',
  41. '<?php interface A{/** @return this */public function test();}',
  42. ];
  43. yield [
  44. '<?php interface B{/** @return self|int */function test();}',
  45. '<?php interface B{/** @return $SELF|int */function test();}',
  46. ];
  47. yield [
  48. '<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
  49. ];
  50. yield [
  51. '<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
  52. ];
  53. yield [
  54. '<?php
  55. trait SomeTrait
  56. {
  57. /** @return $this */
  58. public function someTest(): self
  59. {
  60. return $this;
  61. }
  62. }
  63. // class Foo { use Bla; } $a = (new Foo())->someTest();',
  64. '<?php
  65. trait SomeTrait
  66. {
  67. /** @return this */
  68. public function someTest(): self
  69. {
  70. return $this;
  71. }
  72. }
  73. // class Foo { use Bla; } $a = (new Foo())->someTest();',
  74. ];
  75. }
  76. /**
  77. * @param _AutogeneratedInputConfiguration['replacements'] $configurationReplacements
  78. *
  79. * @dataProvider provideFixCases
  80. */
  81. public function testFix(string $expected, ?string $input = null, array $configurationReplacements = []): void
  82. {
  83. $this->fixer->configure(['replacements' => $configurationReplacements]);
  84. $this->doTest($expected, $input);
  85. }
  86. public static function provideFixCases(): iterable
  87. {
  88. yield [
  89. '<?php interface C{/** @return $self|int */function test();}',
  90. null,
  91. ['$static' => 'static'],
  92. ];
  93. }
  94. /**
  95. * @dataProvider provideGeneratedFixCases
  96. */
  97. public function testGeneratedFix(string $expected, string $input): void
  98. {
  99. $config = ['replacements' => [$input => $expected]];
  100. $this->fixer->configure($config);
  101. $expected = \sprintf('<?php
  102. /**
  103. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
  104. */
  105. class F
  106. {
  107. /**
  108. * @param %s
  109. *
  110. * @return %s
  111. */
  112. public function AB($self)
  113. {
  114. return $this; // %s
  115. }
  116. }
  117. ', $input, $input, $expected, $input);
  118. $input = \sprintf('<?php
  119. /**
  120. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
  121. */
  122. class F
  123. {
  124. /**
  125. * @param %s
  126. *
  127. * @return %s
  128. */
  129. public function AB($self)
  130. {
  131. return $this; // %s
  132. }
  133. }
  134. ', $input, $input, $input, $input);
  135. $this->doTest($expected, $input);
  136. }
  137. /**
  138. * @return iterable<array{string, string}>
  139. */
  140. public static function provideGeneratedFixCases(): iterable
  141. {
  142. yield ['$this', 'this'];
  143. yield ['$this', '@this'];
  144. yield ['self', '$self'];
  145. yield ['self', '@self'];
  146. yield ['static', '$static'];
  147. yield ['static', '@STATIC'];
  148. }
  149. /**
  150. * @param _AutogeneratedInputConfiguration $configuration
  151. *
  152. * @dataProvider provideInvalidConfigurationCases
  153. */
  154. public function testInvalidConfiguration(array $configuration, string $message): void
  155. {
  156. $this->expectException(InvalidFixerConfigurationException::class);
  157. $this->expectExceptionMessageMatches(\sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
  158. $this->fixer->configure($configuration);
  159. }
  160. public static function provideInvalidConfigurationCases(): iterable
  161. {
  162. yield [
  163. ['replacements' => [1 => 'a']],
  164. 'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static" and "@static".',
  165. ];
  166. yield [
  167. ['replacements' => [
  168. 'this' => 'foo',
  169. ]],
  170. 'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static" and "self".',
  171. ];
  172. }
  173. public function testAnonymousClassFixing(): void
  174. {
  175. $this->doTest(
  176. '<?php
  177. $a = new class() {
  178. /** @return $this */
  179. public function a() {
  180. }
  181. };
  182. class C
  183. {
  184. public function A()
  185. {
  186. $a = new class() {
  187. /** @return $this */
  188. public function a() {}
  189. };
  190. }
  191. }
  192. ',
  193. '<?php
  194. $a = new class() {
  195. /** @return @this */
  196. public function a() {
  197. }
  198. };
  199. class C
  200. {
  201. public function A()
  202. {
  203. $a = new class() {
  204. /** @return @this */
  205. public function a() {}
  206. };
  207. }
  208. }
  209. '
  210. );
  211. }
  212. /**
  213. * @dataProvider provideFix81Cases
  214. *
  215. * @requires PHP 8.1
  216. */
  217. public function testFix81(string $expected, string $input): void
  218. {
  219. $this->doTest($expected, $input);
  220. }
  221. /**
  222. * @return iterable<array{string, string}>
  223. */
  224. public static function provideFix81Cases(): iterable
  225. {
  226. yield [
  227. '<?php
  228. enum Foo {
  229. case CAT;
  230. /** @return $this */
  231. public function test(): self {
  232. return $this;
  233. }
  234. }
  235. var_dump(Foo::CAT->test());
  236. ',
  237. '<?php
  238. enum Foo {
  239. case CAT;
  240. /** @return this */
  241. public function test(): self {
  242. return $this;
  243. }
  244. }
  245. var_dump(Foo::CAT->test());
  246. ',
  247. ];
  248. }
  249. }