PhpdocReturnSelfReferenceFixerTest.php 6.6 KB

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