PhpdocIndentFixerTest.php 5.9 KB

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