NoBlankLinesAfterPhpdocFixerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Graham Campbell <graham@alt-three.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\NoBlankLinesAfterPhpdocFixer
  20. */
  21. final class NoBlankLinesAfterPhpdocFixerTest extends AbstractFixerTestCase
  22. {
  23. public function testSimpleExampleIsNotChanged(): void
  24. {
  25. $input = <<<'EOF'
  26. <?php
  27. /**
  28. * This is the bar class.
  29. */
  30. class Bar
  31. {
  32. /**
  33. * @return void
  34. */
  35. public function foo()
  36. {
  37. //
  38. }
  39. }
  40. EOF;
  41. $this->doTest($input);
  42. }
  43. public function testComplexExampleIsNotChanged(): void
  44. {
  45. $input = <<<'EOF'
  46. <?php
  47. /**
  48. * This is the hello function.
  49. * Yeh, this layout should be allowed.
  50. * We're fixing lines following a docblock.
  51. */
  52. function hello($foo) {}
  53. /**
  54. * This is the bar class.
  55. */
  56. final class Bar
  57. {
  58. /**
  59. * @return void
  60. */
  61. public static function foo()
  62. {
  63. //
  64. }
  65. /**
  66. * @return void
  67. */
  68. static private function bar123() {}
  69. /*
  70. * This T_COMMENT should not be moved
  71. *
  72. * Only T_DOC_COMMENT should be moved
  73. */
  74. final protected
  75. // mixin' it up a bit
  76. function baz() {
  77. }
  78. /*
  79. * This T_COMMENT should not be moved
  80. *
  81. * Only T_DOC_COMMENT should be moved
  82. */
  83. public function cool() {}
  84. /**
  85. * This is the first docblock
  86. *
  87. * Not removing blank line here.
  88. * No element is being documented
  89. */
  90. /**
  91. * Another docblock
  92. */
  93. public function silly() {}
  94. }
  95. EOF;
  96. $this->doTest($input);
  97. }
  98. public function testCommentsAreNotChanged(): void
  99. {
  100. $input = <<<'EOF'
  101. <?php
  102. /*
  103. * This file is part of xyz.
  104. *
  105. * License etc...
  106. */
  107. namespace Foo\Bar;
  108. EOF;
  109. $this->doTest($input);
  110. }
  111. public function testLineBeforeDeclareIsNotBeRemoved(): void
  112. {
  113. $expected = <<<'EOF'
  114. <?php
  115. /**
  116. * This is some license header.
  117. */
  118. declare(strict_types=1);
  119. EOF;
  120. $this->doTest($expected);
  121. }
  122. public function testLineBeforeUseStatementIsNotRemoved(): void
  123. {
  124. $expected = <<<'EOF'
  125. <?php
  126. /**
  127. * This is some license header.
  128. */
  129. use Foo\Bar;
  130. EOF;
  131. $this->doTest($expected);
  132. }
  133. public function testLineWithSpacesIsRemovedWhenNextTokenIsIndented(): void
  134. {
  135. $this->doTest(
  136. '<?php
  137. /**
  138. * PHPDoc with a line with space
  139. */
  140. class Foo {}',
  141. '<?php
  142. /**
  143. * PHPDoc with a line with space
  144. */
  145. '.'
  146. class Foo {}'
  147. );
  148. }
  149. public function testLineWithSpacesIsRemovedWhenNextTokenIsNotIndented(): void
  150. {
  151. $this->doTest(
  152. '<?php
  153. /**
  154. * PHPDoc with a line with space
  155. */
  156. class Foo {}',
  157. '<?php
  158. /**
  159. * PHPDoc with a line with space
  160. */
  161. '.'
  162. class Foo {}'
  163. );
  164. }
  165. public function testFixesSimpleClass(): void
  166. {
  167. $expected = <<<'EOF'
  168. <?php
  169. /**
  170. * This is the bar class.
  171. */
  172. class Bar {}
  173. EOF;
  174. $input = <<<'EOF'
  175. <?php
  176. /**
  177. * This is the bar class.
  178. */
  179. class Bar {}
  180. EOF;
  181. $this->doTest($expected, $input);
  182. }
  183. public function testFixesIndentedClass(): void
  184. {
  185. $expected = <<<'EOF'
  186. <?php
  187. /**
  188. *
  189. */
  190. class Foo {
  191. private $a;
  192. }
  193. EOF;
  194. $input = <<<'EOF'
  195. <?php
  196. /**
  197. *
  198. */
  199. class Foo {
  200. private $a;
  201. }
  202. EOF;
  203. $this->doTest($expected, $input);
  204. }
  205. public function testFixesOthers(): void
  206. {
  207. $expected = <<<'EOF'
  208. <?php
  209. /**
  210. * Constant!
  211. */
  212. const test = 'constant';
  213. /**
  214. * Foo!
  215. */
  216. $foo = 123;
  217. EOF;
  218. $input = <<<'EOF'
  219. <?php
  220. /**
  221. * Constant!
  222. */
  223. const test = 'constant';
  224. /**
  225. * Foo!
  226. */
  227. $foo = 123;
  228. EOF;
  229. $this->doTest($expected, $input);
  230. }
  231. public function testFixesWindowsStyle(): void
  232. {
  233. $expected = "<?php\r\n /** * Constant! */\n \$foo = 123;";
  234. $input = "<?php\r\n /** * Constant! */\r\n\r\n\r\n \$foo = 123;";
  235. $this->doTest($expected, $input);
  236. }
  237. /**
  238. * Empty line between typehinting docs and return statement should be preserved.
  239. *
  240. * @dataProvider provideInlineTypehintingDocsBeforeFlowBreakCases
  241. */
  242. public function testInlineTypehintingDocsBeforeFlowBreak(string $expected, ?string $input = null): void
  243. {
  244. $this->doTest($expected, $input);
  245. }
  246. public function provideInlineTypehintingDocsBeforeFlowBreakCases()
  247. {
  248. $cases = [];
  249. $cases[] = [<<<'EOF'
  250. <?php
  251. function parseTag($tag)
  252. {
  253. $tagClass = get_class($tag);
  254. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  255. /** @var DocBlock\Tag\VarTag $tag */
  256. return $tag->getDescription();
  257. }
  258. }
  259. EOF
  260. ];
  261. $cases[] = [<<<'EOF'
  262. <?php
  263. function parseTag($tag)
  264. {
  265. $tagClass = get_class($tag);
  266. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  267. /** @var DocBlock\Tag\VarTag $tag */
  268. throw new Exception($tag->getDescription());
  269. }
  270. }
  271. EOF
  272. ];
  273. $cases[] = [<<<'EOF'
  274. <?php
  275. function parseTag($tag)
  276. {
  277. $tagClass = get_class($tag);
  278. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  279. /** @var DocBlock\Tag\VarTag $tag */
  280. goto FOO;
  281. }
  282. FOO:
  283. }
  284. EOF
  285. ];
  286. $cases[] = [<<<'EOF'
  287. <?php
  288. function parseTag($tag)
  289. {
  290. while (true) {
  291. $tagClass = get_class($tag);
  292. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  293. /** @var DocBlock\Tag\VarTag $tag */
  294. continue;
  295. }
  296. }
  297. }
  298. EOF
  299. ];
  300. $cases[] = [<<<'EOF'
  301. <?php
  302. function parseTag($tag)
  303. {
  304. while (true) {
  305. $tagClass = get_class($tag);
  306. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  307. /** @var DocBlock\Tag\VarTag $tag */
  308. break;
  309. }
  310. }
  311. }
  312. EOF
  313. ];
  314. return $cases;
  315. }
  316. }