123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Tokenizer\Transformer;
- use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
- use PhpCsFixer\Tokenizer\CT;
- /**
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- *
- * @covers \PhpCsFixer\Tokenizer\Transformer\UseTransformer
- *
- * @phpstan-import-type _TransformerTestExpectedTokens from AbstractTransformerTestCase
- * @phpstan-import-type _TransformerTestObservedKindsOrPrototypes from AbstractTransformerTestCase
- */
- final class UseTransformerTest extends AbstractTransformerTestCase
- {
- /**
- * @param _TransformerTestExpectedTokens $expectedTokens
- *
- * @dataProvider provideProcessCases
- */
- public function testProcess(string $source, array $expectedTokens = []): void
- {
- $this->doTest(
- $source,
- $expectedTokens,
- [
- T_USE,
- CT::T_USE_LAMBDA,
- CT::T_USE_TRAIT,
- ]
- );
- }
- /**
- * @return iterable<array{string, _TransformerTestExpectedTokens}>
- */
- public static function provideProcessCases(): iterable
- {
- yield [
- '<?php use Foo;',
- [
- 1 => T_USE,
- ],
- ];
- yield [
- '<?php $foo = function() use ($bar) {};',
- [
- 9 => CT::T_USE_LAMBDA,
- ],
- ];
- yield [
- '<?php class Foo { use Bar; }',
- [
- 7 => CT::T_USE_TRAIT,
- ],
- ];
- yield [
- '<?php namespace Aaa; use Bbb; class Foo { use Bar; function baz() { $a=1; return function () use ($a) {}; } }',
- [
- 6 => T_USE,
- 17 => CT::T_USE_TRAIT,
- 42 => CT::T_USE_LAMBDA,
- ],
- ];
- yield [
- '<?php
- namespace A {
- class Foo {}
- echo Foo::class;
- }
- namespace B {
- use \stdClass;
- echo 123;
- }',
- [
- 30 => T_USE,
- ],
- ];
- yield [
- '<?php use Foo; $a = Bar::class;',
- [
- 1 => T_USE,
- ],
- ];
- yield 'nested anonymous classes' => [
- '<?php
- namespace SomeWhereOverTheRainbow;
- trait Foo {
- public function test()
- {
- $a = time();
- return function() use ($a) { echo $a; };
- }
- };
- $a = new class(
- new class() {
- use Foo;
- }
- ) {
- public function __construct($bar)
- {
- $a = $bar->test();
- $a();
- }
- };
- ',
- [
- 38 => CT::T_USE_LAMBDA,
- 76 => CT::T_USE_TRAIT,
- ],
- ];
- yield [
- '<?php
- use A\{B,};
- use function D;
- use C\{D,E,};
- ',
- [
- 1 => T_USE,
- 11 => T_USE,
- 18 => T_USE,
- ],
- ];
- }
- /**
- * @param _TransformerTestExpectedTokens $expectedTokens
- *
- * @requires PHP 8.1
- *
- * @dataProvider provideProcessPhp81Cases
- */
- public function testProcessPhp81(string $source, array $expectedTokens = []): void
- {
- $this->doTest($source, $expectedTokens, [CT::T_USE_TRAIT]);
- }
- /**
- * @return iterable<array{string, _TransformerTestExpectedTokens}>
- */
- public static function provideProcessPhp81Cases(): iterable
- {
- yield [
- '<?php enum Foo: string
- {
- use Bar;
- case Test1 = "a";
- }
- ',
- [
- 10 => CT::T_USE_TRAIT,
- ],
- ];
- }
- }
|