PhpdocLineSpanFixerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 Gert de Pagter <BackEndTea@gmail.com>
  15. *
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
  19. */
  20. final class PhpdocLineSpanFixerTest extends AbstractFixerTestCase
  21. {
  22. /**
  23. * @param string $expected
  24. * @param null|string $input
  25. *
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix($expected, $input = null, array $config = [])
  29. {
  30. $this->fixer->configure($config);
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function provideFixCases()
  37. {
  38. return [
  39. 'It does not change doc blocks if not needed' => [
  40. '<?php
  41. class Foo
  42. {
  43. /**
  44. * Important
  45. */
  46. const FOO_BAR = "foobar";
  47. /**
  48. * @var bool
  49. */
  50. public $variable = true;
  51. /**
  52. * @var bool
  53. */
  54. private $var = false;
  55. /**
  56. * @return void
  57. */
  58. public function hello() {}
  59. }
  60. ',
  61. ],
  62. 'It does change doc blocks to multi by default' => [
  63. '<?php
  64. class Foo
  65. {
  66. /**
  67. * Important
  68. */
  69. const FOO_BAR = "foobar";
  70. /**
  71. * @var bool
  72. */
  73. public $variable = true;
  74. /**
  75. * @var bool
  76. */
  77. private $var = false;
  78. /**
  79. * @return void
  80. */
  81. public function hello() {}
  82. }
  83. ',
  84. '<?php
  85. class Foo
  86. {
  87. /** Important */
  88. const FOO_BAR = "foobar";
  89. /** @var bool */
  90. public $variable = true;
  91. /** @var bool */
  92. private $var = false;
  93. /** @return void */
  94. public function hello() {}
  95. }
  96. ',
  97. ],
  98. 'It does change doc blocks to single if configured to do so' => [
  99. '<?php
  100. class Foo
  101. {
  102. /** Important */
  103. const FOO_BAR = "foobar";
  104. /** @var bool */
  105. public $variable = true;
  106. /** @var bool */
  107. private $var = false;
  108. /** @return void */
  109. public function hello() {}
  110. }
  111. ',
  112. '<?php
  113. class Foo
  114. {
  115. /**
  116. * Important
  117. */
  118. const FOO_BAR = "foobar";
  119. /**
  120. * @var bool
  121. */
  122. public $variable = true;
  123. /**
  124. * @var bool
  125. */
  126. private $var = false;
  127. /**
  128. * @return void
  129. */
  130. public function hello() {}
  131. }
  132. ',
  133. [
  134. 'property' => 'single',
  135. 'const' => 'single',
  136. 'method' => 'single',
  137. ],
  138. ],
  139. 'It does not changes doc blocks from single if configured to do so' => [
  140. '<?php
  141. class Foo
  142. {
  143. /** Important */
  144. const FOO_BAR = "foobar";
  145. /** @var bool */
  146. public $variable = true;
  147. /** @var bool */
  148. private $var = false;
  149. /** @return void */
  150. public function hello() {}
  151. }
  152. ',
  153. null,
  154. [
  155. 'property' => 'single',
  156. 'const' => 'single',
  157. 'method' => 'single',
  158. ],
  159. ],
  160. 'It can be configured to change certain elements to single line' => [
  161. '<?php
  162. class Foo
  163. {
  164. /**
  165. * Important
  166. */
  167. const FOO_BAR = "foobar";
  168. /** @var bool */
  169. public $variable = true;
  170. /** @var bool */
  171. private $var = false;
  172. /**
  173. * @return void
  174. */
  175. public function hello() {}
  176. }
  177. ',
  178. '<?php
  179. class Foo
  180. {
  181. /**
  182. * Important
  183. */
  184. const FOO_BAR = "foobar";
  185. /**
  186. * @var bool
  187. */
  188. public $variable = true;
  189. /**
  190. * @var bool
  191. */
  192. private $var = false;
  193. /**
  194. * @return void
  195. */
  196. public function hello() {}
  197. }
  198. ',
  199. [
  200. 'property' => 'single',
  201. ],
  202. ],
  203. 'It wont change a doc block to single line if it has multiple useful lines' => [
  204. '<?php
  205. class Foo
  206. {
  207. /**
  208. * Important
  209. * Really important
  210. */
  211. const FOO_BAR = "foobar";
  212. }
  213. ',
  214. null,
  215. [
  216. 'const' => 'single',
  217. ],
  218. ],
  219. 'It updates doc blocks correctly, even with more indentation' => [
  220. '<?php
  221. if (false) {
  222. class Foo
  223. {
  224. /** @var bool */
  225. public $var = true;
  226. /**
  227. * @return void
  228. */
  229. public function hello () {}
  230. }
  231. }
  232. ',
  233. '<?php
  234. if (false) {
  235. class Foo
  236. {
  237. /**
  238. * @var bool
  239. */
  240. public $var = true;
  241. /** @return void */
  242. public function hello () {}
  243. }
  244. }
  245. ',
  246. [
  247. 'property' => 'single',
  248. ],
  249. ],
  250. 'It can convert empty doc blocks' => [
  251. '<?php
  252. class Foo
  253. {
  254. /**
  255. *
  256. */
  257. const FOO = "foobar";
  258. /** */
  259. private $foo;
  260. }',
  261. '<?php
  262. class Foo
  263. {
  264. /** */
  265. const FOO = "foobar";
  266. /**
  267. *
  268. */
  269. private $foo;
  270. }',
  271. [
  272. 'property' => 'single',
  273. ],
  274. ],
  275. 'It can update doc blocks of static properties' => [
  276. '<?php
  277. class Bar
  278. {
  279. /**
  280. * Important
  281. */
  282. public static $variable = "acme";
  283. }
  284. ',
  285. '<?php
  286. class Bar
  287. {
  288. /** Important */
  289. public static $variable = "acme";
  290. }
  291. ',
  292. ],
  293. 'It can update doc blocks of properties that use the var keyword instead of public' => [
  294. '<?php
  295. class Bar
  296. {
  297. /**
  298. * Important
  299. */
  300. var $variable = "acme";
  301. }
  302. ',
  303. '<?php
  304. class Bar
  305. {
  306. /** Important */
  307. var $variable = "acme";
  308. }
  309. ',
  310. ],
  311. 'It can update doc blocks of static that do not declare visibility' => [
  312. '<?php
  313. class Bar
  314. {
  315. /**
  316. * Important
  317. */
  318. static $variable = "acme";
  319. }
  320. ',
  321. '<?php
  322. class Bar
  323. {
  324. /** Important */
  325. static $variable = "acme";
  326. }
  327. ',
  328. ],
  329. ];
  330. }
  331. /**
  332. * @requires PHP 7.1
  333. * @dataProvider provideFix71Cases
  334. *
  335. * @param string $expected
  336. * @param string $input
  337. */
  338. public function testFix71($expected, $input = null, array $config = [])
  339. {
  340. $this->fixer->configure($config);
  341. $this->doTest($expected, $input);
  342. }
  343. public function provideFix71Cases()
  344. {
  345. return [
  346. 'It can handle constants with visibility' => [
  347. '<?php
  348. class Foo
  349. {
  350. /**
  351. *
  352. */
  353. public const FOO = "foobar";
  354. /** */
  355. private $foo;
  356. }',
  357. '<?php
  358. class Foo
  359. {
  360. /** */
  361. public const FOO = "foobar";
  362. /**
  363. *
  364. */
  365. private $foo;
  366. }',
  367. [
  368. 'property' => 'single',
  369. ],
  370. ],
  371. ];
  372. }
  373. /**
  374. * @requires PHP 7.4
  375. * @dataProvider provideFixPhp74Cases
  376. *
  377. * @param string $expected
  378. * @param string $input
  379. */
  380. public function testFixPhp74($expected, $input = null, array $config = [])
  381. {
  382. $this->fixer->configure($config);
  383. $this->doTest($expected, $input);
  384. }
  385. public function provideFixPhp74Cases()
  386. {
  387. return [
  388. 'It can handle properties with type declaration' => [
  389. '<?php
  390. class Foo
  391. {
  392. /** */
  393. private ?string $foo;
  394. }',
  395. '<?php
  396. class Foo
  397. {
  398. /**
  399. *
  400. */
  401. private ?string $foo;
  402. }',
  403. [
  404. 'property' => 'single',
  405. ],
  406. ],
  407. ];
  408. }
  409. }