Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Fix PHPDoc alignment fixer containing callbacks using \Closure #6746

Merged
merged 4 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DocBlock/TypeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class TypeExpression
)
|
(?<callable> # callable syntax, e.g. `callable(string): bool`
(?<callable_start>(?:callable|Closure)\h*\(\h*)
(?<callable_start>(?:callable|\\\\?Closure)\h*\(\h*)
(?<callable_arguments>
(?&types)
(?:
Expand Down
2 changes: 1 addition & 1 deletion tests/Console/Output/ProcessOutputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public function testWakeup(): void

/**
* @param list<array{0: FixerFileProcessedEvent::STATUS_*, 1?: int}> $statuses
* @param \Closure(FixerFileProcessedEvent::STATUS_*): void $action
* @param \Closure(FixerFileProcessedEvent::STATUS_*): void $action
*/
private function foreachStatus(array $statuses, \Closure $action): void
{
Expand Down
16 changes: 16 additions & 0 deletions tests/DocBlock/TypeExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public static function provideGetTypesCases(): iterable

yield ['Closure(string)', ['Closure(string)']];

yield ['\\Closure', ['\\Closure']];

yield ['\\Closure()', ['\\Closure()']];

yield ['\\Closure(string)', ['\\Closure(string)']];

yield ['\\Closure(string, bool)', ['\\Closure(string, bool)']];

yield ['\\Closure(string|int, bool)', ['\\Closure(string|int, bool)']];

yield ['\\Closure(string):bool', ['\\Closure(string):bool']];

yield ['\\Closure(string): bool', ['\\Closure(string): bool']];

yield ['\\Closure(string|int, bool): bool', ['\\Closure(string|int, bool): bool']];

yield ['array < int , callable ( string ) : bool >', ['array < int , callable ( string ) : bool >']];
}

Expand Down
85 changes: 85 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1454,4 +1454,89 @@ public function testTypesContainingWhitespace(): void
*/
');
}

public function testClosureTypesContainingBackslash(): void
{
$this->doTest('<?php
/**
* @var string $input
* @var \Closure $fn
cs278 marked this conversation as resolved.
Show resolved Hide resolved
* @var \Closure(bool):int $fn2
* @var Closure $fn3
* @var Closure(string):string $fn4
* @var array<string,array<string,mixed>> $data
*/
/**
* @param string $input
* @param \Closure $fn
* @param \Closure(bool):int $fn2
* @param Closure $fn3
* @param Closure(string):string $fn4
* @param array<string,array<string,mixed>> $data
*/
/**
* @var string $value
* @var \Closure(string): string $callback
* @var Closure(int): bool $callback2
*/
/**
* @param string $value
* @param \Closure(string): string $callback
* @param Closure(int): bool $callback2
*/
/**
* @var Closure(array<int,bool>): bool $callback1
* @var \Closure(string): string $callback2
*/
/**
cs278 marked this conversation as resolved.
Show resolved Hide resolved
* @param Closure(array<int,bool>): bool $callback1
* @param \Closure(string): string $callback2
cs278 marked this conversation as resolved.
Show resolved Hide resolved
*/
');
}

/**
* @dataProvider provideCallableTypesWithUglyCodeCases
*/
public function testCallableTypesWithUglyCode(string $input): void
{
$this->doTest(<<<'EOT'
<?php
/**
* @var callable $fn
* @var callable(bool): int $fn2
* @var Closure $fn3
* @var Closure(string|object):string $fn4
* @var \Closure $fn5
* @var \Closure(int, bool): bool $fn6
*/
EOT, $input);
}

public function provideCallableTypesWithUglyCodeCases(): iterable
{
yield [<<<'EOT'
<?php
/**
* @var callable $fn
* @var callable(bool): int $fn2
* @var Closure $fn3
* @var Closure(string|object):string $fn4
* @var \Closure $fn5
* @var \Closure(int, bool): bool $fn6
*/
EOT];

yield [<<<'EOT'
<?php
/**
* @var callable $fn
* @var callable(bool): int $fn2
* @var Closure $fn3
* @var Closure(string|object):string $fn4
* @var \Closure $fn5
* @var \Closure(int, bool): bool $fn6
*/
EOT];
}
}