Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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+)(?<name>\$\w+)(\s+)(?<type>(?:[|\\\\\w\[\]]|<[^<>]*>)+)#';
private const string NAME_THEN_TYPE_REGEX = '#@((?:psalm-|phpstan-)?(?:param|var))(\s+)(?<name>\$\w+)(\s+)(?<type>(?:[|\\\\\w\[\]]|<[^<>]*>)+)(?<rest>\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
{
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* @param $extra Can be used by model to determine what to unlock
*/
function test($extra): void
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/**
* @param $result The result value returned by the callback
*/
function test($result): void
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/**
* @param $x array<int, string>|null
* @phpstan-param $y int
* @psalm-var $c list<int>
* @var $z Foo\Bar
*/
function test($x, $y): void
{
}

?>
-----
<?php

/**
* @param array<int, string>|null $x
* @phpstan-param int $y
* @psalm-var list<int> $c
* @var Foo\Bar $z
*/
function test($x, $y): void
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @param $a string the input value
* @param $b array<int, string> mapped items
*/
function test($a, $b): void
{
}

?>
-----
<?php

/**
* @param string $a the input value
* @param array<int, string> $b mapped items
*/
function test($a, $b): void
{
}

?>
Loading