NoUnneededFinalMethodFixerTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ClassNotation\NoUnneededFinalMethodFixer
  20. */
  21. final class NoUnneededFinalMethodFixerTest 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. public function provideFixCases()
  31. {
  32. return [
  33. 'default' => [
  34. '<?php
  35. final class Foo {
  36. public function foo() {}
  37. protected function bar() {}
  38. private function baz() {}
  39. }',
  40. '<?php
  41. final class Foo {
  42. final public function foo() {}
  43. final protected function bar() {}
  44. final private function baz() {}
  45. }',
  46. ],
  47. 'final-after-visibility' => [
  48. '<?php
  49. final class Foo {
  50. public function foo() {}
  51. protected function bar() {}
  52. private function baz() {}
  53. }',
  54. '<?php
  55. final class Foo {
  56. public final function foo() {}
  57. protected final function bar() {}
  58. private final function baz() {}
  59. }',
  60. ],
  61. 'default-static' => [
  62. '<?php
  63. final class SomeClass {
  64. public static function foo() {}
  65. protected static function bar() {}
  66. private static function baz() {}
  67. }',
  68. '<?php
  69. final class SomeClass {
  70. final public static function foo() {}
  71. final protected static function bar() {}
  72. final private static function baz() {}
  73. }',
  74. ],
  75. 'visibility-then-final-then-static' => [
  76. '<?php
  77. final class SomeClass {
  78. public static function foo() {}
  79. protected static function bar() {}
  80. private static function baz() {}
  81. }',
  82. '<?php
  83. final class SomeClass {
  84. public final static function foo() {}
  85. protected final static function bar() {}
  86. private final static function baz() {}
  87. }',
  88. ],
  89. 'visibility-then-static-then-final' => [
  90. '<?php
  91. final class SomeClass {
  92. public static function foo() {}
  93. protected static function bar() {}
  94. private static function baz() {}
  95. }',
  96. '<?php
  97. final class SomeClass {
  98. public static final function foo() {}
  99. protected static final function bar() {}
  100. private static final function baz() {}
  101. }',
  102. ],
  103. 'static-then-visibility-then-final' => [
  104. '<?php
  105. final class SomeClass {
  106. static public function foo() {}
  107. static protected function bar() {}
  108. static private function baz() {}
  109. }',
  110. '<?php
  111. final class SomeClass {
  112. static public final function foo() {}
  113. static protected final function bar() {}
  114. static private final function baz() {}
  115. }',
  116. ],
  117. 'static-then-final-then-visibility' => [
  118. '<?php
  119. final class SomeClass {
  120. static public function foo() {}
  121. static protected function bar() {}
  122. static private function baz() {}
  123. }',
  124. '<?php
  125. final class SomeClass {
  126. static final public function foo() {}
  127. static final protected function bar() {}
  128. static final private function baz() {}
  129. }',
  130. ],
  131. 'no-visibility' => [
  132. '<?php
  133. final class Foo {
  134. function foo() {}
  135. function bar() {}
  136. function baz() {}
  137. }',
  138. '<?php
  139. final class Foo {
  140. final function foo() {}
  141. final function bar() {}
  142. final function baz() {}
  143. }',
  144. ],
  145. 'no-visibility-final-then-static' => [
  146. '<?php
  147. final class SomeClass {
  148. static function foo() {}
  149. static function bar() {}
  150. static function baz() {}
  151. }',
  152. '<?php
  153. final class SomeClass {
  154. final static function foo() {}
  155. final static function bar() {}
  156. final static function baz() {}
  157. }',
  158. ],
  159. 'no-visibility-static-then-final' => [
  160. '<?php
  161. final class SomeClass {
  162. static function foo() {}
  163. static function bar() {}
  164. static function baz() {}
  165. }',
  166. '<?php
  167. final class SomeClass {
  168. static final function foo() {}
  169. static final function bar() {}
  170. static final function baz() {}
  171. }',
  172. ],
  173. 'private-method' => [
  174. '<?php
  175. class Foo {
  176. private function bar() {}
  177. }',
  178. '<?php
  179. class Foo {
  180. final private function bar() {}
  181. }',
  182. ],
  183. 'private-method-with-visibility-before-final' => [
  184. '<?php
  185. class Foo {
  186. private function bar() {}
  187. }',
  188. '<?php
  189. class Foo {
  190. private final function bar() {}
  191. }',
  192. ],
  193. 'preserve-comment' => [
  194. '<?php final class Foo { /* comment */public function foo() {} }',
  195. '<?php final class Foo { final/* comment */public function foo() {} }',
  196. ],
  197. 'multiple-classes-per-file' => [
  198. '<?php final class Foo { public function foo() {} } abstract class Bar { final public function bar() {} }',
  199. '<?php final class Foo { final public function foo() {} } abstract class Bar { final public function bar() {} }',
  200. ],
  201. 'non-final' => [
  202. '<php class Foo { final public function foo() {} }',
  203. ],
  204. 'abstract-class' => [
  205. '<php abstract class Foo { final public function foo() {} }',
  206. ],
  207. 'final-method-with-private-attribute' => [
  208. '<?php abstract class Foo { private static $var; final public function foo() {} }',
  209. ],
  210. 'trait' => [
  211. '<php trait Foo { final public function foo() {} }',
  212. ],
  213. 'do not fix constructors' => [
  214. '<?php
  215. class Bar
  216. {
  217. final private function __construct()
  218. {
  219. }
  220. }',
  221. ],
  222. ];
  223. }
  224. /**
  225. * @requires PHP 7.0
  226. * @dataProvider providePhp70Cases
  227. */
  228. public function testFixPhp70(string $expected, ?string $input = null): void
  229. {
  230. $this->doTest($expected, $input);
  231. }
  232. public function providePhp70Cases()
  233. {
  234. return [
  235. 'anonymous-class-inside' => [
  236. '<?php
  237. final class Foo
  238. {
  239. public function foo()
  240. {
  241. }
  242. private function bar()
  243. {
  244. new class {
  245. final public function baz()
  246. {
  247. }
  248. };
  249. }
  250. }
  251. ',
  252. '<?php
  253. final class Foo
  254. {
  255. final public function foo()
  256. {
  257. }
  258. private function bar()
  259. {
  260. new class {
  261. final public function baz()
  262. {
  263. }
  264. };
  265. }
  266. }
  267. ',
  268. ],
  269. 'anonymous-class-inside-with-final-private-method' => [
  270. '<?php
  271. class Foo
  272. {
  273. private function bar()
  274. {
  275. new class {
  276. private function qux()
  277. {
  278. }
  279. };
  280. }
  281. }
  282. ',
  283. '<?php
  284. class Foo
  285. {
  286. private function bar()
  287. {
  288. new class {
  289. final private function qux()
  290. {
  291. }
  292. };
  293. }
  294. }
  295. ',
  296. ],
  297. ];
  298. }
  299. /**
  300. * @dataProvider provideFixConfigCases
  301. */
  302. public function testFixConfig(string $expected, string $input, array $config): void
  303. {
  304. $this->fixer->configure($config);
  305. $this->doTest($expected, $input);
  306. }
  307. public function provideFixConfigCases()
  308. {
  309. yield [
  310. '<?php
  311. final class Foo
  312. {
  313. private function baz() {}
  314. }
  315. class Bar
  316. {
  317. final private function bar1() {}
  318. }
  319. ',
  320. '<?php
  321. final class Foo
  322. {
  323. final private function baz() {}
  324. }
  325. class Bar
  326. {
  327. final private function bar1() {}
  328. }
  329. ',
  330. ['private_methods' => false],
  331. ];
  332. }
  333. }