PhpdocReturnSelfReferenceFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Fixer\Phpdoc;
  12. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  13. /**
  14. * @author SpacePossum
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
  19. */
  20. final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected PHP code
  24. * @param null|string $input PHP code
  25. *
  26. * @group legacy
  27. * @dataProvider provideDefaultConfigurationTestCases
  28. * @expectedDeprecation Passing NULL to set default configuration is deprecated and will not be supported in 3.0, use an empty array instead.
  29. */
  30. public function testLegacyFixWithDefaultConfiguration($expected, $input = null)
  31. {
  32. $this->fixer->configure(null);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @param string $expected PHP code
  37. * @param null|string $input PHP code
  38. *
  39. * @dataProvider provideDefaultConfigurationTestCases
  40. */
  41. public function testFixWithDefaultConfiguration($expected, $input = null)
  42. {
  43. $this->fixer->configure([]);
  44. $this->doTest($expected, $input);
  45. }
  46. public function provideDefaultConfigurationTestCases()
  47. {
  48. return [
  49. [
  50. '<?php interface A{/** @return $this */public function test();}',
  51. '<?php interface A{/** @return this */public function test();}',
  52. ],
  53. [
  54. '<?php interface B{/** @return self|int */function test();}',
  55. '<?php interface B{/** @return $SELF|int */function test();}',
  56. ],
  57. [
  58. '<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
  59. ],
  60. [
  61. '<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
  62. ],
  63. ];
  64. }
  65. /**
  66. * @param string $expected PHP code
  67. * @param null|string $input PHP code
  68. *
  69. * @group legacy
  70. * @dataProvider provideTestCases
  71. * @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.
  72. */
  73. public function testLegacyFix($expected, $input = null, array $configuration = [])
  74. {
  75. $this->fixer->configure($configuration);
  76. $this->doTest($expected, $input);
  77. }
  78. /**
  79. * @param string $expected PHP code
  80. * @param null|string $input PHP code
  81. *
  82. * @dataProvider provideTestCases
  83. */
  84. public function testFix($expected, $input = null, array $configuration = [])
  85. {
  86. $this->fixer->configure(['replacements' => $configuration]);
  87. $this->doTest($expected, $input);
  88. }
  89. public function provideTestCases()
  90. {
  91. return [
  92. [
  93. '<?php interface C{/** @return $self|int */function test();}',
  94. null,
  95. ['$static' => 'static'],
  96. ],
  97. ];
  98. }
  99. /**
  100. * @param string $expected
  101. * @param string $input
  102. *
  103. * @dataProvider provideGeneratedFixCases
  104. */
  105. public function testGeneratedFix($expected, $input)
  106. {
  107. $config = ['replacements' => [$input => $expected]];
  108. $this->fixer->configure($config);
  109. $expected = sprintf('<?php
  110. /**
  111. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type hint
  112. */
  113. class F
  114. {
  115. /**
  116. * @param %s
  117. *
  118. * @return %s
  119. */
  120. public function AB($self)
  121. {
  122. return $this; // %s
  123. }
  124. }
  125. ', $input, $input, $expected, $input);
  126. $input = sprintf('<?php
  127. /**
  128. * Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type hint
  129. */
  130. class F
  131. {
  132. /**
  133. * @param %s
  134. *
  135. * @return %s
  136. */
  137. public function AB($self)
  138. {
  139. return $this; // %s
  140. }
  141. }
  142. ', $input, $input, $input, $input);
  143. $this->doTest($expected, $input);
  144. }
  145. /**
  146. * Expected after fixing, return type to fix.
  147. *
  148. * @return array<array<string, string>
  149. */
  150. public function provideGeneratedFixCases()
  151. {
  152. return [
  153. ['$this', 'this'],
  154. ['$this', '@this'],
  155. ['self', '$self'],
  156. ['self', '@self'],
  157. ['static', '$static'],
  158. ['static', '@STATIC'],
  159. ];
  160. }
  161. /**
  162. * @param string $message
  163. *
  164. * @dataProvider provideInvalidConfigurationCases
  165. */
  166. public function testInvalidConfiguration(array $configuration, $message)
  167. {
  168. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  169. $this->expectExceptionMessageRegExp(sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
  170. $this->fixer->configure($configuration);
  171. }
  172. public function provideInvalidConfigurationCases()
  173. {
  174. return [
  175. [
  176. ['replacements' => [1 => 'a']],
  177. 'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static", "@static".',
  178. ],
  179. [
  180. ['replacements' => [
  181. 'this' => 'foo',
  182. ]],
  183. 'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static", "self".',
  184. ],
  185. ];
  186. }
  187. /**
  188. * @requires PHP 7.0
  189. */
  190. public function testAnonymousClassFixing()
  191. {
  192. $this->doTest(
  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. '<?php
  211. $a = new class() {
  212. /** @return @this */
  213. public function a() {
  214. }
  215. };
  216. class C
  217. {
  218. public function A()
  219. {
  220. $a = new class() {
  221. /** @return @this */
  222. public function a() {}
  223. };
  224. }
  225. }
  226. '
  227. );
  228. }
  229. }