Skip to content

Commit bf32365

Browse files
kubawerloskeradus
authored andcommitted
ImportTransformer - fix for grouped constant and function imports
1 parent 3de1d1b commit bf32365

File tree

5 files changed

+55
-7
lines changed

5 files changed

+55
-7
lines changed

src/Fixer/Import/SingleImportPerStatementFixer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ private function getGroupStatements(Tokens $tokens, string $groupPrefix, int $gr
147147
if ($tokens[$j]->equals([T_AS])) {
148148
$statement .= ' as ';
149149
$i += 2;
150-
} elseif ($tokens[$j]->equals([T_FUNCTION])) {
150+
} elseif ($tokens[$j]->isGivenKind(CT::T_FUNCTION_IMPORT)) {
151151
$statement = ' function'.$statement;
152152
$i += 2;
153-
} elseif ($tokens[$j]->equals([T_CONST])) {
153+
} elseif ($tokens[$j]->isGivenKind(CT::T_CONST_IMPORT)) {
154154
$statement = ' const'.$statement;
155155
$i += 2;
156156
}

src/Tokenizer/Transformer/ImportTransformer.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
*/
3333
final class ImportTransformer extends AbstractTransformer
3434
{
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getPriority(): int
39+
{
40+
// Should run after CurlyBraceTransformer and ReturnRefTransformer
41+
return -1;
42+
}
43+
3544
/**
3645
* {@inheritdoc}
3746
*/
@@ -51,12 +60,18 @@ public function process(Tokens $tokens, Token $token, int $index): void
5160

5261
$prevToken = $tokens[$tokens->getPrevMeaningfulToken($index)];
5362

54-
if ($prevToken->isGivenKind(T_USE)) {
55-
$tokens[$index] = new Token([
56-
$token->isGivenKind(T_FUNCTION) ? CT::T_FUNCTION_IMPORT : CT::T_CONST_IMPORT,
57-
$token->getContent(),
58-
]);
63+
if (!$prevToken->isGivenKind(T_USE)) {
64+
$nextToken = $tokens[$tokens->getNextTokenOfKind($index, ['=', '(', [CT::T_RETURN_REF], [CT::T_GROUP_IMPORT_BRACE_CLOSE]])];
65+
66+
if (!$nextToken->isGivenKind(CT::T_GROUP_IMPORT_BRACE_CLOSE)) {
67+
return;
68+
}
5969
}
70+
71+
$tokens[$index] = new Token([
72+
$token->isGivenKind(T_FUNCTION) ? CT::T_FUNCTION_IMPORT : CT::T_CONST_IMPORT,
73+
$token->getContent(),
74+
]);
6075
}
6176

6277
/**

tests/AutoReview/TransformerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ public function provideTransformerPriorityCases(): array
8181
[$transformers['attribute'], $transformers['curly_brace']],
8282
[$transformers['attribute'], $transformers['square_brace']],
8383
[$transformers['curly_brace'], $transformers['brace_class_instantiation']],
84+
[$transformers['curly_brace'], $transformers['import']],
8485
[$transformers['curly_brace'], $transformers['use']],
8586
[$transformers['name_qualified'], $transformers['namespace_operator']],
87+
[$transformers['return_ref'], $transformers['import']],
8688
[$transformers['return_ref'], $transformers['type_colon']],
8789
[$transformers['square_brace'], $transformers['brace_class_instantiation']],
8890
[$transformers['type_colon'], $transformers['named_argument']],

tests/Fixer/FunctionNotation/VoidReturnFixerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function provideFixCases(): array
4949
['<?php interface Test { public function foo($param); }'],
5050
['<?php function foo($param) { return function($a) use ($param): string {}; }'],
5151
['<?php abstract class Test { abstract public function foo($param); }'],
52+
['<?php use Foo\ { function Bar }; function test() { return Bar(); }'],
5253
['<?php
5354
/**
5455
* @return array

tests/Tokenizer/Transformer/ImportTransformerTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,42 @@ public function provideProcessCases(): array
8888
7 => T_FUNCTION,
8989
],
9090
],
91+
[
92+
'<?php function & foo() {}',
93+
[
94+
1 => T_FUNCTION,
95+
],
96+
],
9197
[
9298
'<?php use function Foo\\bar;',
9399
[
94100
3 => CT::T_FUNCTION_IMPORT,
95101
],
96102
],
103+
[
104+
'<?php use Foo\ { function Bar };',
105+
[
106+
8 => CT::T_FUNCTION_IMPORT,
107+
],
108+
],
109+
[
110+
'<?php use Foo\ {
111+
function F1,
112+
const Constants\C1,
113+
function Functions\F2,
114+
const C2,
115+
function F3,
116+
const C3,
117+
};',
118+
[
119+
8 => CT::T_FUNCTION_IMPORT,
120+
13 => CT::T_CONST_IMPORT,
121+
20 => CT::T_FUNCTION_IMPORT,
122+
27 => CT::T_CONST_IMPORT,
123+
32 => CT::T_FUNCTION_IMPORT,
124+
37 => CT::T_CONST_IMPORT,
125+
],
126+
],
97127
];
98128
}
99129
}

0 commit comments

Comments
 (0)