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