SingleLineThrowFixerTest.php 8.9 KB

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