UseTransformerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Tokenizer\Transformer;
  13. use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
  14. use PhpCsFixer\Tokenizer\CT;
  15. /**
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Tokenizer\Transformer\UseTransformer
  21. *
  22. * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
  23. * @phpstan-import-type _TransformerTestObservedKindsOrPrototypes from AbstractTransformerTestCase
  24. */
  25. final class UseTransformerTest extends AbstractTransformerTestCase
  26. {
  27. /**
  28. * @param _TransformerTestExpectedTokens $expectedTokens
  29. *
  30. * @dataProvider provideProcessCases
  31. */
  32. public function testProcess(string $source, array $expectedTokens = []): void
  33. {
  34. $this->doTest(
  35. $source,
  36. $expectedTokens,
  37. [
  38. T_USE,
  39. CT::T_USE_LAMBDA,
  40. CT::T_USE_TRAIT,
  41. ]
  42. );
  43. }
  44. /**
  45. * @return iterable<array{string, _TransformerTestExpectedTokens}>
  46. */
  47. public static function provideProcessCases(): iterable
  48. {
  49. yield [
  50. '<?php use Foo;',
  51. [
  52. 1 => T_USE,
  53. ],
  54. ];
  55. yield [
  56. '<?php $foo = function() use ($bar) {};',
  57. [
  58. 9 => CT::T_USE_LAMBDA,
  59. ],
  60. ];
  61. yield [
  62. '<?php class Foo { use Bar; }',
  63. [
  64. 7 => CT::T_USE_TRAIT,
  65. ],
  66. ];
  67. yield [
  68. '<?php namespace Aaa; use Bbb; class Foo { use Bar; function baz() { $a=1; return function () use ($a) {}; } }',
  69. [
  70. 6 => T_USE,
  71. 17 => CT::T_USE_TRAIT,
  72. 42 => CT::T_USE_LAMBDA,
  73. ],
  74. ];
  75. yield [
  76. '<?php
  77. namespace A {
  78. class Foo {}
  79. echo Foo::class;
  80. }
  81. namespace B {
  82. use \stdClass;
  83. echo 123;
  84. }',
  85. [
  86. 30 => T_USE,
  87. ],
  88. ];
  89. yield [
  90. '<?php use Foo; $a = Bar::class;',
  91. [
  92. 1 => T_USE,
  93. ],
  94. ];
  95. yield 'nested anonymous classes' => [
  96. '<?php
  97. namespace SomeWhereOverTheRainbow;
  98. trait Foo {
  99. public function test()
  100. {
  101. $a = time();
  102. return function() use ($a) { echo $a; };
  103. }
  104. };
  105. $a = new class(
  106. new class() {
  107. use Foo;
  108. }
  109. ) {
  110. public function __construct($bar)
  111. {
  112. $a = $bar->test();
  113. $a();
  114. }
  115. };
  116. ',
  117. [
  118. 38 => CT::T_USE_LAMBDA,
  119. 76 => CT::T_USE_TRAIT,
  120. ],
  121. ];
  122. yield [
  123. '<?php
  124. use A\{B,};
  125. use function D;
  126. use C\{D,E,};
  127. ',
  128. [
  129. 1 => T_USE,
  130. 11 => T_USE,
  131. 18 => T_USE,
  132. ],
  133. ];
  134. }
  135. /**
  136. * @param _TransformerTestExpectedTokens $expectedTokens
  137. *
  138. * @requires PHP 8.1
  139. *
  140. * @dataProvider provideProcessPhp81Cases
  141. */
  142. public function testProcessPhp81(string $source, array $expectedTokens = []): void
  143. {
  144. $this->doTest($source, $expectedTokens, [CT::T_USE_TRAIT]);
  145. }
  146. /**
  147. * @return iterable<array{string, _TransformerTestExpectedTokens}>
  148. */
  149. public static function provideProcessPhp81Cases(): iterable
  150. {
  151. yield [
  152. '<?php enum Foo: string
  153. {
  154. use Bar;
  155. case Test1 = "a";
  156. }
  157. ',
  158. [
  159. 10 => CT::T_USE_TRAIT,
  160. ],
  161. ];
  162. }
  163. }