PhpdocReturnSelfReferenceFixerTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @dataProvider provideFixWithDefaultConfigurationCases
  24. */
  25. public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void
  26. {
  27. $this->fixer->configure([]);
  28. $this->doTest($expected, $input);
  29. }
  30. public static function provideFixWithDefaultConfigurationCases(): iterable
  31. {
  32. yield [
  33. '<?php interface A{/** @return $this */public function test();}',
  34. '<?php interface A{/** @return this */public function test();}',
  35. ];
  36. yield [
  37. '<?php interface B{/** @return self|int */function test();}',
  38. '<?php interface B{/** @return $SELF|int */function test();}',
  39. ];
  40. yield [
  41. '<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
  42. ];
  43. yield [
  44. '<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
  45. ];
  46. yield [
  47. '<?php
  48. trait SomeTrait
  49. {
  50. /** @return $this */
  51. public function someTest(): self
  52. {
  53. return $this;
  54. }
  55. }
  56. // class Foo { use Bla; } $a = (new Foo())->someTest();',
  57. '<?php
  58. trait SomeTrait
  59. {
  60. /** @return this */
  61. public function someTest(): self
  62. {
  63. return $this;
  64. }
  65. }
  66. // class Foo { use Bla; } $a = (new Foo())->someTest();',
  67. ];
  68. }
  69. /**
  70. * @param array<string, string> $configuration
  71. *
  72. * @dataProvider provideFixCases
  73. */
  74. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  75. {
  76. $this->fixer->configure(['replacements' => $configuration]);
  77. $this->doTest($expected, $input);
  78. }
  79. public static function provideFixCases(): iterable
  80. {
  81. yield [
  82. '<?php interface C{/** @return $self|int */function test();}',
  83. null,
  84. ['$static' => 'static'],
  85. ];
  86. }
  87. /**
  88. * @dataProvider provideGeneratedFixCases
  89. */
  90. public function testGeneratedFix(string $expected, string $input): void
  91. {
  92. $config = ['replacements' => [$input => $expected]];
  93. $this->fixer->configure($config);
  94. $expected = sprintf('<?php
  95. /**
  96. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type hint
  97. */
  98. class F
  99. {
  100. /**
  101. * @param %s
  102. *
  103. * @return %s
  104. */
  105. public function AB($self)
  106. {
  107. return $this; // %s
  108. }
  109. }
  110. ', $input, $input, $expected, $input);
  111. $input = sprintf('<?php
  112. /**
  113. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type hint
  114. */
  115. class F
  116. {
  117. /**
  118. * @param %s
  119. *
  120. * @return %s
  121. */
  122. public function AB($self)
  123. {
  124. return $this; // %s
  125. }
  126. }
  127. ', $input, $input, $input, $input);
  128. $this->doTest($expected, $input);
  129. }
  130. public static function provideGeneratedFixCases(): iterable
  131. {
  132. yield ['$this', 'this'];
  133. yield ['$this', '@this'];
  134. yield ['self', '$self'];
  135. yield ['self', '@self'];
  136. yield ['static', '$static'];
  137. yield ['static', '@STATIC'];
  138. }
  139. /**
  140. * @param array<mixed> $configuration
  141. *
  142. * @dataProvider provideInvalidConfigurationCases
  143. */
  144. public function testInvalidConfiguration(array $configuration, string $message): void
  145. {
  146. $this->expectException(InvalidFixerConfigurationException::class);
  147. $this->expectExceptionMessageMatches(sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
  148. $this->fixer->configure($configuration);
  149. }
  150. public static function provideInvalidConfigurationCases(): iterable
  151. {
  152. yield [
  153. ['replacements' => [1 => 'a']],
  154. 'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static" and "@static".',
  155. ];
  156. yield [
  157. ['replacements' => [
  158. 'this' => 'foo',
  159. ]],
  160. 'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static" and "self".',
  161. ];
  162. }
  163. public function testAnonymousClassFixing(): void
  164. {
  165. $this->doTest(
  166. '<?php
  167. $a = new class() {
  168. /** @return $this */
  169. public function a() {
  170. }
  171. };
  172. class C
  173. {
  174. public function A()
  175. {
  176. $a = new class() {
  177. /** @return $this */
  178. public function a() {}
  179. };
  180. }
  181. }
  182. ',
  183. '<?php
  184. $a = new class() {
  185. /** @return @this */
  186. public function a() {
  187. }
  188. };
  189. class C
  190. {
  191. public function A()
  192. {
  193. $a = new class() {
  194. /** @return @this */
  195. public function a() {}
  196. };
  197. }
  198. }
  199. '
  200. );
  201. }
  202. /**
  203. * @dataProvider provideFix81Cases
  204. *
  205. * @requires PHP 8.1
  206. */
  207. public function testFix81(string $expected, string $input): void
  208. {
  209. $this->doTest($expected, $input);
  210. }
  211. public static function provideFix81Cases(): iterable
  212. {
  213. yield [
  214. '<?php
  215. enum Foo {
  216. case CAT;
  217. /** @return $this */
  218. public function test(): self {
  219. return $this;
  220. }
  221. }
  222. var_dump(Foo::CAT->test());
  223. ',
  224. '<?php
  225. enum Foo {
  226. case CAT;
  227. /** @return this */
  228. public function test(): self {
  229. return $this;
  230. }
  231. }
  232. var_dump(Foo::CAT->test());
  233. ',
  234. ];
  235. }
  236. }