UseTransformerTest.php 3.4 KB

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