NoBlankLinesAfterPhpdocFixerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 testLineBeforeDeclareIsNotRemoved(): 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. /**
  134. * @dataProvider provideLineBeforeIncludeOrRequireIsNotRemovedCases
  135. */
  136. public function testLineBeforeIncludeOrRequireIsNotRemoved(string $expected, ?string $input = null): void
  137. {
  138. $this->doTest($expected, $input);
  139. }
  140. public static function provideLineBeforeIncludeOrRequireIsNotRemovedCases(): iterable
  141. {
  142. yield [<<<'EOF'
  143. <?php
  144. /**
  145. * This describes what my script does.
  146. */
  147. include 'vendor/autoload.php';
  148. EOF
  149. ];
  150. yield [<<<'EOF'
  151. <?php
  152. /**
  153. * This describes what my script does.
  154. */
  155. include_once 'vendor/autoload.php';
  156. EOF
  157. ];
  158. yield [<<<'EOF'
  159. <?php
  160. /**
  161. * This describes what my script does.
  162. */
  163. require 'vendor/autoload.php';
  164. EOF
  165. ];
  166. yield [<<<'EOF'
  167. <?php
  168. /**
  169. * This describes what my script does.
  170. */
  171. require_once 'vendor/autoload.php';
  172. EOF
  173. ];
  174. }
  175. public function testLineWithSpacesIsRemovedWhenNextTokenIsIndented(): void
  176. {
  177. $this->doTest(
  178. '<?php
  179. /**
  180. * PHPDoc with a line with space
  181. */
  182. class Foo {}',
  183. '<?php
  184. /**
  185. * PHPDoc with a line with space
  186. */
  187. '.'
  188. class Foo {}'
  189. );
  190. }
  191. public function testLineWithSpacesIsRemovedWhenNextTokenIsNotIndented(): void
  192. {
  193. $this->doTest(
  194. '<?php
  195. /**
  196. * PHPDoc with a line with space
  197. */
  198. class Foo {}',
  199. '<?php
  200. /**
  201. * PHPDoc with a line with space
  202. */
  203. '.'
  204. class Foo {}'
  205. );
  206. }
  207. public function testFixesSimpleClass(): void
  208. {
  209. $expected = <<<'EOF'
  210. <?php
  211. /**
  212. * This is the bar class.
  213. */
  214. class Bar {}
  215. EOF;
  216. $input = <<<'EOF'
  217. <?php
  218. /**
  219. * This is the bar class.
  220. */
  221. class Bar {}
  222. EOF;
  223. $this->doTest($expected, $input);
  224. }
  225. public function testFixesIndentedClass(): void
  226. {
  227. $expected = <<<'EOF'
  228. <?php
  229. /**
  230. *
  231. */
  232. class Foo {
  233. private $a;
  234. }
  235. EOF;
  236. $input = <<<'EOF'
  237. <?php
  238. /**
  239. *
  240. */
  241. class Foo {
  242. private $a;
  243. }
  244. EOF;
  245. $this->doTest($expected, $input);
  246. }
  247. public function testFixesOthers(): void
  248. {
  249. $expected = <<<'EOF'
  250. <?php
  251. /**
  252. * Constant!
  253. */
  254. const test = 'constant';
  255. /**
  256. * Foo!
  257. */
  258. $foo = 123;
  259. EOF;
  260. $input = <<<'EOF'
  261. <?php
  262. /**
  263. * Constant!
  264. */
  265. const test = 'constant';
  266. /**
  267. * Foo!
  268. */
  269. $foo = 123;
  270. EOF;
  271. $this->doTest($expected, $input);
  272. }
  273. public function testWhitespaceInDocBlockAboveNamespaceIsNotTouched(): void
  274. {
  275. $expected = <<<'EOF'
  276. <?php
  277. /**
  278. * This is a file-level docblock.
  279. */
  280. namespace Foo\Bar\Baz;
  281. EOF;
  282. $this->doTest($expected);
  283. }
  284. public function testFixesWindowsStyle(): void
  285. {
  286. $expected = "<?php\r\n /** * Constant! */\n \$foo = 123;";
  287. $input = "<?php\r\n /** * Constant! */\r\n\r\n\r\n \$foo = 123;";
  288. $this->doTest($expected, $input);
  289. }
  290. /**
  291. * Empty line between typehinting docs and return statement should be preserved.
  292. *
  293. * @dataProvider provideInlineTypehintingDocsBeforeFlowBreakCases
  294. */
  295. public function testInlineTypehintingDocsBeforeFlowBreak(string $expected, ?string $input = null): void
  296. {
  297. $this->doTest($expected, $input);
  298. }
  299. public static function provideInlineTypehintingDocsBeforeFlowBreakCases(): iterable
  300. {
  301. yield [<<<'EOF'
  302. <?php
  303. function parseTag($tag)
  304. {
  305. $tagClass = get_class($tag);
  306. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  307. /** @var DocBlock\Tag\VarTag $tag */
  308. return $tag->getDescription();
  309. }
  310. }
  311. EOF
  312. ];
  313. yield [<<<'EOF'
  314. <?php
  315. function parseTag($tag)
  316. {
  317. $tagClass = get_class($tag);
  318. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  319. /** @var DocBlock\Tag\VarTag $tag */
  320. throw new Exception($tag->getDescription());
  321. }
  322. }
  323. EOF
  324. ];
  325. yield [<<<'EOF'
  326. <?php
  327. function parseTag($tag)
  328. {
  329. $tagClass = get_class($tag);
  330. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  331. /** @var DocBlock\Tag\VarTag $tag */
  332. goto FOO;
  333. }
  334. FOO:
  335. }
  336. EOF
  337. ];
  338. yield [<<<'EOF'
  339. <?php
  340. function parseTag($tag)
  341. {
  342. while (true) {
  343. $tagClass = get_class($tag);
  344. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  345. /** @var DocBlock\Tag\VarTag $tag */
  346. continue;
  347. }
  348. }
  349. }
  350. EOF
  351. ];
  352. yield [<<<'EOF'
  353. <?php
  354. function parseTag($tag)
  355. {
  356. while (true) {
  357. $tagClass = get_class($tag);
  358. if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
  359. /** @var DocBlock\Tag\VarTag $tag */
  360. break;
  361. }
  362. }
  363. }
  364. EOF
  365. ];
  366. }
  367. }