PhpdocIndentFixerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 Ceeram <ceeram@cakephp.org>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocIndentFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocIndentFixer>
  22. */
  23. final class PhpdocIndentFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield ['<?php /** @var Foo $foo */ ?>'];
  38. yield ['<?php /** foo */'];
  39. yield [
  40. '<?php
  41. /**
  42. * Do not indent
  43. */
  44. /**
  45. * Do not indent this
  46. */
  47. class DocBlocks
  48. {
  49. /**
  50. *Test that attribute docblocks are indented
  51. */
  52. protected $indent = false;
  53. /**
  54. * Test that method docblocks are indented.
  55. */
  56. public function test() {}
  57. }',
  58. '<?php
  59. /**
  60. * Do not indent
  61. */
  62. /**
  63. * Do not indent this
  64. */
  65. class DocBlocks
  66. {
  67. /**
  68. *Test that attribute docblocks are indented
  69. */
  70. protected $indent = false;
  71. /**
  72. * Test that method docblocks are indented.
  73. */
  74. public function test() {}
  75. }',
  76. ];
  77. yield [
  78. '<?php
  79. class DocBlocks
  80. {
  81. /**
  82. * Test constants
  83. */
  84. const INDENT = 1;
  85. /**
  86. * Test with var keyword
  87. */
  88. var $oldStyle = false;
  89. /**
  90. * Test static
  91. */
  92. public static function test1() {}
  93. /**
  94. * Test static first.
  95. */
  96. static public function test2() {}
  97. /**
  98. * Test final first.
  99. */
  100. final public function test3() {}
  101. /**
  102. * Test no keywords
  103. */
  104. function test4() {}
  105. }',
  106. '<?php
  107. class DocBlocks
  108. {
  109. /**
  110. * Test constants
  111. */
  112. const INDENT = 1;
  113. /**
  114. * Test with var keyword
  115. */
  116. var $oldStyle = false;
  117. /**
  118. * Test static
  119. */
  120. public static function test1() {}
  121. /**
  122. * Test static first.
  123. */
  124. static public function test2() {}
  125. /**
  126. * Test final first.
  127. */
  128. final public function test3() {}
  129. /**
  130. * Test no keywords
  131. */
  132. function test4() {}
  133. }',
  134. ];
  135. yield [
  136. '<?php
  137. /**
  138. * Final class should also not be indented
  139. */
  140. final class DocBlocks
  141. {
  142. /**
  143. * Test with var keyword
  144. */
  145. var $oldStyle = false;
  146. }',
  147. '<?php
  148. /**
  149. * Final class should also not be indented
  150. */
  151. final class DocBlocks
  152. {
  153. /**
  154. * Test with var keyword
  155. */
  156. var $oldStyle = false;
  157. }',
  158. ];
  159. yield [
  160. '<?php
  161. if (1) {
  162. class Foo {
  163. /**
  164. * Foo
  165. */
  166. function foo() {}
  167. /**
  168. * Bar
  169. */
  170. function bar() {}
  171. }
  172. }',
  173. '<?php
  174. if (1) {
  175. class Foo {
  176. /**
  177. * Foo
  178. */
  179. function foo() {}
  180. /**
  181. * Bar
  182. */
  183. function bar() {}
  184. }
  185. }',
  186. ];
  187. yield [
  188. '<?php
  189. /**
  190. * Variable
  191. */
  192. $variable = true;
  193. /**
  194. * Partial docblock fix
  195. */
  196. $partialFix = true;
  197. /**
  198. * Other partial docblock fix
  199. */
  200. $otherPartial = true;
  201. /** Single line */
  202. $single = true;
  203. /**
  204. * Function
  205. */
  206. function something()
  207. {
  208. /**
  209. * Inside functions
  210. */
  211. return;
  212. }
  213. /**
  214. * function call
  215. */
  216. something();
  217. /**
  218. * Control structure
  219. * @var \Sqlite3 $sqlite
  220. */
  221. foreach($connections as $sqlite) {
  222. $sqlite->open();
  223. }',
  224. '<?php
  225. /**
  226. * Variable
  227. */
  228. $variable = true;
  229. /**
  230. * Partial docblock fix
  231. */
  232. $partialFix = true;
  233. /**
  234. * Other partial docblock fix
  235. */
  236. $otherPartial = true;
  237. /** Single line */
  238. $single = true;
  239. /**
  240. * Function
  241. */
  242. function something()
  243. {
  244. /**
  245. * Inside functions
  246. */
  247. return;
  248. }
  249. /**
  250. * function call
  251. */
  252. something();
  253. /**
  254. * Control structure
  255. * @var \Sqlite3 $sqlite
  256. */
  257. foreach($connections as $sqlite) {
  258. $sqlite->open();
  259. }',
  260. ];
  261. yield [
  262. '<?php
  263. $user = $event->getForm()->getData(); /** @var User $user */
  264. echo "Success";',
  265. ];
  266. yield [
  267. '<?php
  268. $user = $event->getForm()->getData();/** @var User $user */
  269. echo "Success";',
  270. ];
  271. yield [
  272. "<?php
  273. class DocBlocks
  274. {
  275. \t/**
  276. \t *Test that attribute docblocks are indented
  277. \t */
  278. \tprotected \$indent = false;
  279. \t/**
  280. \t * Test that method docblocks are indented.
  281. \t */
  282. \tpublic function test() {}
  283. }",
  284. "<?php
  285. class DocBlocks
  286. {
  287. /**
  288. *Test that attribute docblocks are indented
  289. */
  290. \tprotected \$indent = false;
  291. /**
  292. * Test that method docblocks are indented.
  293. */
  294. \tpublic function test() {}
  295. }",
  296. ];
  297. yield [
  298. '<?php
  299. /**
  300. * Used to write a value to a session key.
  301. *
  302. * ...
  303. */
  304. function write($name) {}
  305. ',
  306. "<?php
  307. \t/**
  308. * Used to write a value to a session key.
  309. *
  310. * ...
  311. */
  312. function write(\$name) {}
  313. ",
  314. ];
  315. yield [
  316. '<?php
  317. class Foo
  318. {
  319. public function bar()
  320. {
  321. /**
  322. * baz
  323. */
  324. }
  325. }',
  326. ];
  327. yield [
  328. '<?php
  329. /**
  330. * docs
  331. */
  332. // comment
  333. $foo = $bar;
  334. ',
  335. ];
  336. yield [
  337. '<?php
  338. function foo()
  339. {
  340. $foo->bar(/** oops */$baz);
  341. $foo->bar($a,/** oops */$baz);
  342. }',
  343. ];
  344. yield [
  345. '<?php
  346. /**
  347. * Foo
  348. Bar
  349. */
  350. class Foo
  351. {
  352. }',
  353. ];
  354. yield [
  355. '<?php
  356. class Application
  357. {
  358. }/**
  359. */
  360. class Dispatcher
  361. {
  362. }
  363. ',
  364. ];
  365. yield [
  366. '<?php
  367. $foo->bar() /** comment */
  368. ->baz();
  369. ',
  370. '<?php
  371. $foo->bar()/** comment */
  372. ->baz();
  373. ',
  374. ];
  375. }
  376. }