PhpdocIndentFixerTest.php 5.9 KB

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