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
@@ -0,0 +1,60 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Property\MergePhpstanDocTagIntoNativeRector\Fixture;

final class KeepNativeDescription
{
/**
* @param string|array|null $excludePattern Optional regex; classes matching this are excluded
* from the layer even when $pattern matches.
* @phpstan-param string|list<string|null>|null $excludePattern
*/
public function layerPattern(string $name, string|array $pattern, string|array|null $excludePattern = null): self
{
}

/**
* @return array some items
* @phpstan-return list<string>
*/
public function items(): array
{
}

/**
* @var array some values
* @phpstan-var list<string>
*/
private $values;
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Property\MergePhpstanDocTagIntoNativeRector\Fixture;

final class KeepNativeDescription
{
/**
* @param string|list<string|null>|null $excludePattern Optional regex; classes matching this are excluded
* from the layer even when $pattern matches.
Comment on lines +40 to +41

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked that on phpcs, multilines description on @param will got notice:

Squiz.Commenting.FunctionComment.MissingParamName

I am thinking when the tag is multilines, it should be skipped instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe inline to a single-line comment? It might be hard to indent it exactly as before.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's will make another error, eg: more than 120 chars

*/
public function layerPattern(string $name, string|array $pattern, string|array|null $excludePattern = null): self
{
}

/**
* @return list<string> some items
*/
public function items(): array
{
}

/**
* @var list<string> some values
*/
private $values;
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,23 @@ public function refactor(Node $node): ?Node
$phpstanParamNames = [];
$phpstanVarNames = [];
$hasPhpstanReturn = false;
$nativeDescriptions = [];

foreach ($phpDocNode->children as $phpDocChildNode) {
if (! $phpDocChildNode instanceof PhpDocTagNode) {
continue;
}

// remember native tag descriptions, so they survive when the native tag is overruled
if (in_array($phpDocChildNode->name, self::PHPSTAN_TAG_TO_NATIVE_TAG, true)) {
$tagValueNode = $phpDocChildNode->value;
if (($tagValueNode instanceof ParamTagValueNode || $tagValueNode instanceof VarTagValueNode || $tagValueNode instanceof ReturnTagValueNode) && $tagValueNode->description !== '') {
$nativeDescriptions[$this->resolveTagKey($tagValueNode)] = $tagValueNode->description;
}

continue;
}

if ($phpDocChildNode->name === '@phpstan-param' && $phpDocChildNode->value instanceof ParamTagValueNode) {
$phpstanParamNames[] = $phpDocChildNode->value->parameterName;
} elseif ($phpDocChildNode->name === '@phpstan-var' && $phpDocChildNode->value instanceof VarTagValueNode) {
Expand Down Expand Up @@ -144,6 +155,12 @@ public function refactor(Node $node): ?Node
// promote the @phpstan-* tag to its native counterpart
$nativeTagName = self::PHPSTAN_TAG_TO_NATIVE_TAG[$phpDocChildNode->name] ?? null;
if ($nativeTagName !== null) {
// keep the description of the overruled native tag
$tagValueNode = $phpDocChildNode->value;
if (($tagValueNode instanceof ParamTagValueNode || $tagValueNode instanceof VarTagValueNode || $tagValueNode instanceof ReturnTagValueNode) && $tagValueNode->description === '') {
$tagValueNode->description = $nativeDescriptions[$this->resolveTagKey($tagValueNode)] ?? '';
}

$phpDocNode->children[$key] = new PhpDocTagNode($nativeTagName, $phpDocChildNode->value);
$hasChanged = true;
}
Expand Down Expand Up @@ -195,4 +212,17 @@ private function isOverruledNativeTag(

return false;
}

private function resolveTagKey(ParamTagValueNode|VarTagValueNode|ReturnTagValueNode $tagValueNode): string
{
if ($tagValueNode instanceof ParamTagValueNode) {
return '@param ' . $tagValueNode->parameterName;
}

if ($tagValueNode instanceof VarTagValueNode) {
return '@var ' . $tagValueNode->variableName;
}

return '@return';
}
}
Loading