PhpdocLineSpanFixerTest.php 8.6 KB

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