Skip to content

Commit

Permalink
bug #6159 ImportTransformer - fix for grouped constant and function i…
Browse files Browse the repository at this point in the history
…mports (kubawerlos)

This PR was squashed before being merged into the master branch.

Discussion
----------

ImportTransformer - fix for grouped constant and function imports

Commits
-------

bf32365 ImportTransformer - fix for grouped constant and function imports
  • Loading branch information
keradus committed Dec 11, 2021
2 parents eebb66a + bf32365 commit 12e3ae9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Fixer/Import/SingleImportPerStatementFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ private function getGroupStatements(Tokens $tokens, string $groupPrefix, int $gr
if ($tokens[$j]->equals([T_AS])) {
$statement .= ' as ';
$i += 2;
} elseif ($tokens[$j]->equals([T_FUNCTION])) {
} elseif ($tokens[$j]->isGivenKind(CT::T_FUNCTION_IMPORT)) {
$statement = ' function'.$statement;
$i += 2;
} elseif ($tokens[$j]->equals([T_CONST])) {
} elseif ($tokens[$j]->isGivenKind(CT::T_CONST_IMPORT)) {
$statement = ' const'.$statement;
$i += 2;
}
Expand Down
25 changes: 20 additions & 5 deletions src/Tokenizer/Transformer/ImportTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
*/
final class ImportTransformer extends AbstractTransformer
{
/**
* {@inheritdoc}
*/
public function getPriority(): int
{
// Should run after CurlyBraceTransformer and ReturnRefTransformer
return -1;
}

/**
* {@inheritdoc}
*/
Expand All @@ -51,12 +60,18 @@ public function process(Tokens $tokens, Token $token, int $index): void

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

if ($prevToken->isGivenKind(T_USE)) {
$tokens[$index] = new Token([
$token->isGivenKind(T_FUNCTION) ? CT::T_FUNCTION_IMPORT : CT::T_CONST_IMPORT,
$token->getContent(),
]);
if (!$prevToken->isGivenKind(T_USE)) {
$nextToken = $tokens[$tokens->getNextTokenOfKind($index, ['=', '(', [CT::T_RETURN_REF], [CT::T_GROUP_IMPORT_BRACE_CLOSE]])];

if (!$nextToken->isGivenKind(CT::T_GROUP_IMPORT_BRACE_CLOSE)) {
return;
}
}

$tokens[$index] = new Token([
$token->isGivenKind(T_FUNCTION) ? CT::T_FUNCTION_IMPORT : CT::T_CONST_IMPORT,
$token->getContent(),
]);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/AutoReview/TransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public function provideTransformerPriorityCases(): array
[$transformers['attribute'], $transformers['curly_brace']],
[$transformers['attribute'], $transformers['square_brace']],
[$transformers['curly_brace'], $transformers['brace_class_instantiation']],
[$transformers['curly_brace'], $transformers['import']],
[$transformers['curly_brace'], $transformers['use']],
[$transformers['name_qualified'], $transformers['namespace_operator']],
[$transformers['return_ref'], $transformers['import']],
[$transformers['return_ref'], $transformers['type_colon']],
[$transformers['square_brace'], $transformers['brace_class_instantiation']],
[$transformers['type_colon'], $transformers['named_argument']],
Expand Down
1 change: 1 addition & 0 deletions tests/Fixer/FunctionNotation/VoidReturnFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function provideFixCases(): array
['<?php interface Test { public function foo($param); }'],
['<?php function foo($param) { return function($a) use ($param): string {}; }'],
['<?php abstract class Test { abstract public function foo($param); }'],
['<?php use Foo\ { function Bar }; function test() { return Bar(); }'],
['<?php
/**
* @return array
Expand Down
30 changes: 30 additions & 0 deletions tests/Tokenizer/Transformer/ImportTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,42 @@ public function provideProcessCases(): array
7 => T_FUNCTION,
],
],
[
'<?php function & foo() {}',
[
1 => T_FUNCTION,
],
],
[
'<?php use function Foo\\bar;',
[
3 => CT::T_FUNCTION_IMPORT,
],
],
[
'<?php use Foo\ { function Bar };',
[
8 => CT::T_FUNCTION_IMPORT,
],
],
[
'<?php use Foo\ {
function F1,
const Constants\C1,
function Functions\F2,
const C2,
function F3,
const C3,
};',
[
8 => CT::T_FUNCTION_IMPORT,
13 => CT::T_CONST_IMPORT,
20 => CT::T_FUNCTION_IMPORT,
27 => CT::T_CONST_IMPORT,
32 => CT::T_FUNCTION_IMPORT,
37 => CT::T_CONST_IMPORT,
],
],
];
}
}

0 comments on commit 12e3ae9

Please sign in to comment.