SingleLineThrowFixerTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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\FunctionNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer>
  20. */
  21. final class SingleLineThrowFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield ['<?php throw new Exception; foo(
  36. "Foo"
  37. );'];
  38. yield ['<?php throw new $exceptionName; foo(
  39. "Foo"
  40. );'];
  41. yield ['<?php throw $exception; foo(
  42. "Foo"
  43. );'];
  44. yield ['<?php throw new Exception("Foo.", 0);'];
  45. yield [
  46. '<?php throw new Exception("Foo.", 0);',
  47. '<?php throw new Exception(
  48. "Foo.",
  49. 0
  50. );',
  51. ];
  52. yield [
  53. '<?php throw new Exception("Foo." . "Bar");',
  54. '<?php throw new Exception(
  55. "Foo."
  56. .
  57. "Bar"
  58. );',
  59. ];
  60. yield [
  61. '<?php throw new Exception(new ExceptionReport("Foo"), 0);',
  62. '<?php throw new Exception(
  63. new
  64. ExceptionReport("Foo"),
  65. 0
  66. );',
  67. ];
  68. yield [
  69. '<?php throw new Exception(sprintf(\'Error with number "%s".\', 42));',
  70. '<?php throw new Exception(sprintf(
  71. \'Error with number "%s".\',
  72. 42
  73. ));',
  74. ];
  75. yield [
  76. '<?php throw new SomeVendor\Exception("Foo.");',
  77. '<?php throw new SomeVendor\Exception(
  78. "Foo."
  79. );',
  80. ];
  81. yield [
  82. '<?php throw new \SomeVendor\Exception("Foo.");',
  83. '<?php throw new \SomeVendor\Exception(
  84. "Foo."
  85. );',
  86. ];
  87. yield [
  88. '<?php throw $this->exceptionFactory->createAnException("Foo");',
  89. '<?php throw $this
  90. ->exceptionFactory
  91. ->createAnException(
  92. "Foo"
  93. );',
  94. ];
  95. yield [
  96. '<?php throw $this->getExceptionFactory()->createAnException("Foo");',
  97. '<?php throw $this
  98. ->getExceptionFactory()
  99. ->createAnException(
  100. "Foo"
  101. );',
  102. ];
  103. yield [
  104. '<?php throw $this->getExceptionFactory()->createAnException(function ($x, $y) { return $x === $y + 2; });',
  105. '<?php throw $this
  106. ->getExceptionFactory()
  107. ->createAnException(
  108. function
  109. (
  110. $x,
  111. $y
  112. )
  113. {
  114. return $x === $y + 2
  115. ;
  116. }
  117. );',
  118. ];
  119. yield [
  120. '<?php throw ExceptionFactory::createAnException("Foo");',
  121. '<?php throw ExceptionFactory
  122. ::
  123. createAnException(
  124. "Foo"
  125. );',
  126. ];
  127. yield [
  128. '<?php throw new Exception("Foo.", 0);',
  129. '<?php throw
  130. new
  131. Exception
  132. (
  133. "Foo."
  134. ,
  135. 0
  136. );',
  137. ];
  138. yield [
  139. '<?php throw new $exceptionName("Foo.");',
  140. '<?php throw new $exceptionName(
  141. "Foo."
  142. );',
  143. ];
  144. yield [
  145. '<?php throw new $exceptions[4];',
  146. '<?php throw new $exceptions[
  147. 4
  148. ];',
  149. ];
  150. yield [
  151. '<?php throw clone $exceptionName("Foo.");',
  152. '<?php throw clone $exceptionName(
  153. "Foo."
  154. );',
  155. ];
  156. yield [
  157. '<?php throw new WeirdException("Foo.", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
  158. '<?php throw new WeirdException("Foo.", -20, "An elephant",
  159. 1,
  160. 2,
  161. 3, 4, 5, 6, 7, 8
  162. );',
  163. ];
  164. yield [
  165. '<?php
  166. if ($foo) {
  167. throw new Exception("It is foo.", 1);
  168. } else {
  169. throw new \Exception("It is not foo.", 0);
  170. }
  171. ',
  172. '<?php
  173. if ($foo) {
  174. throw new Exception(
  175. "It is foo.",
  176. 1
  177. );
  178. } else {
  179. throw new \Exception(
  180. "It is not foo.", 0
  181. );
  182. }
  183. ',
  184. ];
  185. yield [
  186. '<?php throw new Exception( /* A */"Foo", /* 1 */0 /* 2 */); //3',
  187. '<?php throw new Exception( // A
  188. "Foo", // 1
  189. 0 // 2
  190. ); //3',
  191. ];
  192. yield [
  193. '<?php throw new Exception( /* 0123 */ "Foo", /* 1 */0 /* 2 */); //3',
  194. '<?php throw new Exception( /* 0123 */
  195. "Foo", // 1
  196. 0 // 2
  197. ); //3',
  198. ];
  199. yield [
  200. '<?php throw new Exception( /* X */ "Foo", /* 1 */0 /* 2 */); //3',
  201. '<?php throw new Exception( /* X
  202. */
  203. "Foo", // 1
  204. 0 // 2
  205. ); //3',
  206. ];
  207. yield [
  208. '<?php throw new Exception( 0, /* 1 2 3 */ /*4*/ "Foo", /*5*/ /*6*/0 /*7*/);',
  209. '<?php throw new Exception( 0, /*
  210. 1
  211. 2
  212. 3
  213. */
  214. /*4*/ "Foo", /*5*/
  215. /*6*/0 /*7*/);',
  216. ];
  217. yield [
  218. '<?php throw new Exception( /* 0 */${"Foo" /* a */}, /*b */fnx/*c */(/*d */0/*e */)/*f */);',
  219. '<?php throw new Exception( // 0
  220. ${"Foo"
  221. # a
  222. }, #b
  223. fnx#c
  224. (#d
  225. 0#e
  226. )#f
  227. );',
  228. ];
  229. yield [
  230. "<?php throw new Exception('Message.'. 1);",
  231. "<?php throw new Exception('Message.'.
  232. 1
  233. );",
  234. ];
  235. yield [
  236. '<?php throw new class() extends Exception
  237. {
  238. protected $message = "Custom message";
  239. }
  240. ;',
  241. '<?php throw
  242. new class()
  243. extends Exception
  244. {
  245. protected $message = "Custom message";
  246. }
  247. ;',
  248. ];
  249. yield [
  250. '<?php throw new class extends Exception
  251. {
  252. protected $message = "Custom message";
  253. }
  254. ;',
  255. '<?php throw
  256. new class
  257. extends Exception
  258. {
  259. protected $message = "Custom message";
  260. }
  261. ;',
  262. ];
  263. yield [
  264. '<?php throw new Exception("Foo.", 0)?>',
  265. '<?php throw new Exception(
  266. "Foo.",
  267. 0
  268. )?>',
  269. ];
  270. }
  271. /**
  272. * @dataProvider provideFix80Cases
  273. *
  274. * @requires PHP 8.0
  275. */
  276. public function testFix80(string $expected, ?string $input = null): void
  277. {
  278. $this->doTest($expected, $input);
  279. }
  280. /**
  281. * @return iterable<array{0: string, 1?: string}>
  282. */
  283. public static function provideFix80Cases(): iterable
  284. {
  285. yield [
  286. '<?php throw $this?->getExceptionFactory()?->createAnException("Foo");',
  287. '<?php throw $this
  288. ?->getExceptionFactory()
  289. ?->createAnException(
  290. "Foo"
  291. );',
  292. ];
  293. yield [
  294. '<?php
  295. match ($number) {
  296. 1 => $function->one(),
  297. 2 => $function->two(),
  298. default => throw new \NotOneOrTwo()
  299. };
  300. ',
  301. ];
  302. yield [
  303. '<?php
  304. match ($number) {
  305. 1 => $function->one(),
  306. 2 => throw new Exception("Number 2 is not allowed."),
  307. 1 => $function->three(),
  308. default => throw new \NotOneOrTwo()
  309. };
  310. ',
  311. ];
  312. yield [
  313. '<?php throw new Exception(match ($a) { 1 => "a", 3 => "b" });',
  314. '<?php throw new Exception(match ($a) {
  315. 1 => "a",
  316. 3 => "b"
  317. });',
  318. ];
  319. yield [
  320. '<?php
  321. $var = [
  322. $something[1] ?? throw new Exception(123)
  323. ];
  324. ',
  325. '<?php
  326. $var = [
  327. $something[1] ?? throw new Exception(
  328. 123
  329. )
  330. ];
  331. ',
  332. ];
  333. yield [
  334. '<?php
  335. $var = [
  336. $something[1] ?? throw new Exception()
  337. ];
  338. ',
  339. ];
  340. }
  341. }