Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare type directly in CleanupUnneededNullsafeOperatorRector #6086

Merged
merged 2 commits into from
Jun 30, 2024
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
Expand Up @@ -16,5 +16,3 @@ function run(): ?SkipNullable
}

echo run()?->getString();

?>
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\TypeDeclaration\TypeAnalyzer\ReturnStrictTypeAnalyzer;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -93,14 +93,8 @@ public function refactor(Node $node): ?Node
return null;
}

$returnNode = $this->returnStrictTypeAnalyzer->resolveMethodCallReturnNode($node->var);

if (! $returnNode instanceof Node) {
return null;
}

$type = $this->getType($returnNode);
if (! $type instanceof FullyQualifiedObjectType) {
$returnType = $this->returnStrictTypeAnalyzer->resolveMethodCallReturnType($node->var);
if (! $returnType instanceof ObjectType) {
return null;
}

Expand Down
13 changes: 11 additions & 2 deletions rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function collectStrictReturnTypes(array $returns, Scope $scope): array
}

public function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $call): ?Node
{
$returnType = $this->resolveMethodCallReturnType($call);
if (! $returnType instanceof Type) {
return null;
}

return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
}

public function resolveMethodCallReturnType(MethodCall | StaticCall | FuncCall $call): ?Type
{
$methodReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($call);
if ($methodReflection === null) {
Expand Down Expand Up @@ -121,8 +131,7 @@ public function resolveMethodCallReturnNode(MethodCall | StaticCall | FuncCall $
return null;
}

$returnType = $this->normalizeStaticType($call, $returnType);
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
return $this->normalizeStaticType($call, $returnType);
}

private function normalizeStaticType(MethodCall | StaticCall | FuncCall $call, Type $type): Type
Expand Down