NoBlankLinesAfterPhpdocFixerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 <hello@gjcampbell.co.uk>
  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 testWhitespaceInDocBlockAboveNamespaceIsNotTouched(): void
  232. {
  233. $expected = <<<'EOF'
  234. <?php
  235. /**
  236. * This is a file-level docblock.
  237. */
  238. namespace Foo\Bar\Baz;
  239. EOF;
  240. $this->doTest($expected);
  241. }
  242. public function testFixesWindowsStyle(): void
  243. {
  244. $expected = "<?php\r\n /** * Constant! */\n \$foo = 123;";
  245. $input = "<?php\r\n /** * Constant! */\r\n\r\n\r\n \$foo = 123;";
  246. $this->doTest($expected, $input);
  247. }
  248. /**
  249. * Empty line between typehinting docs and return statement should be preserved.
  250. *
  251. * @dataProvider provideInlineTypehintingDocsBeforeFlowBreakCases
  252. */
  253. public function testInlineTypehintingDocsBeforeFlowBreak(string $expected, ?string $input = null): void
  254. {
  255. $this->doTest($expected, $input);
  256. }
  257. public static function provideInlineTypehintingDocsBeforeFlowBreakCases(): array
  258. {
  259. $cases = [];
  260. $cases[] = [<<<'EOF'
  261. <?php
  262. function parseTag($tag)
  263. {
  264. $tagClass = get_class($tag);
  265. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  266. /** @var DocBlock\Tag\VarTag $tag */
  267. return $tag->getDescription();
  268. }
  269. }
  270. EOF
  271. ];
  272. $cases[] = [<<<'EOF'
  273. <?php
  274. function parseTag($tag)
  275. {
  276. $tagClass = get_class($tag);
  277. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  278. /** @var DocBlock\Tag\VarTag $tag */
  279. throw new Exception($tag->getDescription());
  280. }
  281. }
  282. EOF
  283. ];
  284. $cases[] = [<<<'EOF'
  285. <?php
  286. function parseTag($tag)
  287. {
  288. $tagClass = get_class($tag);
  289. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  290. /** @var DocBlock\Tag\VarTag $tag */
  291. goto FOO;
  292. }
  293. FOO:
  294. }
  295. EOF
  296. ];
  297. $cases[] = [<<<'EOF'
  298. <?php
  299. function parseTag($tag)
  300. {
  301. while (true) {
  302. $tagClass = get_class($tag);
  303. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  304. /** @var DocBlock\Tag\VarTag $tag */
  305. continue;
  306. }
  307. }
  308. }
  309. EOF
  310. ];
  311. $cases[] = [<<<'EOF'
  312. <?php
  313. function parseTag($tag)
  314. {
  315. while (true) {
  316. $tagClass = get_class($tag);
  317. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  318. /** @var DocBlock\Tag\VarTag $tag */
  319. break;
  320. }
  321. }
  322. }
  323. EOF
  324. ];
  325. return $cases;
  326. }
  327. }