Skip to content

Commit

Permalink
bug: Fix tokenizing of type hints (#7054)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum committed Jun 13, 2023
1 parent 7b7f77f commit 0f1a74f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/Tokenizer/AbstractTypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,17 @@ private function isPartOfType(Tokens $tokens, int $index): bool

$beforeVariableIndex = $tokens->getPrevMeaningfulToken($afterTypeIndex);
if ($tokens[$beforeVariableIndex]->equals('&')) {
$prevIndex = $tokens->getPrevTokenOfKind($index, ['{', '}', ';', [T_FN], [T_FUNCTION]]);
$prevIndex = $tokens->getPrevTokenOfKind(
$index,
[
'{',
'}',
';',
[T_CLOSE_TAG],
[T_FN],
[T_FUNCTION],
],
);

return null !== $prevIndex && $tokens[$prevIndex]->isGivenKind([T_FN, T_FUNCTION]);
}
Expand Down
31 changes: 29 additions & 2 deletions tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
final class TypeAlternationTransformerTest extends AbstractTransformerTestCase
{
/**
* @param array<int, int> $expectedTokens
* @param array<int, int|string> $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
public function testProcess(string $source, array $expectedTokens): void
{
$this->doTest(
$source,
Expand Down Expand Up @@ -86,7 +86,34 @@ public static function provideProcessCases(): iterable
$x = ($y|$z);
function foo(){}
$a = $b|$c;
const A = B|C;
const B = D::X|C ?>
',
[
6 => '|',
15 => '|',
24 => '|',
35 => '|',
52 => '|',
76 => '|',
94 => '|',
105 => '|',
118 => '|',
],
],
'do not fix, close/open' => [
'<?php fn() => 0 ?><?php $a = FOO|BAR|BAZ&$x;',
[
16 => '|',
18 => '|',
],
],
'do not fix, foreach' => [
'<?php while(foo()){} $a = FOO|BAR|BAZ&$x;',
[
15 => '|',
17 => '|',
],
],
];
}
Expand Down
18 changes: 17 additions & 1 deletion tests/Tokenizer/Transformer/TypeIntersectionTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
final class TypeIntersectionTransformerTest extends AbstractTransformerTestCase
{
/**
* @param array<int, int> $expectedTokens
* @param array<int, int|string> $expectedTokens
*
* @dataProvider provideProcessCases
*
Expand Down Expand Up @@ -58,6 +58,8 @@ public static function provideProcessCases(): iterable
function foo(){}
$a = $b&$c;
$a &+ $b;
const A1 = B&C;
const B1 = D::X & C;
',
];

Expand All @@ -68,6 +70,20 @@ function foo(){}
6 => T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];

yield 'do not fix, close/open' => [
'<?php fn() => 0 ?><?php $a = FOO|BAR|BAZ&$x;',
[
20 => T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];

yield 'do not fix, foreach' => [
'<?php while(foo()){} $a = FOO|BAR|BAZ&$x;',
[
19 => T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];
}

yield 'arrow function' => [
Expand Down

0 comments on commit 0f1a74f

Please sign in to comment.