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,31 @@
<?php

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

final class MatchReturnDocWithNative
{
/**
* @return \stdClass[]|null|int
*/
public function run($value): int|null|array
{
}
}

?>
-----
<?php

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

final class MatchReturnDocWithNative
{
/**
* @return \stdClass[]|null|int
*/
public function run($value)
{
}
}

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

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

final class NotMatchReturnDocWithNative
{
/**
* @return \stdClass[]|null
*/
public function run($value): int|null|array
{
}
}

?>
-----
<?php

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

final class NotMatchReturnDocWithNative
{
/**
* @return \stdClass[]|null
*/
public function run($value)
{
}
}

?>
8 changes: 5 additions & 3 deletions src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer,
private readonly PhpVersionProvider $phpVersionProvider,
private readonly AstResolver $astResolver,
private readonly AstResolver $astResolver
) {
$this->classMethodWillChangeReturnTypes = [
// @todo how to make list complete? is the method list needed or can we use just class names?
Expand All @@ -74,7 +74,9 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);

$returnTagValueNode = $phpDocInfo->getReturnTagValue();
$returnType = $returnTagValueNode instanceof ReturnTagValueNode

$returnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType);
$returnDocType = $returnTagValueNode instanceof ReturnTagValueNode
? $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($returnTagValueNode, $functionLike->returnType)
: $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType);

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

$this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnType);
$this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType);

$functionLike->returnType = null;
if (! $functionLike instanceof ClassMethod) {
Expand Down