SingleLineThrowFixerTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 function provideFixCases(): \Generator
  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. if (\PHP_VERSION_ID >= 70000) {
  231. yield [
  232. '<?php throw new class() extends Exception
  233. {
  234. protected $message = "Custom message";
  235. }
  236. ;',
  237. '<?php throw
  238. new class()
  239. extends Exception
  240. {
  241. protected $message = "Custom message";
  242. }
  243. ;',
  244. ];
  245. yield [
  246. '<?php throw new class extends Exception
  247. {
  248. protected $message = "Custom message";
  249. }
  250. ;',
  251. '<?php throw
  252. new class
  253. extends Exception
  254. {
  255. protected $message = "Custom message";
  256. }
  257. ;',
  258. ];
  259. }
  260. }
  261. /**
  262. * @dataProvider provideFix80Cases
  263. * @requires PHP 8.0
  264. */
  265. public function testFix80(string $expected, ?string $input = null): void
  266. {
  267. $this->doTest($expected, $input);
  268. }
  269. public static function provideFix80Cases(): \Generator
  270. {
  271. yield [
  272. '<?php throw $this?->getExceptionFactory()?->createAnException("Foo");',
  273. '<?php throw $this
  274. ?->getExceptionFactory()
  275. ?->createAnException(
  276. "Foo"
  277. );',
  278. ];
  279. yield [
  280. '<?php
  281. match ($number) {
  282. 1 => $function->one(),
  283. 2 => $function->two(),
  284. default => throw new \NotOneOrTwo()
  285. };
  286. ',
  287. ];
  288. yield [
  289. '<?php
  290. match ($number) {
  291. 1 => $function->one(),
  292. 2 => throw new Exception("Number 2 is not allowed."),
  293. 1 => $function->three(),
  294. default => throw new \NotOneOrTwo()
  295. };
  296. ',
  297. ];
  298. yield [
  299. '<?php throw new Exception(match ($a) { 1 => "a", 3 => "b" });',
  300. '<?php throw new Exception(match ($a) {
  301. 1 => "a",
  302. 3 => "b"
  303. });',
  304. ];
  305. }
  306. }