Skip to content

Commit 7c5025b

Browse files
[DowngradePhp80] Handle not match @return with native return type on DowngradeUnionTypeDeclarationRector (#295)
* [DowngradePhp80] Handle not match @return with native return type on DowngradeUnionTypeDeclarationRector * Fix * Fix * [ci-review] Rector Rectify * keep doc * keep doc --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 31b8bde commit 7c5025b

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;
4+
5+
final class MatchReturnDocWithNative
6+
{
7+
/**
8+
* @return \stdClass[]|null|int
9+
*/
10+
public function run($value): int|null|array
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;
20+
21+
final class MatchReturnDocWithNative
22+
{
23+
/**
24+
* @return \stdClass[]|null|int
25+
*/
26+
public function run($value)
27+
{
28+
}
29+
}
30+
31+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;
4+
5+
final class NotMatchReturnDocWithNative
6+
{
7+
/**
8+
* @return \stdClass[]|null
9+
*/
10+
public function run($value): int|null|array
11+
{
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDeclarationRector\Fixture;
20+
21+
final class NotMatchReturnDocWithNative
22+
{
23+
/**
24+
* @return \stdClass[]|null
25+
*/
26+
public function run($value)
27+
{
28+
}
29+
}
30+
31+
?>

src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
private readonly ReflectionResolver $reflectionResolver,
5757
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
5858
private readonly PhpVersionProvider $phpVersionProvider,
59-
private readonly AstResolver $astResolver,
59+
private readonly AstResolver $astResolver
6060
) {
6161
$this->classMethodWillChangeReturnTypes = [
6262
// @todo how to make list complete? is the method list needed or can we use just class names?
@@ -74,7 +74,9 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
7474
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
7575

7676
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
77-
$returnType = $returnTagValueNode instanceof ReturnTagValueNode
77+
78+
$returnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType);
79+
$returnDocType = $returnTagValueNode instanceof ReturnTagValueNode
7880
? $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($returnTagValueNode, $functionLike->returnType)
7981
: $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType);
8082

@@ -88,7 +90,7 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
8890
return;
8991
}
9092

91-
$this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnType);
93+
$this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType);
9294

9395
$functionLike->returnType = null;
9496
if (! $functionLike instanceof ClassMethod) {

0 commit comments

Comments
 (0)