Skip to content

Commit

Permalink
[DowngradePhp80] Do not change correct union array docblock on Downgr…
Browse files Browse the repository at this point in the history
…adeUnionTypeDeclarationRector (#191)

* [DowngradePhp80] Do not change correct union array docblock on DowngradeUnionTypeDeclarationRector

* [ci-review] Rector Rectify

* Fixed

* Fix

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Sep 11, 2023
1 parent fa41cc7 commit 74058c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;

use PhpParser\Node;

final class DoNotChangeCorrectUnionArrayDocblockOnParam2
{
/**
* @param Node|Node[] $node
*/
function execute(Node | array $node): void
{
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;

use PhpParser\Node;

final class DoNotChangeCorrectUnionArrayDocblockOnParam2
{
/**
* @param Node|Node[] $node
*/
function execute($node): void
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function isFollowed(File $file, Node $node): bool

return ! isset($oldTokens[$nextTokenPosition]) ||
isset($oldTokens[$nextTokenPosition][1]) &&
\str_starts_with((string) $oldTokens[$nextTokenPosition][1], "\n");
\str_starts_with($oldTokens[$nextTokenPosition][1], "\n");
}
}
11 changes: 9 additions & 2 deletions src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Function_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -223,11 +224,17 @@ private function moveParamTypeToParamDoc(
Param $param,
Type $type
): void {
$param->type = null;

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$paramName = $this->nodeNameResolver->getName($param);
$this->phpDocTypeChanger->changeParamType($functionLike, $phpDocInfo, $type, $param, $paramName);

$param->type = null;
$phpDocParamType = $phpDocInfo->getParamType($paramName);
if (! $type instanceof MixedType && $type::class === $phpDocParamType::class) {
return;
}

$this->phpDocTypeChanger->changeParamType($functionLike, $phpDocInfo, $type, $param, $paramName);
}

/**
Expand Down

0 comments on commit 74058c2

Please sign in to comment.