diff --git a/packages/coding-standard/src/Fixer/Commenting/SwitchedTypeAndNameFixer.php b/packages/coding-standard/src/Fixer/Commenting/SwitchedTypeAndNameFixer.php index 59bd632ff7..dc6eeeda7b 100644 --- a/packages/coding-standard/src/Fixer/Commenting/SwitchedTypeAndNameFixer.php +++ b/packages/coding-standard/src/Fixer/Commenting/SwitchedTypeAndNameFixer.php @@ -21,7 +21,16 @@ final class SwitchedTypeAndNameFixer extends AbstractDocBlockFixer /** * @see https://regex101.com/r/4us32A/1 */ - private const string NAME_THEN_TYPE_REGEX = '#@((?:psalm-|phpstan-)?(?:param|var))(\s+)(?\$\w+)(\s+)(?(?:[|\\\\\w\[\]]|<[^<>]*>)+)#'; + private const string NAME_THEN_TYPE_REGEX = '#@((?:psalm-|phpstan-)?(?:param|var))(\s+)(?\$\w+)(\s+)(?(?:[|\\\\\w\[\]]|<[^<>]*>)+)(?\s+\S.*)?$#'; + + /** + * @var string[] + */ + private const array KNOWN_PRIMITIVE_TYPES = [ + 'string', 'int', 'integer', 'float', 'bool', 'boolean', 'array', 'object', 'callable', + 'iterable', 'mixed', 'void', 'null', 'false', 'true', 'self', 'static', 'parent', + 'resource', 'scalar', 'never', 'number', 'double', + ]; public function getDefinition(): FixerDefinitionInterface { @@ -51,15 +60,25 @@ protected function processDocContent(string $docContent, Tokens $tokens, int $po continue; } - // skip random words that look like type without autolaoding - if (in_array($match['type'], ['The', 'Set'], true)) { + // skip plain comment words mistaken for a type, e.g. "@param $a Can be used to..." + if (! $this->isKnownType($match['type'])) { continue; } - $newLine = Regex::replace($line->getContent(), self::NAME_THEN_TYPE_REGEX, '@$1$2$5$4$3'); + $newLine = Regex::replace($line->getContent(), self::NAME_THEN_TYPE_REGEX, '@$1$2$5$4$3$6'); $line->setContent($newLine); } return $docBlock->getContent(); } + + private function isKnownType(string $type): bool + { + // has type syntax: namespace, generics, array brackets or union + if (Regex::match($type, '#[\\\\\[\]<>|]#') !== null) { + return true; + } + + return in_array(strtolower($type), self::KNOWN_PRIMITIVE_TYPES, true); + } } diff --git a/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/skip_comment.php.inc b/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/skip_comment.php.inc new file mode 100644 index 0000000000..5dec1bc7be --- /dev/null +++ b/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/skip_comment.php.inc @@ -0,0 +1,8 @@ +|null + * @phpstan-param $y int + * @psalm-var $c list + * @var $z Foo\Bar + */ +function test($x, $y): void +{ +} + +?> +----- +|null $x + * @phpstan-param int $y + * @psalm-var list $c + * @var Foo\Bar $z + */ +function test($x, $y): void +{ +} + +?> diff --git a/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/wrong6.php.inc b/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/wrong6.php.inc new file mode 100644 index 0000000000..4ed825ee44 --- /dev/null +++ b/packages/coding-standard/tests/Fixer/Commenting/SwitchedTypeAndNameFixer/Fixture/wrong6.php.inc @@ -0,0 +1,23 @@ + mapped items + */ +function test($a, $b): void +{ +} + +?> +----- + $b mapped items + */ +function test($a, $b): void +{ +} + +?>