Skip to content

Commit

Permalink
Skip null type in ReturnTypeFromStrictScalarReturnExprRector as often…
Browse files Browse the repository at this point in the history
… not desired (#5462)
  • Loading branch information
TomasVotruba authored Jan 13, 2024
1 parent 2ac26e7 commit 9d310ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector\Fixture;

class SkipNullAsTemporary
{
public function getValue()
{
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function refactor(Node $node): array|If_|null
// only default → basically unwrap
if (! $onlyCase->cond instanceof Expr) {
// remove default clause because it cause syntax error
return array_filter($onlyCase->stmts, static fn(Stmt $stmt): bool => ! $stmt instanceof Break_);
return array_filter($onlyCase->stmts, static fn (Stmt $stmt): bool => ! $stmt instanceof Break_);
}

$if = new If_(new Identical($node->cond, $onlyCase->cond));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\UnionType;
use PHPStan\Analyser\Scope;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
Expand Down Expand Up @@ -117,6 +118,11 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

// skip null as often placeholder value and not an only type
if ($scalarReturnType instanceof NullType) {
return null;
}

$returnTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($scalarReturnType, TypeKind::RETURN);
if (! $returnTypeNode instanceof Node) {
return null;
Expand Down

0 comments on commit 9d310ce

Please sign in to comment.