Skip to content

Commit

Permalink
Handle anonymous functions in the type hint order fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Jan 29, 2024
1 parent b80439d commit 3d84859
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Fixer/TypeHintOrderFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct(

public function isCandidate(Tokens $tokens): bool
{
return $tokens->isAnyTokenKindsFound([T_PUBLIC, T_PROTECTED, T_PRIVATE]);
return $tokens->isAnyTokenKindsFound([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FUNCTION, T_FN]);
}

/**
Expand All @@ -75,7 +75,13 @@ public function getPriority(): int
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 1, $count = \count($tokens); $index < $count; ++$index) {
if (!$tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE])) {
if (!$tokens[$index]->isGivenKind([T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FUNCTION, T_FN])) {
continue;
}

// Anonymous functions
if ($tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
$index = $this->handleFunction($tokens, $index);
continue;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Fixer/TypeHintOrderFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public function __construct(
public function bar(object|FooService|BarService $service, iterable|int $count): null|string|int
{
$foo = function (string|int $id): ?FooService {
};
$foo = function (string|int $id) use ($count): ?FooService {
};
$bar = fn (string|int $id): ?FooService => null;
}
}
EOT,
Expand All @@ -73,6 +80,13 @@ public function __construct(
public function bar(BarService|FooService|object $service, int|iterable $count): int|string|null
{
$foo = function (int|string $id): FooService|null {
};
$foo = function (int|string $id) use ($count): FooService|null {
};
$bar = fn (int|string $id): FooService|null => null;
}
}
EOT,
Expand Down

0 comments on commit 3d84859

Please sign in to comment.